< Summary

Class:DCLServices.Lambdas.LambdaResponsePagePointer[T]
Assembly:LambdasService
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdaResponsePagePointer.cs
Covered lines:21
Uncovered lines:11
Coverable lines:32
Total lines:98
Line coverage:65.6% (21 of 32)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:7
Method coverage:71.4% (5 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LambdaResponsePagePointer(...)0%110100%
LambdaResponsePagePointer(...)0%110100%
GetPageAsync()0%12.699064.29%
Dispose()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/Lambdas/LambdaResponsePagePointer.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine.Pool;
 6
 7namespace DCLServices.Lambdas
 8{
 9    /// <summary>
 10    /// Caches paginated results until `Dispose` is called
 11    /// assuming the results are immutable while iterating
 12    /// </summary>
 13    /// <typeparam name="T"></typeparam>
 14    public class LambdaResponsePagePointer<T> : IDisposable where T : PaginatedResponse
 15    {
 16        private readonly int pageSize;
 17        internal readonly Dictionary<int, (T page, DateTime retrievalTime)> cachedPages;
 18        private readonly ILambdaServiceConsumer<T> serviceConsumer;
 19        private readonly TimeSpan cacheExpiration;
 20        private readonly CancellationToken cancellationToken;
 21        private readonly string constEndPoint;
 22
 223        internal bool isDisposed { get; private set; }
 24
 025        internal IReadOnlyDictionary<int, (T page, DateTime retrievalTime)> CachedPages => cachedPages;
 26
 27        /// <param name="constEndpoint"></param>
 28        /// <param name="pageSize"></param>
 29        /// <param name="cancellationToken">Pass Cancellation Token so the pointer will be automatically disposed on can
 30        /// <param name="consumer"></param>
 31        public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken,
 232            ILambdaServiceConsumer<T> consumer) : this(constEndpoint, pageSize, cancellationToken, consumer, TimeSpan.Fr
 33        {
 234        }
 35
 36        /// <param name="constEndpoint"></param>
 37        /// <param name="pageSize"></param>
 38        /// <param name="cancellationToken">Pass Cancellation Token so the pointer will be automatically disposed on can
 39        /// <param name="consumer"></param>
 240        public LambdaResponsePagePointer(string constEndpoint, int pageSize, CancellationToken cancellationToken,
 41            ILambdaServiceConsumer<T> consumer, TimeSpan cacheExpiration )
 42        {
 243            this.pageSize = pageSize;
 244            this.cancellationToken = cancellationToken;
 245            this.constEndPoint = constEndpoint;
 46
 247            cachedPages = DictionaryPool<int, (T page, DateTime retrievalTime)>.Get();
 248            serviceConsumer = consumer;
 249            this.cacheExpiration = cacheExpiration;
 50
 251            cancellationToken.Register(Dispose, false);
 252        }
 53
 54        /// <summary>
 55        /// Retrieves a page from the endpoint or cache
 56        /// </summary>
 57        /// <exception cref="ObjectDisposedException"></exception>
 58        /// <exception cref="OperationCanceledException"></exception>
 59        public async UniTask<(T response, bool success)> GetPageAsync(int pageNum, CancellationToken localCancellationTo
 60        {
 261            if (isDisposed)
 062                throw new ObjectDisposedException(nameof(LambdaResponsePagePointer<T>));
 63
 64
 265            if (cachedPages.TryGetValue(pageNum, out var cachedPage))
 66            {
 067                if (DateTime.Now < cachedPage.retrievalTime + cacheExpiration)
 068                    return (cachedPage.page, true);
 69
 70                // cache expired
 071                cachedPages.Remove(pageNum);
 72            }
 73
 274            var ct = this.cancellationToken;
 75
 276            if (localCancellationToken != CancellationToken.None && !localCancellationToken.Equals(ct))
 077                ct = CancellationTokenSource.CreateLinkedTokenSource(this.cancellationToken, localCancellationToken).Tok
 78
 279            var res = await serviceConsumer.CreateRequest(constEndPoint, pageSize, pageNum, additionalData, ct);
 80
 281            if (res.success)
 82            {
 283                cachedPages[pageNum] = (res.response, DateTime.Now);
 84            }
 85
 286            return res;
 287        }
 88
 89        public void Dispose()
 90        {
 091            if (isDisposed)
 092                return;
 93
 094            DictionaryPool<int, (T, DateTime)>.Release(cachedPages);
 095            isDisposed = true;
 096        }
 97    }
 98}