public static void UseStorage(this PersistenceExtensions <TestingInMemoryPersistence> extensions, TestingInMemorySubscriptionStorage storageInstance)
 {
     extensions.GetSettings().Set("InMemoryPersistence.StorageInstance", storageInstance);
 }
Ejemplo n.º 2
0
        public async Task Should_not_lose_any_events()
        {
            var subscriptionStorage = new TestingInMemorySubscriptionStorage();

            //Before migration begins
            var beforeMigration = await Scenario.Define <Context>()
                                  .WithEndpoint <Publisher>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                    c.GetSettings().Set("SqlServer.DisableNativePubSub", true);
                });
                b.When(c => c.SubscribedMessageDriven, (session, ctx) => session.Publish(new MyEvent()));
            })

                                  .WithEndpoint <Subscriber>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.GetSettings().Set("SqlServer.DisableNativePubSub", true);
                    c.GetSettings().GetOrCreate <Publishers>().AddOrReplacePublishers("LegacyConfig", new List <PublisherTableEntry>
                    {
                        new PublisherTableEntry(typeof(MyEvent), PublisherAddress.CreateFromEndpointName(PublisherEndpoint))
                    });
                });
                b.When(async(session, ctx) =>
                {
                    await session.Subscribe <MyEvent>();
                });
            })
                                  .Done(c => c.GotTheEvent)
                                  .Run(TimeSpan.FromSeconds(30));

            Assert.True(beforeMigration.GotTheEvent);

            //Publisher migrated and in compatibility mode
            var publisherMigrated = await Scenario.Define <Context>()
                                    .WithEndpoint <Publisher>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                    c.GetSettings().Set("NServiceBus.Subscriptions.EnableMigrationMode", true);
                });
                b.When(c => c.EndpointsStarted, (session, ctx) => session.Publish(new MyEvent()));
            })

                                    .WithEndpoint <Subscriber>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.GetSettings().Set("SqlServer.DisableNativePubSub", true);
                    c.GetSettings().GetOrCreate <Publishers>().AddOrReplacePublishers("LegacyConfig", new List <PublisherTableEntry>
                    {
                        new PublisherTableEntry(typeof(MyEvent), PublisherAddress.CreateFromEndpointName(PublisherEndpoint))
                    });
                });
                b.When(async(session, ctx) =>
                {
                    await session.Subscribe <MyEvent>();
                });
            })
                                    .Done(c => c.GotTheEvent)
                                    .Run(TimeSpan.FromSeconds(30));

            Assert.True(publisherMigrated.GotTheEvent);

            //Subscriber migrated and in compatibility mode
            var subscriberMigratedRunSettings = new RunSettings
            {
                TestExecutionTimeout = TimeSpan.FromSeconds(30)
            };

            subscriberMigratedRunSettings.Set("DoNotCleanNativeSubscriptions", true);
            var subscriberMigrated = await Scenario.Define <Context>()
                                     .WithEndpoint <Publisher>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                    c.GetSettings().Set("NServiceBus.Subscriptions.EnableMigrationMode", true);
                });
                b.When(c => c.SubscribedMessageDriven && c.SubscribedNative, (session, ctx) => session.Publish(new MyEvent()));
            })

                                     .WithEndpoint <Subscriber>(b =>
            {
                b.CustomConfig(c =>
                {
                    c.GetSettings().Set("NServiceBus.Subscriptions.EnableMigrationMode", true);
                    var compatModeSettings = new SubscriptionMigrationModeSettings(c.GetSettings());
                    compatModeSettings.RegisterPublisher(typeof(MyEvent), PublisherEndpoint);
                });
                b.When(async(session, ctx) =>
                {
                    //Subscribes both using native feature and message-driven
                    await session.Subscribe <MyEvent>();
                    ctx.SubscribedNative = true;
                });
            })
                                     .Done(c => c.GotTheEvent)
                                     .Run(subscriberMigratedRunSettings);

            Assert.True(subscriberMigrated.GotTheEvent);

            //Compatibility mode disabled in both publisher and subscriber
            var compatModeDisabled = await Scenario.Define <Context>()
                                     .WithEndpoint <Publisher>(b =>
            {
                b.When(c => c.EndpointsStarted, (session, ctx) => session.Publish(new MyEvent()));
            })
                                     .WithEndpoint <Subscriber>()
                                     .Done(c => c.GotTheEvent)
                                     .Run(TimeSpan.FromSeconds(30));

            Assert.True(compatModeDisabled.GotTheEvent);
        }
        public async Task Should_not_lose_any_events()
        {
            var subscriptionStorage = new TestingInMemorySubscriptionStorage();

            //Before migration begins
            var beforeMigration = await Scenario.Define <Context>()
                                  .WithEndpoint(new Publisher(supportsPublishSubscribe: false), b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                });
                b.When(c => c.SubscribedMessageDriven, (session, ctx) => session.Publish(new MyEvent()));
            })

                                  .WithEndpoint(new Subscriber(supportsPublishSubscribe: false), b =>
            {
                b.CustomConfig(c =>
                {
                    c.GetSettings().GetOrCreate <Publishers>().AddOrReplacePublishers("LegacyConfig", new List <PublisherTableEntry>
                    {
                        new PublisherTableEntry(typeof(MyEvent), PublisherAddress.CreateFromEndpointName(PublisherEndpoint))
                    });
                });
                b.When(async(session, ctx) =>
                {
                    await session.Subscribe <MyEvent>();
                });
            })
                                  .Done(c => c.GotTheEvent)
                                  .Run(TimeSpan.FromSeconds(30));

            Assert.True(beforeMigration.GotTheEvent);

            //Subscriber migrated and in compatibility mode.
            var subscriberMigratedRunSettings = new RunSettings
            {
                TestExecutionTimeout = TimeSpan.FromSeconds(30)
            };
            var subscriberMigrated = await Scenario.Define <Context>()
                                     .WithEndpoint(new Publisher(supportsPublishSubscribe: false), b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                });
                b.When(c => c.SubscribedMessageDriven, (session, ctx) => session.Publish(new MyEvent()));
            })

                                     .WithEndpoint(new Subscriber(supportsPublishSubscribe: true), b =>
            {
                b.CustomConfig(c =>
                {
                    var compatModeSettings = c.ConfigureRouting().EnableMessageDrivenPubSubCompatibilityMode();
                    compatModeSettings.RegisterPublisher(typeof(MyEvent), PublisherEndpoint);
                });
                b.When(async(session, ctx) =>
                {
                    //Subscribes both using native feature and message-driven
                    await session.Subscribe <MyEvent>();
                    ctx.SubscribedNative = true;
                });
            })
                                     .Done(c => c.GotTheEvent && c.SubscribedNative) //we ensure the subscriber did subscriber with the native mechanism
                                     .Run(subscriberMigratedRunSettings);

            Assert.True(subscriberMigrated.GotTheEvent);

            //Publisher migrated and in compatibility mode
            var publisherMigratedRunSettings = new RunSettings
            {
                TestExecutionTimeout = TimeSpan.FromSeconds(30)
            };
            var publisherMigrated = await Scenario.Define <Context>()
                                    .WithEndpoint(new Publisher(supportsPublishSubscribe: true), b =>
            {
                b.CustomConfig(c =>
                {
                    c.UsePersistence <TestingInMemoryPersistence, StorageType.Subscriptions>().UseStorage(subscriptionStorage);
                    c.ConfigureRouting().EnableMessageDrivenPubSubCompatibilityMode();
                });
                b.When(c => c.SubscribedMessageDriven && c.SubscribedNative, (session, ctx) => session.Publish(new MyEvent()));
            })

                                    .WithEndpoint(new Subscriber(supportsPublishSubscribe: true), b =>
            {
                b.CustomConfig(c =>
                {
                    var compatModeSettings = c.ConfigureRouting().EnableMessageDrivenPubSubCompatibilityMode();
                    // not needed but left here to enforce duplicates
                    compatModeSettings.RegisterPublisher(typeof(MyEvent), PublisherEndpoint);
                });
                b.When(async(session, ctx) =>
                {
                    await session.Subscribe <MyEvent>();
                    ctx.SubscribedNative = true;
                });
            })
                                    .Done(c => c.GotTheEvent)
                                    .Run(publisherMigratedRunSettings);

            Assert.True(publisherMigrated.GotTheEvent);

            //Compatibility mode disabled in both publisher and subscriber
            var compatModeDisabled = await Scenario.Define <Context>()
                                     .WithEndpoint(new Publisher(supportsPublishSubscribe: true), b =>
            {
                b.When(c => c.EndpointsStarted, (session, ctx) => session.Publish(new MyEvent()));
            })
                                     .WithEndpoint(new Subscriber(supportsPublishSubscribe: true), c => { })
                                     .Done(c => c.GotTheEvent)
                                     .Run(TimeSpan.FromSeconds(30));

            Assert.True(compatModeDisabled.GotTheEvent);
        }