-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5621 from Particular/backport-startuptask-order
Start FeatureStartupTasks in feature dependency order
- Loading branch information
Showing
3 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
src/NServiceBus.AcceptanceTests/Core/Feature/When_depending_on_feature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
namespace NServiceBus.AcceptanceTests.Core.Feature | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using AcceptanceTesting; | ||
using EndpointTemplates; | ||
using Features; | ||
using NUnit.Framework; | ||
|
||
public class When_depending_on_feature : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_start_startup_tasks_in_order_of_dependency() | ||
{ | ||
var context = await Scenario.Define<Context>() | ||
.WithEndpoint<EndpointWithFeatures>(b => b.CustomConfig(c => | ||
{ | ||
c.EnableFeature<DependencyFeature>(); | ||
c.EnableFeature<TypedDependentFeature>(); | ||
})) | ||
.Done(c => c.EndpointsStarted) | ||
.Run(); | ||
|
||
Assert.That(context.StartCalled, Is.True); | ||
Assert.That(context.StopCalled, Is.True); | ||
} | ||
|
||
class Context : ScenarioContext | ||
{ | ||
public bool StartCalled { get; set; } | ||
public bool StopCalled { get; set; } | ||
public bool InitializeCalled { get; set; } | ||
} | ||
|
||
public class EndpointWithFeatures : EndpointConfigurationBuilder | ||
{ | ||
public EndpointWithFeatures() | ||
{ | ||
EndpointSetup<DefaultServer>(); | ||
} | ||
} | ||
|
||
public class TypedDependentFeature : Feature | ||
{ | ||
public TypedDependentFeature() | ||
{ | ||
DependsOn<DependencyFeature>(); | ||
} | ||
|
||
protected override void Setup(FeatureConfigurationContext context) | ||
{ | ||
context.Container.ConfigureComponent<Runner>(DependencyLifecycle.SingleInstance); | ||
context.RegisterStartupTask(b => b.Build<Runner>()); | ||
} | ||
|
||
class Runner : FeatureStartupTask | ||
{ | ||
Dependency dependency; | ||
|
||
public Runner(Dependency dependency) | ||
{ | ||
this.dependency = dependency; | ||
} | ||
protected override Task OnStart(IMessageSession session) | ||
{ | ||
dependency.Start(); | ||
return Task.FromResult(0); | ||
} | ||
|
||
protected override Task OnStop(IMessageSession session) | ||
{ | ||
dependency.Stop(); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
} | ||
|
||
public class DependencyFeature : Feature | ||
{ | ||
protected override void Setup(FeatureConfigurationContext context) | ||
{ | ||
context.Container.ConfigureComponent<Dependency>(DependencyLifecycle.SingleInstance); | ||
|
||
context.Container.ConfigureComponent<Runner>(DependencyLifecycle.SingleInstance); | ||
context.RegisterStartupTask(b => b.Build<Runner>()); | ||
} | ||
|
||
class Runner : FeatureStartupTask | ||
{ | ||
Dependency dependency; | ||
|
||
public Runner(Dependency dependency) | ||
{ | ||
this.dependency = dependency; | ||
} | ||
protected override Task OnStart(IMessageSession session) | ||
{ | ||
dependency.Initialize(); | ||
return Task.FromResult(0); | ||
} | ||
|
||
protected override Task OnStop(IMessageSession session) | ||
{ | ||
return Task.FromResult(0); | ||
} | ||
} | ||
} | ||
|
||
class Dependency | ||
{ | ||
Context context; | ||
|
||
public Dependency(Context context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
public void Start() | ||
{ | ||
if (!context.InitializeCalled) | ||
{ | ||
throw new InvalidOperationException("Not initialized"); | ||
} | ||
context.StartCalled = true; | ||
} | ||
|
||
public void Stop() | ||
{ | ||
if (!context.InitializeCalled) | ||
{ | ||
throw new InvalidOperationException("Not initialized"); | ||
} | ||
|
||
context.StopCalled = true; | ||
} | ||
|
||
public void Initialize() | ||
{ | ||
context.InitializeCalled = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters