public MigratedSubscriber() { EndpointSetup <EndpointWithNativePubSub>(c => { // Enable Migration mode c.GetSettings().Set("NServiceBus.Subscriptions.EnableMigrationMode", true); var settings = new SubscriptionMigrationModeSettings(c.GetSettings()); settings.RegisterPublisher(typeof(SomeEvent), Conventions.EndpointNamingConvention(typeof(MessageDrivenPublisher))); }); }
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); }