Ejemplo n.º 1
0
 public void Stop()
 {
     _stopping = true;
     if (_timer != null)
     {
         _timer.Dispose();
         _timer = null;
     }
     while (_scheduled > 0)
     {
         Thread.Sleep(100);
     }
     if (_count != 0)
     {
         while (true)
         {
             RequestInstance instance = DequeueInstance(false);
             if (instance == null)
             {
                 break;
             }
             instance.Reject();
         }
     }
 }
Ejemplo n.º 2
0
        public void SameContextComesBackNormally()
        {
            RequestInstance requestInstance = BuildRequest(r => { });
            RequestInstance executeInstance = _queue.GetInstanceToExecute(requestInstance);

            requestInstance.ShouldBeSameAs(executeInstance);
        }
Ejemplo n.º 3
0
        private void ExecuteIfNeeded(object state)
        {
            Interlocked.Decrement(ref _scheduled);
            if (_stopping)
            {
                return;
            }
            if (_count == 0)
            {
                return;
            }
            ThreadCounts available = _threading.GetAvailableThreads();
            int          active    = _maxThreads.WorkerThreads - available.WorkerThreads;

            if (active >= _activeThreadsBeforeLocalRequestsQueue)
            {
                return;
            }
            bool            localOnly = active >= _activeThreadsBeforeRemoteRequestsQueue;
            RequestInstance instance  = DequeueInstance(localOnly);

            if (instance == null)
            {
                return;
            }
            ScheduleExecuteIfNeeded();
            instance.Execute();
        }
Ejemplo n.º 4
0
        public void CallbacksWillExecuteRequestsInOrder()
        {
            _threading.AvailableThreads = new ThreadCounts(_threading.MaxThreads.WorkerThreads, 0);

            RequestInstance req1 = BuildRequest(r => { });
            RequestInstance req2 = BuildRequest(r => { });
            RequestInstance req3 = BuildRequest(r => { });
            RequestInstance req4 = BuildRequest(r => { });

            _queue.GetInstanceToExecute(req1).ShouldBe(null);
            _queue.GetInstanceToExecute(req2).ShouldBe(null);
            _queue.GetInstanceToExecute(req3).ShouldBe(null);
            _queue.GetInstanceToExecute(req4).ShouldBe(null);

            _threading.Callbacks.Count.ShouldBe(2);
            req1.Task.IsCompleted.ShouldBe(false);
            req2.Task.IsCompleted.ShouldBe(false);
            req3.Task.IsCompleted.ShouldBe(false);
            req4.Task.IsCompleted.ShouldBe(false);

            _threading.AvailableThreads = _threading.MaxThreads;

            _threading.DoOneCallback();
            _threading.Callbacks.Count.ShouldBe(2);
            req1.Task.IsCompleted.ShouldBe(true);
            req2.Task.IsCompleted.ShouldBe(false);
            req3.Task.IsCompleted.ShouldBe(false);
            req4.Task.IsCompleted.ShouldBe(false);

            _threading.DoOneCallback();
            _threading.Callbacks.Count.ShouldBe(2);
            req1.Task.IsCompleted.ShouldBe(true);
            req2.Task.IsCompleted.ShouldBe(true);
            req3.Task.IsCompleted.ShouldBe(false);
            req4.Task.IsCompleted.ShouldBe(false);

            _threading.DoOneCallback();
            _threading.Callbacks.Count.ShouldBe(2);
            req1.Task.IsCompleted.ShouldBe(true);
            req2.Task.IsCompleted.ShouldBe(true);
            req3.Task.IsCompleted.ShouldBe(true);
            req4.Task.IsCompleted.ShouldBe(false);

            _threading.DoOneCallback();
            _threading.Callbacks.Count.ShouldBe(1);
            req1.Task.IsCompleted.ShouldBe(true);
            req2.Task.IsCompleted.ShouldBe(true);
            req3.Task.IsCompleted.ShouldBe(true);
            req4.Task.IsCompleted.ShouldBe(true);

            _threading.DoOneCallback();
            _threading.Callbacks.Count.ShouldBe(0);
            req1.Task.IsCompleted.ShouldBe(true);
            req2.Task.IsCompleted.ShouldBe(true);
            req3.Task.IsCompleted.ShouldBe(true);
            req4.Task.IsCompleted.ShouldBe(true);
        }
Ejemplo n.º 5
0
        public void EarlierInstanceComesBackIfItWasQueued()
        {
            RequestInstance requestInstance1 = BuildRequest(r => { });
            RequestInstance requestInstance2 = BuildRequest(r => { });

            _threading.AvailableThreads = ThreadCounts.Zero;
            _queue.GetInstanceToExecute(requestInstance1).ShouldBe(null);

            _threading.AvailableThreads = _threading.MaxThreads;
            _queue.GetInstanceToExecute(requestInstance2).ShouldBe(requestInstance1);
        }
Ejemplo n.º 6
0
        public void NullContextComesBackWhenEitherPoolExceeds()
        {
            RequestInstance requestInstance = BuildRequest(r => { });

            _threading.AvailableThreads = new ThreadCounts(0, _threading.MaxThreads.CompletionPortThreads);
            _queue.GetInstanceToExecute(requestInstance).ShouldBe(null);

            _threading.AvailableThreads = new ThreadCounts(_threading.MaxThreads.WorkerThreads, 0);
            _queue.GetInstanceToExecute(requestInstance).ShouldBe(null);

            _threading.AvailableThreads = _threading.MaxThreads;
            _queue.GetInstanceToExecute(requestInstance).ShouldNotBe(null);
        }
Ejemplo n.º 7
0
        public void OnlyLocalRequestsExecuteAtCertainLevels()
        {
            int halfway = (_options.ActiveThreadsBeforeRemoteRequestsQueue + _options.ActiveThreadsBeforeLocalRequestsQueue) / 2;

            _threading.AvailableThreads = _threading.MaxThreads.Subtract(new ThreadCounts(halfway, halfway));

            RequestInstance requestInstance1 = BuildRequest(r => r.IsLocal = false);
            RequestInstance requestInstance2 = BuildRequest(r => r.IsLocal = true);
            RequestInstance requestInstance3 = BuildRequest(r => r.IsLocal = false);

            _queue.GetInstanceToExecute(requestInstance1).ShouldBe(null);
            _queue.GetInstanceToExecute(requestInstance2).ShouldBe(requestInstance2);
            _queue.GetInstanceToExecute(requestInstance3).ShouldBe(null);
        }
Ejemplo n.º 8
0
        public void LocalInstanceDequeuesFirst()
        {
            RequestInstance requestInstance1 = BuildRequest(r => r.IsLocal = false);
            RequestInstance requestInstance2 = BuildRequest(r => r.IsLocal = true);
            RequestInstance requestInstance3 = BuildRequest(r => { });
            RequestInstance requestInstance4 = BuildRequest(r => { });

            _threading.AvailableThreads = ThreadCounts.Zero;
            _queue.GetInstanceToExecute(requestInstance1).ShouldBe(null);
            _queue.GetInstanceToExecute(requestInstance2).ShouldBe(null);

            _threading.AvailableThreads = _threading.MaxThreads;
            _queue.GetInstanceToExecute(requestInstance3).ShouldBeSameAs(requestInstance2);
            _queue.GetInstanceToExecute(requestInstance4).ShouldBeSameAs(requestInstance1);
        }
Ejemplo n.º 9
0
 private void QueueInstance(RequestInstance instance, bool isLocal)
 {
     lock (_sync)
     {
         if (isLocal)
         {
             _local.Enqueue(instance);
         }
         else
         {
             _remote.Enqueue(instance);
         }
         _count++;
     }
 }
Ejemplo n.º 10
0
        public void RequestsRejectWhenQueueTooLong()
        {
            _options.QueueLengthBeforeIncomingRequestsRejected = 2;
            CreateRequestQueue();

            _threading.AvailableThreads = ThreadCounts.Zero;

            RequestInstance requestInstance1 = BuildRequest(r => { });
            RequestInstance requestInstance2 = BuildRequest(r => { });
            RequestInstance requestInstance3 = BuildRequest(r => { });

            _queue.GetInstanceToExecute(requestInstance1).ShouldBe(null);
            _queue.GetInstanceToExecute(requestInstance2).ShouldBe(null);
            _queue.GetInstanceToExecute(requestInstance3).ShouldBe(null);

            requestInstance1.Task.IsCompleted.ShouldBe(false);
            requestInstance2.Task.IsCompleted.ShouldBe(false);
            requestInstance3.Task.IsCompleted.ShouldBe(true);
        }
Ejemplo n.º 11
0
        public override string Result()
        {
            if (!Initialized)
            {
                throw new Exception("Initialize is required.");
            }

            var byteArrayData = Encoding.UTF8.GetBytes(BuildDataString());

            RequestInstance.ContentLength = byteArrayData.Length;

            RequestInstance.Method = "POST";

            using (var dataStream = RequestInstance.GetRequestStream())
            {
                dataStream.Write(byteArrayData, 0, byteArrayData.Length);
            }

            var response = (HttpWebResponse)RequestInstance.GetResponse();

            return(new StreamReader(response.GetResponseStream()).ReadToEnd());
        }
Ejemplo n.º 12
0
        public override sealed string Result()
        {
            if (!Initialized)
            {
                throw new Exception("Initialize is required.");
            }

            var newUrl = new UriBuilder(String.Format("{0}?", RealUrl.ToString()))
            {
                Query = String.Join("&", Data.Select(x => String.Format("{0}={1}",
                                                                        x.Key, HttpUtility.UrlEncode(x.Value))))
            };


            Url = newUrl.Uri;

            Initialize();

            RequestInstance.Method = "GET";

            using (var response = RequestInstance.GetResponse())
                using (var stream = response.GetResponseStream())
                    return(new StreamReader(stream).ReadToEnd());
        }
Ejemplo n.º 13
0
        public RequestInstance GetInstanceToExecute(RequestInstance requestInstance)
        {
            ThreadCounts availableCounts = _threading.GetAvailableThreads();
            ThreadCounts activeCounts = _maxThreads.Subtract(availableCounts);
            int active = activeCounts.Greatest();
            if (_count == 0 && active < _activeThreadsBeforeRemoteRequestsQueue)
            {
                return requestInstance;
            }

            bool isLocal = requestInstance.IsLocal;
            if (_count >= _queueLengthBeforeIncomingRequestsRejected)
            {
                requestInstance.Reject();
                return null;
            }

            requestInstance.Defer();
            QueueInstance(requestInstance, isLocal);
            if (active < _activeThreadsBeforeRemoteRequestsQueue)
            {
                return DequeueInstance(false);
            }

            if (active < _activeThreadsBeforeLocalRequestsQueue)
            {
                return DequeueInstance(true);
            }

            ScheduleExecuteIfNeeded();
            return null;
        }
Ejemplo n.º 14
0
 private void QueueInstance(RequestInstance instance, bool isLocal)
 {
     lock (_sync)
     {
         if (isLocal)
         {
             _local.Enqueue(instance);
         }
         else
         {
             _remote.Enqueue(instance);
         }
         _count++;
     }
 }