< Summary

Class:DCL.Helpers.NFT.Markets.OpenSea_Internal.RequestScheduler
Assembly:OpenSea_Internal
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/RequestScheduler/RequestScheduler.cs
Covered lines:13
Uncovered lines:11
Coverable lines:24
Total lines:68
Line coverage:54.1% (13 of 24)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RequestScheduler()0%110100%
EnqueueRequest(...)0%220100%
OnRequestReadyToSchedule(...)0%4.683042.86%
SendRequest(...)0%220100%
ScheduleTaskRoutine()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/ServiceProviders/OpenSea/RequestScheduler/RequestScheduler.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace DCL.Helpers.NFT.Markets.OpenSea_Internal
 7{
 8    internal class RequestScheduler
 9    {
 10        const float REQUEST_DELAY = 0.4f; // max ~2 requests per second
 11
 12        public event Action<IRequestHandler> OnRequestReadyToSend;
 13
 67114        private readonly List<IRequestHandler> scheduledRequests = new List<IRequestHandler>();
 15
 16        private float lastRequestSentTime = 0;
 17        private Coroutine scheduleTask = null;
 18
 19        public void EnqueueRequest(IRequestHandler requests)
 20        {
 1221            requests.schedulableRequestHandler.OnReadyToSchedule -= OnRequestReadyToSchedule;
 22
 1223            if (requests.schedulableRequestHandler.isReadyToSchedule)
 24            {
 425                OnRequestReadyToSchedule(requests);
 426            }
 27            else
 28            {
 829                requests.schedulableRequestHandler.OnReadyToSchedule += OnRequestReadyToSchedule;
 30            }
 831        }
 32
 33        private void OnRequestReadyToSchedule(IRequestHandler requestHandler)
 34        {
 1135            if (Time.unscaledTime - lastRequestSentTime >= REQUEST_DELAY)
 36            {
 1137                SendRequest(requestHandler);
 1138                return;
 39            }
 40
 041            scheduledRequests.Add(requestHandler);
 42
 043            if (scheduleTask == null)
 44            {
 045                scheduleTask = CoroutineStarter.Start(ScheduleTaskRoutine());
 46            }
 047        }
 48
 49        private void SendRequest(IRequestHandler requestHandler)
 50        {
 1151            lastRequestSentTime = Time.unscaledTime;
 1152            OnRequestReadyToSend?.Invoke(requestHandler);
 1153        }
 54
 55        IEnumerator ScheduleTaskRoutine()
 56        {
 057            while (scheduledRequests.Count > 0)
 58            {
 059                float waitTime = Mathf.Clamp(REQUEST_DELAY - (Time.unscaledTime - lastRequestSentTime), 0, REQUEST_DELAY
 060                yield return WaitForSecondsCache.Get(waitTime);
 061                SendRequest(scheduledRequests[0]);
 062                scheduledRequests.RemoveAt(0);
 63            }
 64
 065            scheduleTask = null;
 066        }
 67    }
 68}