Exemple #1
0
        public async Task TestEnqueueReceiveEvents()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue       = new EventQueue(machineStateManager);
            int numMessages = 10000;

            var enqueueTask = Task.Run(() =>
            {
                for (int i = 0; i < numMessages; i++)
                {
                    queue.Enqueue(new E1(), Guid.Empty, null);
                }
            });

            var receiveTask = Task.Run(async() =>
            {
                for (int i = 0; i < numMessages; i++)
                {
                    await queue.ReceiveAsync(typeof(E1));
                }
            });

            await Task.WhenAny(Task.WhenAll(enqueueTask, receiveTask), Task.Delay(3000));

            Assert.True(enqueueTask.IsCompleted);
            Assert.True(receiveTask.IsCompleted);
        }
        public async Task TestReceiveEventWithPredicateWithoutWaiting()
        {
            int notificationCount = 0;
            var tcs    = new TaskCompletionSource <bool>();
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) =>
            {
                notificationCount++;
                if (notificationCount == 2)
                {
                    Assert.Equal(MockMachineStateManager.Notification.ReceiveEventWithoutWaiting, notification);
                    tcs.SetResult(true);
                }
            });

            var queue         = new EventQueue(machineStateManager);
            var enqueueStatus = queue.Enqueue(new E1(), Guid.Empty, null);

            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);
            Assert.Equal(1, queue.Size);

            var receivedEvent = await queue.ReceiveAsync(typeof(E1));

            Assert.IsType <E1>(receivedEvent);
            Assert.Equal(0, queue.Size);

            var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.Equal(DequeueStatus.NotAvailable, deqeueStatus);
            Assert.Equal(0, queue.Size);

            await Task.WhenAny(tcs.Task, Task.Delay(500));

            Assert.True(tcs.Task.IsCompleted);
        }
Exemple #3
0
        public async Task TestEnqueueDequeueEvents()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue       = new EventQueue(machineStateManager);
            int numMessages = 10000;

            var enqueueTask = Task.Run(() =>
            {
                for (int i = 0; i < numMessages; i++)
                {
                    queue.Enqueue(new E1(), Guid.Empty, null);
                }
            });

            var dequeueTask = Task.Run(() =>
            {
                for (int i = 0; i < numMessages; i++)
                {
                    var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
                    if (deqeueStatus is DequeueStatus.Success)
                    {
                        Assert.IsType <E1>(e);
                    }
                }
            });

            await Task.WhenAny(Task.WhenAll(enqueueTask, dequeueTask), Task.Delay(3000));

            Assert.True(enqueueTask.IsCompleted);
            Assert.True(dequeueTask.IsCompleted);
        }
        public async Task TestReceiveEventAfterMultipleEnqueues()
        {
            int notificationCount = 0;
            var tcs    = new TaskCompletionSource <bool>();
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) =>
            {
                notificationCount++;
                if (notificationCount == 4)
                {
                    Assert.Equal(MockMachineStateManager.Notification.ReceiveEvent, notification);
                    tcs.SetResult(true);
                }
            });

            var queue = new EventQueue(machineStateManager);

            var task = Task.Run(async() =>
            {
                var receivedEvent = await queue.ReceiveAsync(typeof(E1));
                Assert.IsType <E1>(receivedEvent);
                Assert.Equal(2, queue.Size);
            });

            // Small delay to force ordering.
            await Task.Delay(100);

            var enqueueStatus = queue.Enqueue(new E2(), Guid.Empty, null);

            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);
            Assert.Equal(1, queue.Size);

            enqueueStatus = queue.Enqueue(new E3(), Guid.Empty, null);
            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);
            Assert.Equal(2, queue.Size);

            enqueueStatus = queue.Enqueue(new E1(), Guid.Empty, null);
            Assert.Equal(EnqueueStatus.Received, enqueueStatus);
            Assert.Equal(2, queue.Size);

            await task;

            var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E2>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(1, queue.Size);

            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E3>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(0, queue.Size);

            await Task.WhenAny(tcs.Task, Task.Delay(500));

            Assert.True(tcs.Task.IsCompleted);
        }
        public void TestEnqueueEventWithHandlerNotRunning()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue = new EventQueue(machineStateManager);

            var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.Equal(DequeueStatus.NotAvailable, deqeueStatus);
            Assert.Equal(0, queue.Size);

            var enqueueStatus = queue.Enqueue(new E1(), Guid.Empty, null);

            Assert.Equal(EnqueueStatus.EventHandlerNotRunning, enqueueStatus);
            Assert.Equal(1, queue.Size);
        }
        public void TestRaiseEvent()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue = new EventQueue(machineStateManager);

            queue.Raise(new E1(), Guid.Empty);
            Assert.True(queue.IsEventRaised);
            Assert.Equal(0, queue.Size);

            var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E1>(e);
            Assert.Equal(DequeueStatus.Raised, deqeueStatus);
            Assert.False(queue.IsEventRaised);
            Assert.Equal(0, queue.Size);
        }
        public void TestDequeueEvent()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue = new EventQueue(machineStateManager);

            var(deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.Equal(DequeueStatus.NotAvailable, deqeueStatus);
            Assert.Equal(0, queue.Size);

            queue.Enqueue(new E1(), Guid.Empty, null);
            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E1>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(0, queue.Size);

            queue.Enqueue(new E3(), Guid.Empty, null);
            queue.Enqueue(new E2(), Guid.Empty, null);
            queue.Enqueue(new E1(), Guid.Empty, null);

            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E3>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(2, queue.Size);

            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E2>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(1, queue.Size);

            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.IsType <E1>(e);
            Assert.Equal(DequeueStatus.Success, deqeueStatus);
            Assert.Equal(0, queue.Size);

            (deqeueStatus, e, opGroupId, info) = queue.Dequeue();
            Assert.Equal(DequeueStatus.NotAvailable, deqeueStatus);
            Assert.Equal(0, queue.Size);
        }
        public void TestEnqueueEvent()
        {
            var logger = new TestOutputLogger(this.TestOutput, false);
            var machineStateManager = new MockMachineStateManager(logger,
                                                                  (notification, evt, _) => { });

            var queue = new EventQueue(machineStateManager);

            Assert.Equal(0, queue.Size);

            var enqueueStatus = queue.Enqueue(new E1(), Guid.Empty, null);

            Assert.Equal(1, queue.Size);
            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);

            enqueueStatus = queue.Enqueue(new E2(), Guid.Empty, null);
            Assert.Equal(2, queue.Size);
            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);

            enqueueStatus = queue.Enqueue(new E3(), Guid.Empty, null);
            Assert.Equal(3, queue.Size);
            Assert.Equal(EnqueueStatus.EventHandlerRunning, enqueueStatus);
        }