コード例 #1
0
        public async Task FilterTest()
        {
            using var dictionary = new WaitingDictionary <int, Mock>();
            await dictionary.SetAsync(1, new Mock());

            Assert.IsEmpty(dictionary.Filter(tcs => tcs.Value.Task.Status == TaskStatus.Canceled));
        }
コード例 #2
0
        public async Task RemoveTest()
        {
            using var dictionary = new WaitingDictionary <int, Mock>();
            await dictionary.SetAsync(1, new Mock());

            Assert.IsTrue(await dictionary.TryRemoveAsync(1));
        }
コード例 #3
0
        public async Task DuplicateErrorTest()
        {
            const int key        = 1337;
            var       dictionary = new WaitingDictionary <int, Mock>();
            var       mock       = new Mock();
            await dictionary.SetAsync(key, mock);

            Assert.CatchAsync <InvalidOperationException>(() => dictionary.SetAsync(key, mock));
        }
コード例 #4
0
        public async Task MultipleWaitTest()
        {
            const int key = 1337;

            using var dictionary = new WaitingDictionary <int, Mock>();
            var waitTask1 = dictionary.WaitAsync(key);
            var waitTask2 = dictionary.WaitAsync(key);

            Assert.IsTrue(!waitTask1.IsCompletedSuccessfully);
            Assert.IsTrue(!waitTask2.IsCompletedSuccessfully);
            await dictionary.SetAsync(key, new Mock());

            await Task.Delay(200);

            Assert.IsTrue(waitTask1.IsCompletedSuccessfully);
            Assert.IsTrue(waitTask2.IsFaulted);
        }
コード例 #5
0
        public async Task DuplicateTest()
        {
            const int key = 1337;

            using var dictionary = new WaitingDictionary <int, Mock>(
                      new MiddlewareBuilder <Mock>()
                      .RegisterDuplicateActionInSet((old, @new) => new Mock(old, @new))
                      );
            var mock = new Mock();
            await dictionary.SetAsync(key, mock);

            await dictionary.SetAsync(key, mock);

            var waitMock = await dictionary.WaitAsync(key);

            Assert.IsNotEmpty(waitMock.Nodes);
        }
コード例 #6
0
        public async Task NormalTest()
        {
            const int key = 1337;

            using var dictionary = new WaitingDictionary <int, Mock>(
                      new MiddlewareBuilder <Mock>()
                      .RegisterCompletionActionInSet(() => TestContext.WriteLine("Set completed"))
                      .RegisterCompletionActionInWait(() => TestContext.WriteLine("Wait completed"))
                      );
            var waitTask = dictionary.WaitAsync(key);

            Assert.IsTrue(!waitTask.IsCompletedSuccessfully);
            var mock = new Mock();
            await dictionary.SetAsync(key, mock);

            Assert.IsTrue(waitTask.IsCompletedSuccessfully);
        }
コード例 #7
0
        public void EnumerateTest()
        {
            using var dictionary = new WaitingDictionary <int, Mock>();
            dictionary.SetAsync(1, new Mock());
            dictionary.SetAsync(2, new Mock());

            var idx = 0;

            foreach (var taskCompletionSource in dictionary)
            {
                Assert.NotNull(taskCompletionSource);
                idx++;
            }

            Assert.IsNotEmpty(dictionary);
            Assert.Greater(dictionary.Count, 1);
            Assert.Greater(idx, 1);
        }
コード例 #8
0
 public void InterfacesTest()
 {
     using var dictionary = new WaitingDictionary <int, Mock>();
     Assert.Catch <NotSupportedException>(() => dictionary.Add(new KeyValuePair <int, TaskCompletionSource <Mock> >()));
     Assert.Catch <NotSupportedException>(() => dictionary.Add(123, new TaskCompletionSource <Mock>()));
     Assert.Catch <NotSupportedException>(() => ((ICollection <KeyValuePair <int, TaskCompletionSource <Mock> > >)dictionary).Remove(new KeyValuePair <int, TaskCompletionSource <Mock> >()));
     Assert.Catch <NotSupportedException>(() => ((IDictionary <int, TaskCompletionSource <Mock> >)dictionary).Remove(123));
     Assert.Catch <NotSupportedException>(() => dictionary[123] = new TaskCompletionSource <Mock>());
     Assert.Catch <NotSupportedException>(() =>
     {
         using var _ = new WaitingDictionary <int, Mock>
               {
                   { 123, new TaskCompletionSource <Mock>() }
               };
     });
     Assert.IsNull(((IDictionary <int, TaskCompletionSource <Mock> >)dictionary).TryGetValue(123, out var test) ? test : null);
     Assert.IsNull(dictionary[123]);
 }
コード例 #9
0
        public async Task CancelTest(Type type)
        {
            const int key = 1337;

            using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
            var dictionary = new WaitingDictionary <int, Mock>(
                new MiddlewareBuilder <Mock>()
                .RegisterCancellationActionInWait((tcs, hasOwnToken) => tcs.SetException((Exception)Activator.CreateInstance(type)))
                );
            var waitTask = dictionary.WaitAsync(key, cts.Token);
            await Task.Delay(200, CancellationToken.None);

            try
            {
                await waitTask;
            }
            catch (Exception e)
            {
                Assert.IsTrue(e.GetType() == type);
            }
        }
コード例 #10
0
        private TcpClientIo(TcpClientIoOptions tcpClientIoOptions, ILogger <TcpClientIo <TId, TRequest, TResponse> > logger)
        {
            var pipe = new Pipe();

            Id          = Guid.NewGuid();
            _logger     = logger;
            _options    = tcpClientIoOptions ?? TcpClientIoOptions.Default;
            _batchRules = TcpBatchRules <TResponse> .Default;
            _baseCancellationTokenSource = new CancellationTokenSource();
            _baseCancellationToken       = _baseCancellationTokenSource.Token;
            _bufferBlockRequests         = new BufferBlock <SerializedRequest>();
            var middleware = new MiddlewareBuilder <ITcpBatch <TResponse> >()
                             .RegisterCancellationActionInWait((tcs, hasOwnToken) =>
            {
                if (_disposing || hasOwnToken)
                {
                    tcs.TrySetCanceled();
                }
                else if (!_disposing && _pipelineReadEnded)
                {
                    tcs.TrySetException(TcpClientIoException.ConnectionBroken());
                }
            })
                             .RegisterDuplicateActionInSet((batch, newBatch) => _batchRules.Update(batch, newBatch.Single()))
                             .RegisterCompletionActionInSet(() => _consumingResetEvent.Set());

            _completeResponses = new WaitingDictionary <TId, ITcpBatch <TResponse> >(middleware);
            _arrayPool         = ArrayPool <byte> .Create();

            var bitConverterHelper = new BitConverterHelper(_options);

            _serializer            = new TcpSerializer <TRequest>(bitConverterHelper, length => _arrayPool.Rent(length));
            _deserializer          = new TcpDeserializer <TId, TResponse>(bitConverterHelper);
            _writeResetEvent       = new AsyncManualResetEvent();
            _readResetEvent        = new AsyncManualResetEvent();
            _consumingResetEvent   = new AsyncManualResetEvent();
            _deserializePipeReader = pipe.Reader;
            _deserializePipeWriter = pipe.Writer;
        }