Example #1
0
        public async void DispatchRunNewJob()
        {
            //Arrange
            using (var hc = new TestHostContext(this))
                using (var tokenSource = new CancellationTokenSource())
                {
                    var worker = new Microsoft.VisualStudio.Services.Agent.Worker.Worker();
                    hc.EnqueueInstance <IProcessChannel>(_processChannel.Object);
                    hc.EnqueueInstance <IJobRunner>(_jobRunner.Object);
                    hc.SetSingleton <IVstsAgentWebProxy>(_proxy.Object);
                    hc.SetSingleton <IAgentCertificateManager>(_cert.Object);
                    worker.Initialize(hc);
                    var jobMessage       = CreateJobRequestMessage("job1");
                    var arWorkerMessages = new WorkerMessage[]
                    {
                        new WorkerMessage
                        {
                            Body        = JsonUtility.ToString(jobMessage),
                            MessageType = MessageType.NewJobRequest
                        }
                    };
                    var workerMessages = new Queue <WorkerMessage>(arWorkerMessages);

                    _processChannel
                    .Setup(x => x.ReceiveAsync(It.IsAny <CancellationToken>()))
                    .Returns(async() =>
                    {
                        // Return the job message.
                        if (workerMessages.Count > 0)
                        {
                            return(workerMessages.Dequeue());
                        }

                        // Wait for the text to run
                        await Task.Delay(-1, tokenSource.Token);
                        return(default(WorkerMessage));
                    });
                    _jobRunner.Setup(x => x.RunAsync(It.IsAny <Pipelines.AgentJobRequestMessage>(), It.IsAny <CancellationToken>()))
                    .Returns(Task.FromResult <TaskResult>(TaskResult.Succeeded));

                    //Act
                    await worker.RunAsync(pipeIn : "1", pipeOut : "2");

                    //Assert
                    _processChannel.Verify(x => x.StartClient("1", "2"), Times.Once());
                    _jobRunner.Verify(x => x.RunAsync(
                                          It.Is <Pipelines.AgentJobRequestMessage>(y => IsMessageIdentical(y, jobMessage)), It.IsAny <CancellationToken>()));
                    tokenSource.Cancel();
                }
        }
Example #2
0
        public async void DispatchRunNewJob()
        {
            //Arrange
            using (var hc = new TestHostContext(this))
            using (var tokenSource = new CancellationTokenSource())
            {
                var worker = new Microsoft.VisualStudio.Services.Agent.Worker.Worker();
                hc.EnqueueInstance<IProcessChannel>(_processChannel.Object);
                hc.SetSingleton<IJobRunner>(_jobRunner.Object);
                worker.Initialize(hc);
                var jobMessage = CreateJobRequestMessage("job1");
                var arWorkerMessages = new WorkerMessage[]
                    {
                        new WorkerMessage
                        {
                            Body = JsonUtility.ToString(jobMessage),
                            MessageType = MessageType.NewJobRequest
                        }
                    };
                var workerMessages = new Queue<WorkerMessage>(arWorkerMessages);

                _processChannel
                    .Setup(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
                    .Returns(async () =>
                    {
                        // Return the job message.
                        if (workerMessages.Count > 0)
                        {
                            return workerMessages.Dequeue();
                        }

                        // Wait for the text to run
                        await Task.Delay(-1, tokenSource.Token);
                        return default(WorkerMessage);
                    });
                _jobRunner.Setup(x => x.RunAsync(It.IsAny<JobRequestMessage>(), It.IsAny<CancellationToken>()))
                    .Returns(Task.FromResult<TaskResult>(TaskResult.Succeeded));

                //Act
                await worker.RunAsync(pipeIn: "1", pipeOut: "2");

                //Assert
                _processChannel.Verify(x => x.StartClient("1", "2"), Times.Once());
                _jobRunner.Verify(x => x.RunAsync(
                    It.Is<JobRequestMessage>(y => JsonUtility.ToString(y) == arWorkerMessages[0].Body), It.IsAny<CancellationToken>()));
                tokenSource.Cancel();
            }
        }
Example #3
0
        public async void DispatchCancellation()
        {
            //Arrange
            using (var hc = new TestHostContext(this))
            {
                var worker = new Microsoft.VisualStudio.Services.Agent.Worker.Worker();
                hc.EnqueueInstance <IProcessChannel>(_processChannel.Object);
                hc.EnqueueInstance <IJobRunner>(_jobRunner.Object);
                hc.SetSingleton <IVstsAgentWebProxy>(_proxy.Object);
                hc.SetSingleton <IAgentCertificateManager>(_cert.Object);
                worker.Initialize(hc);
                var jobMessage       = CreateJobRequestMessage("job1");
                var cancelMessage    = CreateJobCancelMessage(jobMessage.JobId);
                var arWorkerMessages = new WorkerMessage[]
                {
                    new WorkerMessage
                    {
                        Body        = JsonUtility.ToString(jobMessage),
                        MessageType = MessageType.NewJobRequest
                    },
                    new WorkerMessage
                    {
                        Body        = JsonUtility.ToString(cancelMessage),
                        MessageType = MessageType.CancelRequest
                    }
                };
                var workerMessages = new Queue <WorkerMessage>(arWorkerMessages);

                _processChannel.Setup(x => x.ReceiveAsync(It.IsAny <CancellationToken>()))
                .Returns(() => Task.FromResult(workerMessages.Dequeue()));
                _jobRunner.Setup(x => x.RunAsync(It.IsAny <Pipelines.AgentJobRequestMessage>(), It.IsAny <CancellationToken>()))
                .Returns(
                    async(Pipelines.AgentJobRequestMessage jm, CancellationToken ct) =>
                {
                    await Task.Delay(-1, ct);
                    return(TaskResult.Canceled);
                });

                //Act
                await Assert.ThrowsAsync <TaskCanceledException>(
                    async() => await worker.RunAsync("1", "2"));

                //Assert
                _processChannel.Verify(x => x.StartClient("1", "2"), Times.Once());
                _jobRunner.Verify(x => x.RunAsync(
                                      It.Is <Pipelines.AgentJobRequestMessage>(y => IsMessageIdentical(y, jobMessage)), It.IsAny <CancellationToken>()));
            }
        }
Example #4
0
        public async void DispatchCancellation()
        {
            //Arrange
            using (var hc = new TestHostContext(this))
            {
                var worker = new Microsoft.VisualStudio.Services.Agent.Worker.Worker();
                hc.EnqueueInstance<IProcessChannel>(_processChannel.Object);
                hc.SetSingleton<IJobRunner>(_jobRunner.Object);
                worker.Initialize(hc);
                var jobMessage = CreateJobRequestMessage("job1");
                var cancelMessage = CreateJobCancelMessage(jobMessage.JobId);
                var arWorkerMessages = new WorkerMessage[]
                    {
                        new WorkerMessage
                        {
                            Body = JsonUtility.ToString(jobMessage),
                            MessageType = MessageType.NewJobRequest
                        },
                        new WorkerMessage
                        {
                            Body = JsonUtility.ToString(cancelMessage),
                            MessageType = MessageType.CancelRequest
                        }

                    };
                var workerMessages = new Queue<WorkerMessage>(arWorkerMessages);

                _processChannel.Setup(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
                    .Returns(() => Task.FromResult(workerMessages.Dequeue()));
                _jobRunner.Setup(x => x.RunAsync(It.IsAny<JobRequestMessage>(), It.IsAny<CancellationToken>()))
                    .Returns(
                    async (JobRequestMessage jm, CancellationToken ct) =>
                    {
                        await Task.Delay(-1, ct);
                        return TaskResult.Canceled;
                    });

                //Act
                await Assert.ThrowsAsync<TaskCanceledException>(
                    async () => await worker.RunAsync("1", "2"));

                //Assert
                _processChannel.Verify(x => x.StartClient("1", "2"), Times.Once());
                _jobRunner.Verify(x => x.RunAsync(
                    It.Is<JobRequestMessage>(y => JsonUtility.ToString(y) == arWorkerMessages[0].Body), It.IsAny<CancellationToken>()));
            }
        }