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);
        }
Example #2
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);
    }
Example #3
0
        public async Task Complex_Event_Test()
        {
            var personName = Guid.NewGuid().ToString("N");

            var creatingEventTriggered = false;
            var createdEventTriggered  = false;
            var createdEtoTriggered    = false;

            using (var uow = GetRequiredService <IUnitOfWorkManager>().Begin())
            {
#pragma warning disable 618
                LocalEventBus.Subscribe <EntityCreatingEventData <Person> >(data =>
#pragma warning restore 618
                {
                    creatingEventTriggered.ShouldBeFalse();
                    createdEventTriggered.ShouldBeFalse();

                    creatingEventTriggered = true;

                    data.Entity.Name.ShouldBe(personName);

                    /* Want to change age from 15 to 18 */
                    data.Entity.Age.ShouldBe(15);
                    data.Entity.Age = 18;
                    return(Task.CompletedTask);
                });

                LocalEventBus.Subscribe <EntityCreatedEventData <Person> >(data =>
                {
                    creatingEventTriggered.ShouldBeTrue();
                    createdEventTriggered.ShouldBeFalse();

                    createdEventTriggered = true;

                    data.Entity.Age.ShouldBe(18);
                    data.Entity.Name.ShouldBe(personName);

                    return(Task.CompletedTask);
                });

                DistributedEventBus.Subscribe <EntityCreatedEto <PersonEto> >(eto =>
                {
                    eto.Entity.Name.ShouldBe(personName);

                    createdEtoTriggered = true;

                    return(Task.CompletedTask);
                });

                await PersonRepository.InsertAsync(new Person(Guid.NewGuid(), personName, 15));

                await uow.CompleteAsync();
            }

            creatingEventTriggered.ShouldBeTrue();
            createdEventTriggered.ShouldBeTrue();
            createdEtoTriggered.ShouldBeTrue();
        }
    public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant()
    {
        var tenantId = Guid.NewGuid();
        var handler  = new MyEventHandler(GetRequiredService <ICurrentTenant>());

        LocalEventBus.Subscribe <EntityChangedEventData <MyEntity> >(handler);

        await LocalEventBus.PublishAsync(new EntityCreatedEventData <MyEntity>(new MyEntity(tenantId)));

        handler.TenantId.ShouldBe(tenantId);
    }
Example #5
0
    public async Task Should_Call_Created_And_Changed_Once()
    {
        var handler = new MyEventHandler();

        LocalEventBus.Subscribe <EntityChangedEventData <MyEntity> >(handler);
        LocalEventBus.Subscribe <EntityCreatedEventData <MyEntity> >(handler);

        await LocalEventBus.PublishAsync(new EntityCreatedEventData <MyEntity>(new MyEntity()));

        handler.EntityCreatedEventCount.ShouldBe(1);
        handler.EntityChangedEventCount.ShouldBe(1);
    }
Example #6
0
    public async Task Should_Call_Handler_AndDispose()
    {
        LocalEventBus.Subscribe <MySimpleEventData, MySimpleTransientEventHandler>();

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

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

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

        Assert.Equal(3, MySimpleTransientEventHandler.HandleCount);
        Assert.Equal(3, MySimpleTransientEventHandler.DisposeCount);
    }
Example #7
0
    public async Task Should_Trigger_For_Inherited_Generic_2()
    {
        var triggeredEvent = false;

        LocalEventBus.Subscribe <EntityChangedEventData <Person> >(
            eventData =>
        {
            eventData.Entity.Id.ShouldBe(42);
            triggeredEvent = true;
            return(Task.CompletedTask);
        });

        await LocalEventBus.PublishAsync(new EntityChangedEventData <Student>(new Student(42)));

        triggeredEvent.ShouldBe(true);
    }
Example #8
0
    public async Task Should_Handle_Events_For_Derived_Classes()
    {
        var totalData = 0;

        LocalEventBus.Subscribe <MySimpleEventData>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

        await LocalEventBus.PublishAsync(new MySimpleEventData(1));  //Should handle directly registered class

        await LocalEventBus.PublishAsync(new MySimpleEventData(2));  //Should handle directly registered class

        await LocalEventBus.PublishAsync(new MyDerivedEventData(3)); //Should handle derived class too

        await LocalEventBus.PublishAsync(new MyDerivedEventData(4)); //Should handle derived class too

        Assert.Equal(10, totalData);
    }
Example #9
0
    public async Task Should_Not_Handle_Events_For_Base_Classes()
    {
        var totalData = 0;

        LocalEventBus.Subscribe <MyDerivedEventData>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

        await LocalEventBus.PublishAsync(new MySimpleEventData(1));  //Should not handle

        await LocalEventBus.PublishAsync(new MySimpleEventData(2));  //Should not handle

        await LocalEventBus.PublishAsync(new MyDerivedEventData(3)); //Should handle

        await LocalEventBus.PublishAsync(new MyDerivedEventData(4)); //Should handle

        Assert.Equal(7, totalData);
    }
Example #10
0
    public async Task Should_Call_Handler_With_Non_Generic_Trigger()
    {
        var totalData = 0;

        LocalEventBus.Subscribe <MySimpleEventData>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

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

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

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

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

        Assert.Equal(10, totalData);
    }
Example #11
0
    public async Task Should_Call_Action_On_Event_With_Correct_Source()
    {
        var totalData = 0;

        LocalEventBus.Subscribe <MySimpleEventData>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

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

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

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

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

        Assert.Equal(10, totalData);
    }
        public void ParallelPubAndSub()
        {
            int count = 0;
            var mq    = new LocalEventBus(CreateLogger <LocalEventBus>());

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

            Parallel.For(0, 100, async(i) => { await mq.PublishAsync("topic", "a"); });
            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
Example #13
0
    public async Task Should_Call_Action_On_Event_With_Correct_Source_Async()
    {
        int totalData = 0;

        LocalEventBus.Subscribe <MySimpleEventData>(
            async eventData =>
        {
            await Task.Delay(20);
            Interlocked.Add(ref totalData, eventData.Value);
            await Task.Delay(20);
        });

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

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

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

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

        Assert.Equal(10, totalData);
    }
Example #14
0
    public async Task Should_Not_Call_Action_After_Unregister_1()
    {
        var totalData = 0;

        var registerDisposer = LocalEventBus.Subscribe <MySimpleEventData>(
            eventData =>
        {
            totalData += eventData.Value;
            return(Task.CompletedTask);
        });

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

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

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

        registerDisposer.Dispose();

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

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

            mq.Subscribe("topic", msg =>
            {
                Interlocked.Increment(ref count);
            });
            for (int i = 0; i < 100; ++i)
            {
                await mq.PublishAsync("topic", "a");
            }

            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
        public void Complex_Event_Test()
        {
            var personName = Guid.NewGuid().ToString("N");

            var creatingEventTriggered = false;
            var createdEventTriggered  = false;
            var createdEtoTriggered    = false;
            var updatedEtoTriggered    = false;
            var updatingEventTriggered = false;
            var updatedEventTriggered  = false;

            using (var uow = GetRequiredService <IUnitOfWorkManager>().Begin())
            {
                LocalEventBus.Subscribe <EntityCreatingEventData <Person> >(data =>
                {
                    creatingEventTriggered.ShouldBeFalse();
                    createdEventTriggered.ShouldBeFalse();
                    updatingEventTriggered.ShouldBeFalse();
                    updatedEventTriggered.ShouldBeFalse();

                    creatingEventTriggered = true;

                    data.Entity.Name.ShouldBe(personName);

                    /* Want to change age from 15 to 18
                     * Expect to trigger EntityUpdatingEventData, EntityUpdatedEventData events */
                    data.Entity.Age.ShouldBe(15);
                    data.Entity.Age = 18;
                    PersonRepository.Update(data.Entity);
                    return(Task.CompletedTask);
                });

                LocalEventBus.Subscribe <EntityCreatedEventData <Person> >(data =>
                {
                    creatingEventTriggered.ShouldBeTrue();
                    createdEventTriggered.ShouldBeFalse();
                    updatingEventTriggered.ShouldBeTrue();
                    updatedEventTriggered.ShouldBeFalse();

                    createdEventTriggered = true;

                    data.Entity.Age.ShouldBe(18);
                    data.Entity.Name.ShouldBe(personName);

                    return(Task.CompletedTask);
                });

                DistributedEventBus.Subscribe <EntityCreatedEto <PersonEto> >(eto =>
                {
                    eto.Entity.Name.ShouldBe(personName);

                    createdEtoTriggered = true;

                    return(Task.CompletedTask);
                });

                LocalEventBus.Subscribe <EntityUpdatingEventData <Person> >(data =>
                {
                    creatingEventTriggered.ShouldBeTrue();
                    createdEventTriggered.ShouldBeFalse();
                    updatingEventTriggered.ShouldBeFalse();
                    updatedEventTriggered.ShouldBeFalse();

                    updatingEventTriggered = true;

                    data.Entity.Name.ShouldBe(personName);
                    data.Entity.Age.ShouldBe(18);

                    return(Task.CompletedTask);
                });

                LocalEventBus.Subscribe <EntityUpdatedEventData <Person> >(data =>
                {
                    creatingEventTriggered.ShouldBeTrue();
                    createdEventTriggered.ShouldBeTrue();
                    updatingEventTriggered.ShouldBeTrue();
                    updatedEventTriggered.ShouldBeFalse();

                    updatedEventTriggered = true;

                    data.Entity.Name.ShouldBe(personName);
                    data.Entity.Age.ShouldBe(18);

                    return(Task.CompletedTask);
                });

                DistributedEventBus.Subscribe <EntityUpdatedEto <PersonEto> >(eto =>
                {
                    eto.Entity.Name.ShouldBe(personName);
                    eto.Entity.Age.ShouldBe(18);

                    updatedEtoTriggered = true;

                    return(Task.CompletedTask);
                });

                PersonRepository.Insert(new Person(Guid.NewGuid(), personName, 15));

                uow.Complete();
            }

            creatingEventTriggered.ShouldBeTrue();
            createdEventTriggered.ShouldBeTrue();
            createdEtoTriggered.ShouldBeTrue();
            updatingEventTriggered.ShouldBeTrue();
            updatedEventTriggered.ShouldBeTrue();
            updatedEtoTriggered.ShouldBeTrue();
        }