

using System;
using System.Linq;
using NUnit.Framework;
using QuantConnect.Data.Market;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.DataFeeds.Enumerators;
using QuantConnect.Securities;

package com.quantconnect.lean.Tests.Engine.DataFeeds.Enumerators
{
    [TestFixture]
    public class RateLimitEnumeratorTests
    {
        [Test]
        public void LimitsBasedOnTimeBetweenCalls() {
            currentTime = new DateTime(2015, 10, 10, 13, 6, 0);
            timeProvider = new ManualTimeProvider(currentTime, Global.UTC_ZONE_ID);
            data = Enumerable.Range(0, 100).Select(x -> new Tick {Symbol = CreateSymbol(x)}).GetEnumerator();
            rateLimit = new RateLimitEnumerator(data, timeProvider, Time.OneSecond);

            Assert.IsTrue(rateLimit.MoveNext());

            while (rateLimit.MoveNext() && rateLimit.Current == null ) {
                timeProvider.AdvanceSeconds(0.1);
            }

            delta = (timeProvider.GetUtcNow() - currentTime).TotalSeconds;

            Assert.AreEqual(1, delta);

            Assert.AreEqual( "1", data.Current.Symbol.Value);
        }

        private static Symbol CreateSymbol(int x) {
            return new Symbol(SecurityIdentifier.GenerateBase(x.toString(), Market.USA), x.toString());
        }
    }
}
