public void ReadContinuously_Raises_MessageDataRead_On_Read(string username, IPEndPoint endpoint, int code)
        {
            int callCount = 0;

            var streamMock = new Mock <INetworkStream>();

            streamMock.Setup(s => s.ReadAsync(It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>()))
            .Callback <Memory <byte>, CancellationToken>((bytes, token) =>
            {
                if (callCount % 2 == 0)
                {
                    var data = BitConverter.GetBytes(4);
                    data.AsSpan().CopyTo(bytes.Span);
                }
                else if (callCount % 2 == 1)
                {
                    var data = BitConverter.GetBytes(code);
                    data.AsSpan().CopyTo(bytes.Span);
                }

                callCount++;
            })
            .Returns(ValueTask.FromResult(4));

            var tcpMock = new Mock <ITcpClient>();

            using (var socket = new Socket(SocketType.Stream, ProtocolType.IP))
            {
                tcpMock.Setup(m => m.Client).Returns(socket);
                tcpMock.Setup(s => s.Connected).Returns(true);
                tcpMock.Setup(s => s.GetStream()).Returns(streamMock.Object);

                byte[] readMessage = null;

                using (var c = new MessageConnection(username, endpoint, tcpClient: tcpMock.Object))
                {
                    c.StartReadingContinuously();

                    c.MessageDataRead += (sender, e) => readMessage = e.Code;

                    var maxTime = DateTime.UtcNow.AddSeconds(5);

                    // ReadContinuouslyAsync() runs in a separate task, so events won't arrive immediately after connect
                    do
                    {
                        Thread.Sleep(100);
                    }while (readMessage == null && DateTime.UtcNow <= maxTime);

                    Assert.Equal(code, BitConverter.ToInt32(readMessage));
                }
            }
        }
Пример #2
0
        public void ReadContinuously_Raises_MessageReceived_On_Read(string username, IPEndPoint endpoint, int code)
        {
            int callCount = 0;

            var streamMock = new Mock <INetworkStream>();

            streamMock.Setup(s => s.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback <byte[], int, int, CancellationToken>((bytes, offset, length, token) =>
            {
                if (callCount % 2 == 0)
                {
                    var data = BitConverter.GetBytes(4);
                    Array.Copy(data, bytes, data.Length);
                }
                else if (callCount % 2 == 1)
                {
                    var data = BitConverter.GetBytes(code);
                    Array.Copy(data, bytes, data.Length);
                }

                callCount++;
            })
            .Returns(Task.Run(() => 4));

            var tcpMock = new Mock <ITcpClient>();

            using (var socket = new Socket(SocketType.Stream, ProtocolType.IP))
            {
                tcpMock.Setup(m => m.Client).Returns(socket);
                tcpMock.Setup(s => s.Connected).Returns(true);
                tcpMock.Setup(s => s.GetStream()).Returns(streamMock.Object);

                byte[] readMessage = null;

                using (var c = new MessageConnection(username, endpoint, tcpClient: tcpMock.Object))
                {
                    c.StartReadingContinuously();

                    c.MessageReceived += (sender, e) => readMessage = e.Code;

                    Thread.Sleep(1000); // ReadContinuouslyAsync() runs in a separate task, so events won't arrive immediately after connect

                    Assert.Equal(code, BitConverter.ToInt32(readMessage));
                }
            }
        }