< 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:1
Uncovered lines:23
Coverable lines:24
Total lines:68
Line coverage:4.1% (1 of 24)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RequestScheduler()0%110100%
EnqueueRequest(...)0%6200%
OnRequestReadyToSchedule(...)0%12300%
SendRequest(...)0%6200%
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
 7214        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        {
 021            requests.schedulableRequestHandler.OnReadyToSchedule -= OnRequestReadyToSchedule;
 22
 023            if (requests.schedulableRequestHandler.isReadyToSchedule)
 24            {
 025                OnRequestReadyToSchedule(requests);
 026            }
 27            else
 28            {
 029                requests.schedulableRequestHandler.OnReadyToSchedule += OnRequestReadyToSchedule;
 30            }
 031        }
 32
 33        private void OnRequestReadyToSchedule(IRequestHandler requestHandler)
 34        {
 035            if (Time.unscaledTime - lastRequestSentTime >= REQUEST_DELAY)
 36            {
 037                SendRequest(requestHandler);
 038                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        {
 051            lastRequestSentTime = Time.unscaledTime;
 052            OnRequestReadyToSend?.Invoke(requestHandler);
 053        }
 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}