Ejemplo n.º 1
0
        public async void CreatesSession()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var expectedSession = new TaskAgentSession();
                    _runnerServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials());

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    trace.Info("result: {0}", result);

                    // Assert.
                    Assert.True(result);
                    _runnerServer
                    .Verify(x => x.CreateAgentSessionAsync(
                                _settings.PoolId,
                                It.Is <TaskAgentSession>(y => y != null),
                                tokenSource.Token), Times.Once());
                }
        }
        public async void DeleteSession()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var          expectedSession   = new TaskAgentSession();
                    PropertyInfo sessionIdProperty = expectedSession.GetType().GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    Assert.NotNull(sessionIdProperty);
                    sessionIdProperty.SetValue(expectedSession, Guid.NewGuid());

                    _runnerServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials());
                    _store.Setup(x => x.GetCredentials()).Returns(new CredentialData()
                    {
                        Scheme = Constants.Configuration.OAuthAccessToken
                    });
                    _store.Setup(x => x.GetMigratedCredentials()).Returns(default(CredentialData));

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    Assert.True(result);

                    _runnerServer
                    .Setup(x => x.DeleteAgentSessionAsync(
                               _settings.PoolId, expectedSession.SessionId, It.IsAny <CancellationToken>()))
                    .Returns(Task.CompletedTask);
                    await listener.DeleteSessionAsync();

                    //Assert
                    _runnerServer
                    .Verify(x => x.DeleteAgentSessionAsync(
                                _settings.PoolId, expectedSession.SessionId, It.IsAny <CancellationToken>()), Times.Once());
                }
        }
Ejemplo n.º 3
0
        public async void DeleteSession()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var          expectedSession   = new TaskAgentSession();
                    PropertyInfo sessionIdProperty = expectedSession.GetType().GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    Assert.NotNull(sessionIdProperty);
                    sessionIdProperty.SetValue(expectedSession, Guid.NewGuid());

                    _agentServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _capabilitiesManager.Setup(x => x.GetCapabilitiesAsync(_settings, It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Dictionary <string, string>()));

                    _credMgr.Setup(x => x.LoadCredentials()).Returns(new Common.VssCredentials());

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    Assert.True(result);
                    Assert.Equal(expectedSession, listener.Session);

                    _agentServer
                    .Setup(x => x.DeleteAgentSessionAsync(
                               _settings.PoolId, expectedSession.SessionId, It.IsAny <CancellationToken>()))
                    .Returns(Task.CompletedTask);
                    await listener.DeleteSessionAsync();

                    //Assert
                    _agentServer
                    .Verify(x => x.DeleteAgentSessionAsync(
                                _settings.PoolId, expectedSession.SessionId, It.IsAny <CancellationToken>()), Times.Once());
                }
        }
        public async void CreateSessionWithOriginalCredential()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var expectedSession = new TaskAgentSession();
                    _runnerServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials());

                    var originalCred = new CredentialData()
                    {
                        Scheme = Constants.Configuration.OAuth
                    };
                    originalCred.Data["authorizationUrl"] = "https://s.server";
                    originalCred.Data["clientId"]         = "d842fd7b-61b0-4a80-96b4-f2797c353897";

                    _store.Setup(x => x.GetCredentials()).Returns(originalCred);
                    _store.Setup(x => x.GetMigratedCredentials()).Returns(default(CredentialData));

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    trace.Info("result: {0}", result);

                    // Assert.
                    Assert.True(result);
                    _runnerServer
                    .Verify(x => x.CreateAgentSessionAsync(
                                _settings.PoolId,
                                It.Is <TaskAgentSession>(y => y != null),
                                tokenSource.Token), Times.Once());
                }
        }
Ejemplo n.º 5
0
        public async void CreatesSession()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var expectedSession = new TaskAgentSession();
                    _agentServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _capProvider.Setup(x => x.GetCapabilitiesAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Dictionary <string, string>()));

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    trace.Info("result: {0}", result);

                    // Assert.
                    Assert.True(result);
                    Assert.Equal(expectedSession, listener.Session);
                    _agentServer
                    .Verify(x => x.CreateAgentSessionAsync(
                                _settings.PoolId,
                                It.Is <TaskAgentSession>(y => y != null),
                                tokenSource.Token), Times.Once());
                    _capProvider.Verify(x => x.GetCapabilitiesAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()));
                }
        }
Ejemplo n.º 6
0
        public async void GetNextMessage()
        {
            using (TestHostContext tc = CreateTestContext())
                using (var tokenSource = new CancellationTokenSource())
                {
                    Tracing trace = tc.GetTrace();

                    // Arrange.
                    var          expectedSession   = new TaskAgentSession();
                    PropertyInfo sessionIdProperty = expectedSession.GetType().GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    Assert.NotNull(sessionIdProperty);
                    sessionIdProperty.SetValue(expectedSession, Guid.NewGuid());

                    _agentServer
                    .Setup(x => x.CreateAgentSessionAsync(
                               _settings.PoolId,
                               It.Is <TaskAgentSession>(y => y != null),
                               tokenSource.Token))
                    .Returns(Task.FromResult(expectedSession));

                    _capabilitiesManager.Setup(x => x.GetCapabilitiesAsync(_settings, It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Dictionary <string, string>()));

                    _credMgr.Setup(x => x.LoadCredentials()).Returns(new Common.VssCredentials());

                    // Act.
                    MessageListener listener = new MessageListener();
                    listener.Initialize(tc);

                    bool result = await listener.CreateSessionAsync(tokenSource.Token);

                    Assert.True(result);
                    Assert.Equal(expectedSession, listener.Session);

                    var arMessages = new TaskAgentMessage[]
                    {
                        new TaskAgentMessage
                        {
                            Body        = "somebody1",
                            MessageId   = 4234,
                            MessageType = JobRequestMessageTypes.AgentJobRequest
                        },
                        new TaskAgentMessage
                        {
                            Body        = "somebody2",
                            MessageId   = 4235,
                            MessageType = JobCancelMessage.MessageType
                        },
                        null,  //should be skipped by GetNextMessageAsync implementation
                        null,
                        new TaskAgentMessage
                        {
                            Body        = "somebody3",
                            MessageId   = 4236,
                            MessageType = JobRequestMessageTypes.AgentJobRequest
                        }
                    };
                    var messages = new Queue <TaskAgentMessage>(arMessages);

                    _agentServer
                    .Setup(x => x.GetAgentMessageAsync(
                               _settings.PoolId, expectedSession.SessionId, It.IsAny <long?>(), tokenSource.Token))
                    .Returns(async(Int32 poolId, Guid sessionId, Int64? lastMessageId, CancellationToken cancellationToken) =>
                    {
                        await Task.Yield();
                        return(messages.Dequeue());
                    });
                    TaskAgentMessage message1 = await listener.GetNextMessageAsync(tokenSource.Token);

                    TaskAgentMessage message2 = await listener.GetNextMessageAsync(tokenSource.Token);

                    TaskAgentMessage message3 = await listener.GetNextMessageAsync(tokenSource.Token);

                    Assert.Equal(arMessages[0], message1);
                    Assert.Equal(arMessages[1], message2);
                    Assert.Equal(arMessages[4], message3);

                    //Assert
                    _agentServer
                    .Verify(x => x.GetAgentMessageAsync(
                                _settings.PoolId, expectedSession.SessionId, It.IsAny <long?>(), tokenSource.Token), Times.Exactly(arMessages.Length));
                }
        }