Example #1
0
        public void Normalize_ManyUncancelableTokens_ReturnsTokenThatCannotCancel()
        {
            var result = CancellationTokenHelpers.Normalize(CancellationToken.None, CancellationToken.None, CancellationToken.None);

            Assert.IsFalse(result.Token.CanBeCanceled);
            Assert.AreEqual(CancellationToken.None, result.Token);
        }
Example #2
0
        public void FromTask_ReturnsTokenWithoutCancellationRequested()
        {
            var tcs = new TaskCompletionSource();

            var result = CancellationTokenHelpers.FromTask(tcs.Task);

            Assert.IsTrue(result.Token.CanBeCanceled);
            Assert.IsFalse(result.Token.IsCancellationRequested);
        }
Example #3
0
        public void Normalize_OneCancelableTokenWithManyUncancelableTokens_ReturnsCancelableToken()
        {
            var cts = new CancellationTokenSource();

            var result = CancellationTokenHelpers.Normalize(CancellationToken.None, cts.Token, CancellationToken.None, CancellationToken.None);

            Assert.IsTrue(result.Token.CanBeCanceled);
            Assert.AreEqual(cts.Token, result.Token);
        }
Example #4
0
        public void FromTaskSynchronously_TaskCompletes_TokenGetsCancellationRequested()
        {
            var tcs    = new TaskCompletionSource();
            var result = CancellationTokenHelpers.FromTask(tcs.Task, TaskContinuationOptions.ExecuteSynchronously);

            tcs.SetResult();

            Assert.IsTrue(result.Token.CanBeCanceled);
            Assert.IsTrue(result.Token.IsCancellationRequested);
        }
Example #5
0
        public void Normalize_ManyCancelableTokens_ReturnsNewCancelableToken()
        {
            var cts1 = new CancellationTokenSource();
            var cts2 = new CancellationTokenSource();

            var result = CancellationTokenHelpers.Normalize(cts1.Token, cts2.Token);

            Assert.IsTrue(result.Token.CanBeCanceled);
            Assert.AreNotEqual(cts1.Token, result);
            Assert.AreNotEqual(cts2.Token, result);
        }
Example #6
0
        public static Task <bool> Before(this Task primary, TimeSpan timeout, params CancellationToken[] tokens)
        {
            var localTCS = new TaskCompletionSource();

            foreach (var token in tokens)
            {
                token.Register(localTCS.SetResult);
            }
            CancellationTokenHelpers.Timeout(timeout).Token.Register(localTCS.SetResult);
            return(Task.WhenAny(primary, localTCS.Task).ContinueWith(t => { return (primary.Status == TaskStatus.RanToCompletion); }));
        }
Example #7
0
 /// <summary>
 /// [Caution] As per the AsyncEx notes, this is a hack, and neither thread-safe nor truly immediate.
 /// </summary>
 public static bool IsSet(this AsyncManualResetEvent source)
 {
     try
     {
         source.Wait(CancellationTokenHelpers.FromTask(Task.Delay(5)).Token);
         return(true);
     }
     catch (System.OperationCanceledException)
     {
         return(false);
     }
 }
        public async Task Should_receive_multiple_packets()
        {
            TcpTransport transport = CreateTcpTransport();

            var receivedMessages = new AsyncProducerConsumerQueue <byte[]>();

            transport.Subscribe(receivedMessages.Enqueue);

            await transport.ConnectAsync();

            Socket clientSocket = _serverSocket.Accept();

            byte[] payload1 = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray();
            var    packet1  = new TcpTransportPacket(0, payload1);

            byte[] payload2 = Enumerable.Range(11, 40).Select(i => (byte)i).ToArray();
            var    packet2  = new TcpTransportPacket(1, payload2);

            byte[] payload3 = Enumerable.Range(51, 205).Select(i => (byte)i).ToArray();
            var    packet3  = new TcpTransportPacket(2, payload3);

            byte[] allData = ArrayUtils.Combine(packet1.Data, packet2.Data, packet3.Data);

            byte[] dataPart1;
            byte[] dataPart2;
            allData.Split(50, out dataPart1, out dataPart2);

            clientSocket.Send(dataPart1);
            await Task.Delay(100);

            clientSocket.Send(dataPart2);
            await Task.Delay(100);

            byte[] receivedData1 = await receivedMessages.DequeueAsync(CancellationTokenHelpers.Timeout(1000).Token);

            receivedData1.Should().BeEquivalentTo(payload1);

            byte[] receivedData2 = await receivedMessages.DequeueAsync(CancellationTokenHelpers.Timeout(1000).Token);

            receivedData2.Should().BeEquivalentTo(payload2);

            byte[] receivedData3 = await receivedMessages.DequeueAsync(CancellationTokenHelpers.Timeout(1000).Token);

            receivedData3.Should().BeEquivalentTo(payload3);

            await transport.DisconnectAsync();

            clientSocket.Close();
        }
Example #9
0
 private void CreateCTS()
 {
     cts = CancellationTokenSource.CreateLinkedTokenSource(CancellationTokenHelpers.Normalize(externalTokens).Token);
     tokenRegistrations.Clear();
     tokenRegistrations.Add(cts.Token, cts.Token.Register(Deactivate));
 }