public void Dispose_EndsTheService() { // Arrange var maxDurationInMilliseconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds * 3; var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>(); var agentControlDefinition = new AgentControlDefinition { AgentIsEnabled = true, Hostaddress = "127.0.0.1", Hostname = "www.example.com", CheckIntervalInSeconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds, SystemInformationSenderPath = "/api/systeminformation" }; agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition); var agentCoordinationService = new AgentCoordinationService(agentControlDefinitionProvider.Object, () => { }, () => { }); // Act var stopwatch = new Stopwatch(); stopwatch.Start(); var agentCoordinationServiceTask = new Task(agentCoordinationService.Start); agentCoordinationServiceTask.Start(); Thread.Sleep(500); agentCoordinationService.Dispose(); Task.WaitAll(new[] { agentCoordinationServiceTask }, maxDurationInMilliseconds); stopwatch.Stop(); // Assert Assert.Less(stopwatch.ElapsedMilliseconds, maxDurationInMilliseconds); }