예제 #1
0
        public async Task WhenReceiveHeartBeatPing_WhileListeningForPushNotification_ShouldSendBackHeartBeatAck()
        {
            FakeFcmConnection connection = null;
            bool sentHeartBeatAck        = false;
            bool expectHeartBeatAck      = true;

            connection = new FakeFcmConnection((ctoken) =>
            {
                byte[] buffer = new byte[2048];
                byte[] fakeData;

                fakeData = new byte[] { 41 };
                connection.SimulateDataReceived(fakeData);
                fakeData = new byte[] { 3, 62, 10, 18, 99, 104, 114, 111, 109, 101, 45, 54, 51, 46, 48, 46, 51, 50, 51, 52, 46, 48, 18, 31, 117, 115, 101, 114, 64, 102, 105, 114, 101, 98, 97, 115, 101, 46, 99, 111, 109, 47, 110, 111, 116, 105, 102, 105, 99, 97, 116, 105, 111, 110, 115, 48, 1, 64, 192, 216, 151, 177, 230, 46, };
                connection.SimulateDataReceived(fakeData);

                fakeData = new byte[] { 7, 10, 16, 1, 26, 0, 58, 4, 8, 12, 18, 0, };
                connection.SimulateDataReceived(fakeData);

                expectHeartBeatAck = true;
                fakeData           = new byte[] { 0, 0 };
                connection.SimulateDataReceived(fakeData);
                expectHeartBeatAck = false;

                return(Task.CompletedTask);
            });

            connection.DataSent += (s, data) =>
            {
                if (expectHeartBeatAck == true)
                {
                    using (var memoryStream = new MemoryStream(data))
                    {
                        MessageTag currentTag = (MessageTag)memoryStream.ReadByte(); // read messageTag byte

                        if (currentTag == MessageTag.kHeartbeatAckTag)
                        {
                            var instance = HeartbeatAck.Parser.ParseDelimitedFrom(memoryStream);
                            if (instance != null)
                            {
                                sentHeartBeatAck = true;
                            }
                        }
                    }
                }
            };

            FcmListener listener = new FcmListener(connection);
            await listener.ConnectAsync();

            await listener.LoginAsync(0, 0);

            await listener.ListenAsync();

            Assert.IsTrue(sentHeartBeatAck);
        }
예제 #2
0
        public async Task FcmListenerConnect_LoginRequest_ShouldBeTheFirstDataSentToTheServer()
        {
            FakeFcmConnection connection = null;
            bool expectLoginRequest      = true;
            int  expectedVersion         = 41;
            int  version          = 0;
            bool sentLoginRequest = false;

            connection = new FakeFcmConnection((ctoken) => Task.CompletedTask);

            connection.DataSent += (s, data) =>
            {
                if (expectLoginRequest == true)
                {
                    expectLoginRequest = false;

                    using (var memoryStream = new MemoryStream(data))
                    {
                        version = memoryStream.ReadByte();
                        Assert.AreEqual(MessageTag.kLoginRequestTag, (MessageTag)memoryStream.ReadByte());

                        var instance = LoginRequest.Parser.ParseDelimitedFrom(memoryStream);
                        if (instance != null)
                        {
                            sentLoginRequest = true;
                        }
                    }
                }
            };

            FcmListener listener = new FcmListener(connection);
            await listener.ConnectAsync();

            await listener.LoginAsync(0, 0);

            await listener.ListenAsync();

            Assert.AreEqual(expectedVersion, version);
            Assert.IsTrue(sentLoginRequest);
        }