public void Send_TcpServiceSendCalled_NotThrows()
        {
            var         task          = Task.Delay(1);
            const ulong SendMessageId = 1234;

            var mTcpService = TcpServiceMock.Create().BuildSend(returnTask: () => task);

            this.RegisterMock(mTcpService);

            var seqNo = 1;

            var mSession = SessionMock.Create()
                           .BuildGenerateMessageSeqNo(confirm => Tuple.Create(SendMessageId, seqNo));
            var mClientSettings = ClientSettingsMock.Create().AttachSession(() => mSession.Object);

            this.RegisterMock(mClientSettings);

            this.RegisterType <TcpTransport>();

            // ---

            var transport = this.Resolve <TcpTransport>();

            transport.Send(new byte[1]);
            Thread.Sleep(1000);
            // --

            mTcpService.Verify(service => service.Send(It.IsAny <byte[]>()), Times.Once);
        }
        public void SendAndRecive_SimpleCall_NotThrows()
        {
            var        sendData      = new byte[] { 123, 214 };
            const long SendMessageId = 1234;

            var        receiveData     = new byte[] { 123, 214 };
            const long ReciveMessageId = 12;
            const long AuthKeyId       = 12123123;

            var mTcpTransport = TcpTransportMock.Create();

            AddSendHandler(mTcpTransport, SendMessageId, sendData);
            AddReceiveHandler(mTcpTransport, AuthKeyId, ReciveMessageId, receiveData);

            this.RegisterMock(mTcpTransport);

            var mSession        = SessionMock.Create().BuildGenerateMesId(() => SendMessageId);
            var mClientSettings = ClientSettingsMock.Create().AttachSession(() => mSession.Object);

            this.RegisterMock(mClientSettings);



            this.RegisterType <MtProtoPlainSender>();

            // ---

            var mtProtoPlainSender = this.Resolve <MtProtoPlainSender>();
            var sendTask           = mtProtoPlainSender.SendAndReceive(sendData);

            // --

            Assert.Equal(receiveData, sendTask.Result);
            mTcpTransport.Verify(transport => transport.Send(It.IsAny <byte[]>()), Times.Once);
        }
        public async Task Send_SimpleCall_NotThrows()
        {
            const ulong SendMessageId = 1234;
            var         authKey       = SessionMock.GenerateAuthKeyData();
            ulong       sessionId     = 1231231;
            ulong       salt          = 5432111;
            var         seqNo         = 123;
            var         mSession      = SessionMock.Create()
                                        .BuildGenerateMessageSeqNo(confirm => Tuple.Create(SendMessageId, seqNo))
                                        .BuildSession(sessionId, salt, authKey);

            var request = new RequestPing();

            var mTcpTransport = TcpTransportMock.Create();

            AddSendHandler(mTcpTransport, request);

            this.RegisterMock(mTcpTransport);

            var mClientSettings = ClientSettingsMock.Create().AttachSession(() => mSession.Object);

            this.RegisterMock(mClientSettings);

            var mConfirmRecieve = ConfirmationRecieveServiceMock.Create()
                                  .BuildWaitForConfirm(messageId =>
            {
                Assert.Equal(SendMessageId, messageId);
                return(Task.FromResult(true));
            });

            this.RegisterMock(mConfirmRecieve);

            this.RegisterType <MtProtoSendService>();

            // ---

            var mtProtoPlainSender = this.Resolve <MtProtoSendService>();
            var sendResult         = mtProtoPlainSender.Send(request);

            await sendResult.Result.Item1;

            // --

            mTcpTransport.Verify(transport => transport.Send(It.IsAny <byte[]>()), Times.Once);

            mConfirmRecieve.Verify(store => store.WaitForConfirm(It.IsAny <ulong>()), Times.Once);
        }
Beispiel #4
0
        public void Recieve_SimpleCall_NotThrows()
        {
            const long  RequestMessageId = 1234;
            var         authKeyData      = SessionMock.GenerateAuthKeyData();
            const ulong sessionId        = 123456;
            const ulong salt             = 654321;

            uint[] rpcResponceCode = { 0xf35c6d01 };

            var sendUser = new TDialog
            {
                ReadInboxMaxId = 132,
                Peer           = new TPeerUser {
                    UserId = 123
                },
                NotifySettings = new TPeerNotifySettingsEmpty(),
                Draft          = new TDraftMessageEmpty()
            };

            var mSession = SessionMock.Create().BuildSession(sessionId, salt, authKeyData);

            this.RegisterMock(mSession);

            var mClientSettings = ClientSettingsMock.Create().AttachSession(() => mSession.Object);

            this.RegisterMock(mClientSettings);

            var mTcpTransport = TcpTransportMock.Create().BuildReceieve(out var tsc);

            this.RegisterMock(mTcpTransport);

            var mConfrimSendService = ConfirmationSendServiceMock.Create().BuildAddForSend(messageId => Assert.Equal(RequestMessageId, messageId));

            this.RegisterMock(mConfrimSendService);

            var mRecieveHandler = RecieveHandlerMock.Create().BuildRecieveHandler(rpcResponceCode).BuildHandleResponce(
                (code, reader) =>
            {
                Assert.Equal(RequestMessageId, reader.ReadInt64());
                return(null);
            });

            this.RegisterMock(mRecieveHandler);
            this.RegisterAdapterForHandler();

            this.RegisterType <RecievingService>();

            // ---
            var rpcResult = new TRpcResult {
                ReqMsgId = RequestMessageId, Result = sendUser
            };
            var recieveData = EncodePacket(Serializer.SerializeObject(rpcResult), RequestMessageId);

            var mtProtoPlainSender = this.Resolve <RecievingService>();

            mtProtoPlainSender.StartReceiving();
            Thread.Sleep(500);

            tsc.SetResult(recieveData);

            Thread.Sleep(500);

            // --
            mRecieveHandler.Verify(recieveService => recieveService.HandleResponce(It.IsAny <BinaryReader>()), Times.Once);
            mConfrimSendService.Verify(recieveService => recieveService.AddForSend(It.IsAny <long>()), Times.Once);
        }