Example #1
0
    public async Task Should_Not_Call_Action_After_Unregister_2()
    {
        var totalData = 0;

        var action = new Func <MySimpleEventData, Task>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

        LocalEventBus.Subscribe(action);

        await LocalEventBus.PublishAsync(new MySimpleEventData(1));

        await LocalEventBus.PublishAsync(new MySimpleEventData(2));

        await LocalEventBus.PublishAsync(new MySimpleEventData(3));

        LocalEventBus.Unsubscribe(action);

        await LocalEventBus.PublishAsync(new MySimpleEventData(4));

        Assert.Equal(6, totalData);
    }
        public async Task PubAndUnSub()
        {
            int count = 0;
            var mq    = new LocalEventBus(CreateLogger <LocalEventBus>());

            mq.Subscribe("topic", msg =>
            {
                Interlocked.Increment(ref count);
            });

            int i = 0;

            Task.Factory.StartNew(async() =>
            {
                for (; i < 50; ++i)
                {
                    await mq.PublishAsync("topic", "a");
                    await Task.Delay(100);
                }
            }).ConfigureAwait(false).GetAwaiter();
            await Task.Delay(1500);

            mq.Unsubscribe("topic");

            while (i < 50)
            {
                await Task.Delay(100);
            }

            Assert.True(count < 100);
        }