Exemplo n.º 1
0
        public async Task AddUserAsyncAsync_Throws_SoulseekClientException_On_Throw(string username, bool exists, UserData userData)
        {
            var result = new AddUserResponse(username, exists, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Throws(new ConnectionException("foo"));

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.AddUserAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <SoulseekClientException>(ex);
                Assert.IsType <ConnectionException>(ex.InnerException);
            }
        }
Exemplo n.º 2
0
        public async Task GetUserStatusAsync_Throws_UserStatusException_On_Throw(string username, bool exists, UserStatus status, int averageSpeed, int downloadCount, int fileCount, int directoryCount, string countryCode)
        {
            var result = new AddUserResponse(username, exists, status, averageSpeed, downloadCount, fileCount, directoryCount, countryCode);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Throws(new ConnectionException("foo"));

            var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: serverConn.Object);

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            AddUserResponse r  = null;
            var             ex = await Record.ExceptionAsync(async() => r = await s.AddUserAsync(username));

            Assert.NotNull(ex);
            Assert.IsType <AddUserException>(ex);
            Assert.IsType <ConnectionException>(ex.InnerException);
        }
Exemplo n.º 3
0
        public async Task AddUserAsync_Returns_Expected_Info(string username, bool exists, UserStatus status, int averageSpeed, int downloadCount, int fileCount, int directoryCount, string countryCode)
        {
            var result = new AddUserResponse(username, exists, status, averageSpeed, downloadCount, fileCount, directoryCount, countryCode);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <byte[]>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var s = new SoulseekClient("127.0.0.1", 1, waiter: waiter.Object, serverConnection: serverConn.Object);

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            var add = await s.AddUserAsync(username);

            Assert.Equal(result.Username, add.Username);
            Assert.Equal(result.Exists, add.Exists);
            Assert.Equal(result.Status, add.Status);
            Assert.Equal(result.AverageSpeed, add.AverageSpeed);
            Assert.Equal(result.DownloadCount, add.DownloadCount);
            Assert.Equal(result.FileCount, add.FileCount);
            Assert.Equal(result.DirectoryCount, add.DirectoryCount);
            Assert.Equal(result.CountryCode, add.CountryCode);
        }
Exemplo n.º 4
0
        public async Task AddUserAsync_Throws_InvalidOperationException_If_Logged_In(SoulseekClientStates state)
        {
            var s = new SoulseekClient();

            s.SetProperty("State", state);

            var ex = await Record.ExceptionAsync(async() => await s.AddUserAsync("a"));

            Assert.NotNull(ex);
            Assert.IsType <InvalidOperationException>(ex);
        }
Exemplo n.º 5
0
        public async Task AddUserAsync_Throws_ArgumentException_On_Null_Username(string username)
        {
            var s = new SoulseekClient();

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            var ex = await Record.ExceptionAsync(async() => await s.AddUserAsync(username));

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
        }
Exemplo n.º 6
0
        public async Task AddUserAsync_Uses_Given_CancellationToken(string username, UserData userData, CancellationToken cancellationToken)
        {
            var result = new AddUserResponse(username, true, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                await s.AddUserAsync(username, cancellationToken);
            }

            serverConn.Verify(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), cancellationToken), Times.Once);
        }
Exemplo n.º 7
0
        public async Task AddUserAsync_Returns_Expected_Info(string username, UserData userData)
        {
            var result = new AddUserResponse(username, true, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var add = await s.AddUserAsync(username);

                Assert.Equal(result.UserData, add);
            }
        }
Exemplo n.º 8
0
        public async Task AddUserAsync_Throws_UserNotFoundException_When_Exists_Is_False(string username, UserData userData)
        {
            var result = new AddUserResponse(username, false, userData);

            var waiter = new Mock <IWaiter>();

            waiter.Setup(m => m.Wait <AddUserResponse>(It.IsAny <WaitKey>(), null, It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(result));

            var serverConn = new Mock <IMessageConnection>();

            serverConn.Setup(m => m.WriteAsync(It.IsAny <IOutgoingMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            using (var s = new SoulseekClient(waiter: waiter.Object, serverConnection: serverConn.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

                var ex = await Record.ExceptionAsync(() => s.AddUserAsync(username));

                Assert.NotNull(ex);
                Assert.IsType <UserNotFoundException>(ex);
            }
        }