Skip to content

Commit

Permalink
fix start stop start (#232)
Browse files Browse the repository at this point in the history
Co-authored-by: Radu Popovici <rpopovici@totalsoft.ro>
  • Loading branch information
oncicaradupopovici and Radu Popovici authored Jul 19, 2022
1 parent 6921ec3 commit 4e186f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Messaging/NBB.Messaging.Host/Internal/MessagingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public void ScheduleRestart(TimeSpan delay = default)
ExecutionContext.SuppressFlow();

await TryStopAsync();

_stoppingSource = new CancellationTokenSource();

await StartAsync();
}
finally
Expand Down Expand Up @@ -215,6 +212,7 @@ public async Task StopAsync(CancellationToken cancellationToken = default)
finally
{
Interlocked.Exchange(ref _isStopping, 0);
_stoppingSource = new CancellationTokenSource();
}

}
Expand Down
32 changes: 32 additions & 0 deletions test/UnitTests/Messaging/NBB.Messaging.Host.Tests/HostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ public async Task Shoud_restart_even_if_stop_fails()
Mock.Get(mockLogger).VerifyLogInformationWasCalled("Messaging host is starting", Times.Exactly(2), "Messaging host expected to re-start");
}

[Fact]
public async Task Start_Stop_Start_should_work()
{
//Arrange
var hostOptions = Mock.Of<IOptions<MessagingHostOptions>>(x => x.Value == new MessagingHostOptions
{
RestartDelaySeconds = 0,
StartRetryCount = 0,
});
var configurator = new DelegateMessagingHostStartup(config =>
config.AddSubscriberServices(s => s.FromTopic("TestTopic")).WithDefaultOptions().UsePipeline(p => { }));

var mockedMessageBus = Mock.Of<IMessageBus>();
Mock.Get(mockedMessageBus)
.Setup(x => x.SubscribeAsync(It.IsAny<Func<MessagingEnvelope<object>, Task>>(),
It.IsAny<MessagingSubscriberOptions>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Mock.Of<IDisposable>());

var mockLogger = Mock.Of<ILogger<MessagingHost>>();
var mockedServiceProvider = GetMockedServiceProvider(mockedMessageBus);
var messageHost = new MessagingHost(mockLogger, new[] { configurator }, mockedServiceProvider,
Mock.Of<IServiceCollection>(), Mock.Of<IHostApplicationLifetime>(), Mock.Of<ITransportMonitor>(), hostOptions);

//Act
await messageHost.StartAsync();
await messageHost.StopAsync();
await messageHost.StartAsync();

//Assert
Mock.Get(mockLogger).VerifyLogInformationWasCalled("Messaging host has started", Times.Exactly(2), "Messaging host expected to start twice");
}


private IServiceProvider GetMockedServiceProvider(IMessageBus mockedMessageBus)
{
Expand Down

0 comments on commit 4e186f8

Please sign in to comment.