Esempio n. 1
0
        public async Task Test_When()
        {
            var hub = new MetaPubSub();

            var t = Task.Run(async() =>
            {
                await Task.Delay(50);
                await hub.Publish(new MyEvent());
            });

            var res = await hub.When <MyEvent>(100);

            Assert.IsNotNull(res);

            bool timeoutException = false;

            try
            {
                res = await hub.When <MyEvent>(100);
            }
            catch (TimeoutException)
            {
                timeoutException = true;
            }
            Assert.IsTrue(timeoutException);
        }
Esempio n. 2
0
        public async Task Test_Cancel_When()
        {
            var hub = new MetaPubSub();
            var cts = new CancellationTokenSource();

            // publish an event after 100 ms
            var t = Task.Run(async() =>
            {
                await Task.Delay(100);
                await hub.Publish(new MyEvent());
            });

            // cancel waiting after 50 ms
            var t2 = Task.Run(async() =>
            {
                await Task.Delay(50);
                cts.Cancel();
            });


            bool exception = false;

            try
            {
                var res = await hub.When <MyEvent>(200, null, cts.Token);

                Assert.IsTrue(false);
            }
            catch (OperationCanceledException)
            {
                exception = true;
            }
            Assert.IsTrue(exception);
        }
Esempio n. 3
0
        // cancellation token support - you can cancel scheduling or waiting for the message
        public static async Task CancellationExample()
        {
            var hub = new MetaPubSub();
            var cts = new CancellationTokenSource();

            // publish an event after 100 ms
            var t = Task.Run(async() =>
            {
                await Task.Delay(100);
                await hub.Publish(new MyEvent());
            });

            // cancel waiting after 50 ms
            var t2 = Task.Run(async() =>
            {
                await Task.Delay(50);
                cts.Cancel();
            });


            try
            {
                var res = await hub.When <MyEvent>(millisecondsTimeout : 200, match : null, cts.Token);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Waiting for MyEvent has been canceled");
            }
        }
Esempio n. 4
0
        // asynchronous waiting for a specified message by a single method call, without need to Subscribe/Unsubscribe to this message
        public static async Task WhenExample()
        {
            var hub = new MetaPubSub();

            // publishing a MyEvent with 500 ms delay
            var t = Task.Run(async() =>
            {
                await Task.Delay(500);
                await hub.Publish(new MyEvent());
            });

            try
            {
                // This method will wait for MyEvent one second.
                // If the event will not arrive in a specified timeout the TimeoutException will be thrown.
                MyEvent res = await hub.When <MyEvent>(millisecondsTimeout : 1000);

                Console.WriteLine($"Received MyEvent at {DateTime.Now:HH:mm:ss.fff}");
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine($"Exception {ex.GetType()}: {ex.Message}");
            }
        }