Пример #1
0
        public async Task AgentCanHandleMessage()
        {
            var tcs     = new TaskCompletionSource <string>();
            var message = "test";
            var agent   = new AsyncAgent <string, string>(
                string.Empty,
                async(state, msg, ct) =>
            {
                await Task.Delay(0, ct);
                tcs.SetResult(msg);
                return(state);
            },
                (ex, ct) => Task.FromResult(true));

            agent.Send(message);
            var processedMessage = await tcs.Task;

            Assert.Equal(message, processedMessage);
        }
Пример #2
0
        public async Task AgentDoesNotHandleMessagesAfterDispose()
        {
            var tcs   = new TaskCompletionSource <int>();
            var agent = new AsyncAgent <int, int>(
                initialState: 0,
                messageHandler: async(state, msg, ct) =>
            {
                await Task.Delay(0, ct);
                tcs.SetResult(msg);
                return(state);
            },
                errorHandler: (ex, ct) => Task.FromResult(true));

            agent.Dispose();
            agent.Send(1);
            await Task.Delay(50);

            Assert.False(tcs.Task.IsCompleted);
        }
Пример #3
0
        public async Task AgentHandlesMessagesInOrder()
        {
            var       parallelHandlers = 0;
            var       random           = new Random();
            Exception thrownException  = null;
            var       range            = Enumerable.Range(0, 10);
            var       tasks            = range.Select(_ => new TaskCompletionSource <int>()).ToList();
            var       agent            = new AsyncAgent <int, int>(
                initialState: 0,
                messageHandler: async(state, msg, ct) =>
            {
                if (1 != Interlocked.Increment(ref parallelHandlers))
                {
                    throw new Exception("parallelHandlers should be 1");
                }

                await Task.Delay(random.Next(5));

                if (0 != Interlocked.Decrement(ref parallelHandlers))
                {
                    throw new Exception("parrallelHandlers should be 0");
                }

                tasks[msg].SetResult(msg);

                return(state);
            },
                errorHandler: (ex, ct) =>
            {
                thrownException = ex;
                return(Task.FromResult(true));
            });

            foreach (var msg in range)
            {
                agent.Send(msg);
            }
            await Task.WhenAll(tasks.Select(item => item.Task).ToArray());

            Assert.Null(thrownException);
        }
Пример #4
0
        public async Task AgentTriggersErrorHandler()
        {
            var tcs       = new TaskCompletionSource <Exception>();
            var message   = "test";
            var exception = new Exception();
            var agent     = new AsyncAgent <string, string>(
                initialState: string.Empty,
                messageHandler: async(state, msg, ct) =>
            {
                await Task.Delay(0, ct);
                throw exception;
            },
                errorHandler: (ex, ct) =>
            {
                tcs.SetResult(ex);
                return(Task.FromResult(true));
            });

            agent.Send(message);
            var triggeredException = await tcs.Task;

            Assert.Equal(exception, triggeredException);
        }
Пример #5
0
 public void Send(TMessage message)
 {
     _asyncAgent.Send(message);
 }