Exemplo n.º 1
0
        public async Task Stops_on_dirty_disconnect()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            mock.ReturnTaskWithFaultOnEmptyQueue = true;

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);
        }
        public async Task Can_receive_whole_message()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            receiveResult.Enqueue(new WebSocketReceiveResult(4096, WebSocketMessageType.Text, false));
            receiveResult.Enqueue(new WebSocketReceiveResult(4096, WebSocketMessageType.Text, false));
            receiveResult.Enqueue(new WebSocketReceiveResult(1024, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received().ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 2 * 4096 + 1024));
        }
Exemplo n.º 3
0
        public async Task PreviouslySolvedPuzzlesRequest_Should_Send_A_List_With_One_SudokuPuzzle()
        {
            SolveSudokuRequest solveSudokuRequest = new SolveSudokuRequest
            {
                Data = new SudokuPuzzleState
                {
                    PuzzleAsString = "034007008080065000000300070200000700710040096005000001050002000000170060600900430"
                },
                EventName = "sudokuSolvePuzzleRequest"
            };
            string jsonSolve = JsonConvert.SerializeObject(solveSudokuRequest);

            byte[]        messageBufferSolve  = Encoding.Default.GetBytes(jsonSolve);
            WebSocketMock webSocketMockSolver = new WebSocketMock();

            _httpHandler.AddResponse(GetDefaultSavePuzzle());
            await _puzzleHandler.ReceiveAsync(webSocketMockSolver, messageBufferSolve);

            PreMessage previouslySolvedPuzzlesRequest = new PreMessage
            {
                EventName = "previouslySolvedPuzzlesRequest"
            };
            string json = JsonConvert.SerializeObject(previouslySolvedPuzzlesRequest);

            byte[]        messageBuffer = Encoding.Default.GetBytes(json);
            WebSocketMock webSocketMock = new WebSocketMock();
            PuzzleDto     puzzleDto     = new PuzzleDto
            {
                OriginalPuzzle = "034007008080065000000300070200000700710040096005000001050002000000170060600900430",
                PuzzleId       = 1,
                PuzzleType     = "Sudoku",
                SolvedPuzzle   = "534297618187465329962381574246819753718543296395726841459632187823174965671958432"
            };

            _httpHandler.AddResponse(GetPreviouslySolvedPuzzles(new List <PuzzleDto> {
                puzzleDto
            }));

            await _puzzleHandler.ReceiveAsync(webSocketMock, messageBuffer);

            (_, ArraySegment <byte> arraySegment) = webSocketMock.SentMessages.First();
            string responseJson = Encoding.Default.GetString(arraySegment);
            PreviouslySolvedPuzzlesResponse sudokuGeneratePuzzleResponse = JsonConvert.DeserializeObject <PreviouslySolvedPuzzlesResponse>(responseJson);

            Assert.IsTrue(sudokuGeneratePuzzleResponse.Success);
            Assert.IsTrue(sudokuGeneratePuzzleResponse.Data.Puzzles.Count(puzzle => puzzle.PuzzleType.Equals("Sudoku")) == 1);
        }
Exemplo n.º 4
0
        public async Task SudokuGeneratePuzzleRequest_Should_Send_A_Valid_SudokuPuzzleState()
        {
            PreMessage sudokuGeneratePuzzleRequest = new PreMessage
            {
                EventName = "sudokuGeneratePuzzleRequest"
            };
            string json = JsonConvert.SerializeObject(sudokuGeneratePuzzleRequest);

            byte[]        messageBuffer = Encoding.Default.GetBytes(json);
            WebSocketMock webSocketMock = new WebSocketMock();
            await _puzzleHandler.ReceiveAsync(webSocketMock, messageBuffer);

            (_, ArraySegment <byte> arraySegment) = webSocketMock.SentMessages.First();
            string responseJson = Encoding.Default.GetString(arraySegment);
            SudokuGeneratePuzzleResponse sudokuGeneratePuzzleResponse = JsonConvert.DeserializeObject <SudokuGeneratePuzzleResponse>(responseJson);

            Assert.IsTrue(sudokuGeneratePuzzleResponse.Success);
        }
        public async Task Throws_on_too_long_message()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 1024; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(1024, WebSocketMessageType.Text, false));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            Assert.ThrowsAsync <InvalidOperationException>(async() => await mock.ReceiveAsync(webSocketsClient));
            await webSocketsClient.DidNotReceive().ReceiveAsync(Arg.Any <Memory <byte> >());
        }
        public async Task Can_receive_many_messages()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 1000; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(1234, WebSocketMessageType.Text, true));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));

            WebSocketMock     mock             = new WebSocketMock(receiveResult);
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received(1000).ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 1234));
        }
        public async Task Can_receive_whole_message_non_buffer_sizes()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 6; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(2000, WebSocketMessageType.Text, false));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received().ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 6 * 2000 + 1));
        }
Exemplo n.º 8
0
        public async Task SudokuSolvePuzzleRequest_Should_Send_6598_PuzzleStateChanges()
        {
            SolveSudokuRequest solveSudokuRequest = new SolveSudokuRequest
            {
                Data = new SudokuPuzzleState
                {
                    PuzzleAsString = "034007008080065000000300070200000700710040096005000001050002000000170060600900430"
                },
                EventName = "sudokuSolvePuzzleRequest"
            };
            string json = JsonConvert.SerializeObject(solveSudokuRequest);

            byte[]        messageBuffer = Encoding.Default.GetBytes(json);
            WebSocketMock webSocketMock = new WebSocketMock();

            _httpHandler.AddResponse(GetDefaultSavePuzzle());
            await _puzzleHandler.ReceiveAsync(webSocketMock, messageBuffer);

            Assert.IsTrue(webSocketMock.SentMessages.Count(tuple => tuple.preMessage.EventName.Equals("sudokuPuzzleStateChange")) == 6598);
        }
Exemplo n.º 9
0
        public async Task PreviouslySolvedPuzzlesRequest_Should_Send_An_Empty_List()
        {
            PreMessage previouslySolvedPuzzlesRequest = new PreMessage
            {
                EventName = "previouslySolvedPuzzlesRequest"
            };
            string json = JsonConvert.SerializeObject(previouslySolvedPuzzlesRequest);

            byte[]        messageBuffer = Encoding.Default.GetBytes(json);
            WebSocketMock webSocketMock = new WebSocketMock();

            _httpHandler.AddResponse(GetPreviouslySolvedPuzzles(new List <PuzzleDto>()));

            await _puzzleHandler.ReceiveAsync(webSocketMock, messageBuffer);

            (_, ArraySegment <byte> arraySegment) = webSocketMock.SentMessages.First();
            string responseJson = Encoding.Default.GetString(arraySegment);
            PreviouslySolvedPuzzlesResponse sudokuGeneratePuzzleResponse = JsonConvert.DeserializeObject <PreviouslySolvedPuzzlesResponse>(responseJson);

            Assert.IsTrue(sudokuGeneratePuzzleResponse.Success);
            Assert.IsFalse(sudokuGeneratePuzzleResponse.Data.Puzzles.Any());
        }
Exemplo n.º 10
0
        public async Task SudokuVerifyRequest_Should_Send_PuzzleState_With_27_CorrectIndexes()
        {
            VerifySudokuRequest sudokuVerifyRequest = new VerifySudokuRequest
            {
                Data = new VerifiableSudokuState
                {
                    PuzzleAsString = "034007008080065000000300070200000700710040096005000001050002000000170060600900430",
                    CurrentPuzzle  = "034007008080065000000300070200000700710040096005000001050002000000170060600900430"
                },
                EventName = "sudokuVerifyRequest"
            };
            string json = JsonConvert.SerializeObject(sudokuVerifyRequest);

            byte[]        messageBuffer = Encoding.Default.GetBytes(json);
            WebSocketMock webSocketMock = new WebSocketMock();
            await _puzzleHandler.ReceiveAsync(webSocketMock, messageBuffer);

            (_, ArraySegment <byte> arraySegment) = webSocketMock.SentMessages.Last();
            string responseJson = Encoding.Default.GetString(arraySegment);
            VerifySudokuPuzzleResponse verifySudokuPuzzleResponse = JsonConvert.DeserializeObject <VerifySudokuPuzzleResponse>(responseJson);

            Assert.IsTrue(verifySudokuPuzzleResponse.Data.CorrectIndexes.Length == 27);
        }