示例#1
0
        private async Task StartHandlingClientRequestAsync()
        {
            while (_disposeToken.IsCancellationRequested == false)
            {
                _log.InfoFormat("FakeTcpServer: Accepting clients.");
                _client = await _listener.AcceptTcpClientAsync();

                _log.InfoFormat("FakeTcpServer: Connected client");
                if (OnClientConnected != null)
                {
                    OnClientConnected();
                }
                _semaphoreSlim.Release();

                try
                {
                    using (_client)
                    {
                        var buffer = new byte[4096];
                        var stream = _client.GetStream();

                        while (!_disposeToken.IsCancellationRequested)
                        {
                            //connect client
                            var connectTask = stream.ReadAsync(buffer, 0, buffer.Length, _disposeToken.Token);

                            var bytesReceived = await connectTask;

                            if (bytesReceived > 0)
                            {
                                if (OnBytesReceived != null)
                                {
                                    OnBytesReceived(buffer.Take(bytesReceived).ToArray());
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("FakeTcpServer: Client exception...  Exception:{0}", ex.Message);
                }

                _log.ErrorFormat("FakeTcpServer: Client Disconnected.");
                await _semaphoreSlim.WaitAsync(); //remove the one client

                if (OnClientDisconnected != null)
                {
                    OnClientDisconnected();
                }
            }
        }
示例#2
0
        public async Task SendAsyncShouldBlockWhenMaximumAsyncQueueReached()
        {
            _log.InfoFormat("Start SendAsyncShouldBlockWhenMaximumAsyncQueueReached");
            int count       = 0;
            var semaphore   = new SemaphoreSlim(0);
            var routerProxy = new FakeBrokerRouter();

            //block the second call returning from send message async
            routerProxy.BrokerConn0.ProduceResponseFunction = async() => { await semaphore.WaitAsync(); return(new ProduceResponse()); };

            var router = routerProxy.Create();

            using (var producer = new Producer(router, maximumAsyncRequests: 1)
            {
                BatchSize = 1
            })
            {
                var messages = new[] { new Message("1") };

                Task.Run(async() =>
                {
                    var t = producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);
                    Interlocked.Increment(ref count);
                    await t;

                    t = producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);

                    Interlocked.Increment(ref count);
                    await t;
                });

                await TaskTest.WaitFor(() => producer.AsyncCount > 0);

                await TaskTest.WaitFor(() => count > 0);

                Assert.That(count, Is.EqualTo(1), "Only one SendMessageAsync should continue.");

                semaphore.Release();
                await TaskTest.WaitFor(() => count > 1);

                Assert.That(count, Is.EqualTo(2), "The second SendMessageAsync should continue after semaphore is released.");
            }
        }