Esempio n. 1
0
        public async void DispatchesJobRequest()
        {
            //Arrange
            using (var hc = new TestHostContext(this))
                using (var jobDispatcher = new JobDispatcher())
                {
                    hc.SetSingleton <IConfigurationStore>(_configurationStore.Object);
                    hc.SetSingleton <IAgentServer>(_agentServer.Object);

                    hc.EnqueueInstance <IProcessChannel>(_processChannel.Object);
                    hc.EnqueueInstance <IProcessInvoker>(_processInvoker.Object);
                    jobDispatcher.Initialize(hc);
                    var ts = new CancellationTokenSource();
                    JobRequestMessage message    = CreateJobRequestMessage();
                    string            strMessage = JsonUtility.ToString(message);

                    _processInvoker.Setup(x => x.ExecuteAsync(It.IsAny <String>(), It.IsAny <String>(), "spawnclient 1 2", null, It.IsAny <CancellationToken>()))
                    .Returns(Task.FromResult <int>(56));

                    _processChannel.Setup(x => x.StartServer(It.IsAny <StartProcessDelegate>()))
                    .Callback((StartProcessDelegate startDel) => { startDel("1", "2"); });
                    _processChannel.Setup(x => x.SendAsync(MessageType.NewJobRequest, It.Is <string>(s => s.Equals(strMessage)), It.IsAny <CancellationToken>()))
                    .Returns(Task.CompletedTask);

                    _configurationStore.Setup(x => x.GetSettings()).Returns(new AgentSettings()
                    {
                        PoolId = 1
                    });

                    var          request           = new TaskAgentJobRequest();
                    PropertyInfo sessionIdProperty = request.GetType().GetProperty("LockedUntil", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    Assert.NotNull(sessionIdProperty);
                    sessionIdProperty.SetValue(request, DateTime.UtcNow.AddMinutes(5));

                    _agentServer.Setup(x => x.RenewAgentRequestAsync(It.IsAny <int>(), It.IsAny <long>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <TaskAgentJobRequest>(request));

                    _agentServer.Setup(x => x.FinishAgentRequestAsync(It.IsAny <int>(), It.IsAny <long>(), It.IsAny <Guid>(), It.IsAny <DateTime>(), It.IsAny <TaskResult>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <TaskAgentJobRequest>(new TaskAgentJobRequest()));


                    //Actt
                    int exitCode = await jobDispatcher.RunAsync(message, ts.Token);

                    //Assert
                    Assert.Equal(exitCode, 0);
                }
        }