public void TakeWorks()
 {
     var q = new Util.LongPollingQueue<int>();
     q.Add(new[] { 1 });
     var ar = q.BeginTake(5000, null, null);
     var data = q.EndTake(ar);
     Assert.AreEqual(1, data.Count);
 }
        public void TakeWorks()
        {
            var q = new Util.LongPollingQueue <int>();

            q.Add(new[] { 1 });
            var ar   = q.BeginTake(5000, null, null);
            var data = q.EndTake(ar);

            Assert.AreEqual(1, data.Count);
        }
        public void DelayTest()
        {
            var q = new Util.LongPollingQueue <DateTime>();

            int N = 100;

            ThreadPool.QueueUserWorkItem((s) => {
                for (var i = 0; i < N; i++)
                {
                    q.Add(new [] { DateTime.Now });
                    Thread.Sleep(10);
                }
            });

            int beginEndDiff = 0;

            List <TimeSpan> diff     = new List <TimeSpan>();
            AsyncCallback   callback = (ar) =>
            {
                var data = q.EndTake(ar);
                Interlocked.Decrement(ref beginEndDiff);
                var now = DateTime.Now;
                foreach (var item in data)
                {
                    diff.Add(now - item);
                }
                if (diff.Count < N)
                {
                    var readAction = (Action)ar.AsyncState;
                    readAction();
                }
            };

            Action read = null;

            read = () =>
            {
                q.BeginTake(1000, callback, read);
                Interlocked.Increment(ref beginEndDiff);
            };

            read();

            while (diff.Count < N)
            {
                Thread.Sleep(100);
            }

            Assert.AreEqual(N, diff.Count);
            Assert.AreEqual(beginEndDiff, 0);
            var avgDelay = diff.Average(a => a.TotalMilliseconds);

            Assert.IsTrue(avgDelay < 10);
        }
        public void DelayTest()
        {
            var q = new Util.LongPollingQueue<DateTime>();            

            int N = 100;

            ThreadPool.QueueUserWorkItem((s)=> {
                for (var i = 0; i<N; i++) {
                    q.Add(new [] { DateTime.Now });
                    Thread.Sleep(10);
                }                
            });

            int beginEndDiff = 0;

            List<TimeSpan> diff = new List<TimeSpan>();
            AsyncCallback callback = (ar) =>
            {                
                var data = q.EndTake(ar);
                Interlocked.Decrement(ref beginEndDiff);
                var now = DateTime.Now;
                foreach (var item in data)
                    diff.Add(now - item);
                if (diff.Count < N)
                {
                    var readAction = (Action)ar.AsyncState;
                    readAction();
                }
            };

            Action read = null;
            read = () =>
            {
                q.BeginTake(1000, callback, read);
                Interlocked.Increment(ref beginEndDiff);
            };

            read();

            while (diff.Count < N)
                Thread.Sleep(100);

            Assert.AreEqual(N, diff.Count);
            Assert.AreEqual(beginEndDiff, 0);
            var avgDelay = diff.Average(a => a.TotalMilliseconds);

            Assert.IsTrue(avgDelay < 10);
        }