Exemple #1
0
 private void Receive([NotNull] IReceiver receiver, RequestPriority priority)
 {
     if (receiver == null)
     {
         throw new ArgumentNullException(nameof(receiver));
     }
     _factories.Run(async() =>
     {
         var token = _tokenSource.Token;
         var span  = await receiver.ExecuteAsync(token).ConfigureAwait(false);
         var now   = DateTime.Now;
         if (token.IsCancellationRequested)
         {
             return;
         }
         if (span.Ticks > (DateTime.MaxValue - now).Ticks)
         {
             // register as MaxValue
             RegisterReceiver(DateTime.MaxValue, receiver, priority);
         }
         else
         {
             RegisterReceiver(now + span, receiver, priority);
         }
     }, (int)priority, _tokenSource.Token);
 }
Exemple #2
0
        public async Task TestTaskFactoryDistrict()
        {
            var tasks = new[] {
                _district.Run(() => Debug.WriteLine("DummyStick"), 0),
                _district.Run(() => WaitAndEnqueue(10, "0"), 0),
                _district.Run(() => WaitAndEnqueue(15, "1"), 0),
                _district.Run(() => WaitAndEnqueue(20, "2"), 0),

                _district.Run(() => WaitAndEnqueue(20, "6"), 1),
                _district.Run(() => WaitAndEnqueue(20, "7"), 1),
                _district.Run(() => WaitAndEnqueue(20, "8"), 1),

                _district.Run(() => WaitAndEnqueue(20, "3"), 0),
                _district.Run(() => WaitAndEnqueue(20, "4"), 0),
                _district.Run(() => WaitAndEnqueue(20, "5"), 0),

                _district.Run(() => WaitAndEnqueue(20, "9"), 2),
                _district.Run(() => WaitAndEnqueue(20, "a"), 2),
                _district.Run(() => WaitAndEnqueue(20, "b"), 2),
            };
            await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(TimeSpan.FromSeconds(20)));

            var success = "0123456789ab";

            foreach (var str in _resultList)
            {
                Debug.WriteLine(str);
            }
            Assert.AreEqual(success.Length, _resultList.Count);
            CollectionAssert.AreEqual(success.Select(s => s.ToString()).ToArray(), _resultList.ToArray());
        }