public async Task GiveOneAgentsAddingTargets_WhenGetAssignments_CorrectTargetsShouldBeReturned() { // Arrange IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory); await watcherRepository.Database.Delete(_databaseName, CancellationToken.None); await watcherRepository.InitializeContainers(); IRecordContainer <TargetRecord> targetContainer = await watcherRepository.Container.Create <TargetRecord>(); targetContainer.Should().NotBeNull(); IAgentController agentController = new AgentController(_watcherOption, watcherRepository, _loggerFactory.CreateLogger <AgentController>()); // Act const int max = 10; AgentRecord agentRecord = await agentController.CreateTestAgent(1); foreach (int targetCount in Enumerable.Range(1, max)) { await targetContainer.CreateTestTarget(targetCount); await agentController.LoadBalanceAssignments(); IReadOnlyList <TargetRecord> assignments = await targetContainer.GetAssignments(agentRecord.Id, _loggerFactory.CreateLogger <TargetRecord>()); assignments.Count.Should().BeGreaterThan(targetCount); } // Clean up await watcherRepository.Database.Delete(_databaseName, CancellationToken.None); }
public async Task GiveNoAgentsAndOneTarget_WhenGetAssignments_NoTargetsReturn() { // Arrange IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory); await watcherRepository.Database.Delete(_databaseName); await watcherRepository.InitializeContainers(); IRecordContainer <TargetRecord> targetContainer = await watcherRepository.Container.Create <TargetRecord>(); targetContainer.Should().NotBeNull(); await targetContainer.CreateTestTarget(1); IAgentController agentController = new AgentController(_watcherOption, watcherRepository, _loggerFactory.CreateLogger <AgentController>()); agentController.Should().NotBeNull(); // Act await agentController.LoadBalanceAssignments(); IReadOnlyList <TargetRecord> targetRecords = await targetContainer.GetAssignments(_fakeAgentText, _loggerFactory.CreateLogger <TargetRecord>()); // Assert targetRecords.Should().NotBeNull(); targetRecords.Count.Should().Be(0); // Clean up await watcherRepository.Database.Delete(_databaseName); }
public async Task GivenAgent_WhenRegistgeredAndStateIsChange_ShouldSucceed() { // Arrange IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory); await watcherRepository.Database.Delete(_databaseName); await watcherRepository.InitializeContainers(); IRecordContainer <AgentRecord> container = await watcherRepository.Container.Create <AgentRecord>(); container.Should().NotBeNull(); IAgentController agentController = new AgentController(_watcherOption, watcherRepository, _loggerFactory.CreateLogger <AgentController>()); // Act - verify agent is not register const string agentName = "Agent.1"; // Act - register agent and verify var agentRecord = new AgentRecord { Id = agentName, State = AgentState.Running, }; await agentController.Register(agentRecord); (await container.Exist(agentName)).Should().BeTrue(); Record <AgentRecord>?record = await container.Get(agentName, token : CancellationToken.None); record.Should().NotBeNull(); (record !.Value == agentRecord).Should().BeTrue(); await agentController.SetState(agentName, AgentState.Stopped); agentRecord.State = AgentState.Stopped; record = await container.Get(agentName, token : CancellationToken.None); record.Should().NotBeNull(); (record !.Value == agentRecord).Should().BeTrue(); // Act - Un-register agent await agentController.UnRegister(agentName); (await container.Exist(agentName)).Should().BeFalse(); // Clean up await watcherRepository.Database.Delete(_databaseName); }