Exemplo n.º 1
0
        public void ShouldSendString()
        {
            var    serializer         = new JsonSerializer();
            Guid   eventId            = Guid.NewGuid();
            string eventName          = "Chat";
            string message            = "Hola";
            var    metaData           = new List <KeyValue>();
            string serializedMetaData = serializer.Serialize(metaData);
            string to   = "Bob";
            var    info = new EventInfo(eventName, to, string.Empty);
            Action <object, string> onMetaSerialized = (cSharpObj, json) => {
                Assert.Equal(serializedMetaData, json);
                Assert.True(cSharpObj is List <KeyValue>);
            };
            var metaSerializer = new SerializationMock(onMetaSerialized);

            Func <string, object, object, object, object, Guid> action = (serverAction, o1, o2, o3, o4) => {
                Assert.Equal(EventNames.SendStringTo, serverAction);
                Assert.Equal(eventName, o1);
                Assert.Equal(info.To, o2);
                Assert.Equal(serializedMetaData, o3);
                Assert.Equal(message, o4);
                return(eventId);
            };

            ConnectionMock connection = new ConnectionMock(action);
            EventSender    sender     = new EventSender(connection, metaSerializer);

            Task <Guid> id = sender.String(info, message);

            id = sender.String(info, new StringMessage(message, metaData));
            id = sender.String(info, new StringMessage(message, metaData), metaSerializer);

            Assert.Equal(eventId, id.GetAwaiter().GetResult());
        }
Exemplo n.º 2
0
        public void GetBinaryGenericMetaData()
        {
            var    serializer = new JsonSerializer();
            string eventName  = "File";

            byte[] message            = new byte[] { 1, 2, 3 };
            var    metaData           = new List <KeyValue>();
            string serializedMetaData = serializer.Serialize(metaData);
            Action <object, string> onMetaSerialized = (cSharpObj, json) => {
                Assert.Equal(serializedMetaData, json);
                Assert.True(cSharpObj is List <KeyValue>);
            };
            var metaSerializer = new SerializationMock(onMetaSerialized);

            ConnectionMock connection = new ConnectionMock();
            var            factory    = new ObservableFactory(connection, metaSerializer);
            // Sending generic T
            var observable = factory.GetBinary <List <KeyValue> >(eventName, metaSerializer);

            observable.Subscribe(msg => {
                Assert.Equal(message, msg.Data);
                Assert.Equal(metaData, msg.MetaData);
            });


            var serverEvent = connection.ServerEvents[eventName] as Action <string, byte[]>;

            Assert.NotNull(serverEvent);
            serverEvent(serializedMetaData, message);
        }
Exemplo n.º 3
0
        public void ShouldSendBinary()
        {
            var    serializer = new JsonSerializer();
            Guid   eventId    = Guid.NewGuid();
            string eventName  = "Binary";

            byte[] message            = new byte[] { 1, 2, 3 };
            var    metaData           = new List <KeyValue>();
            string serializedMetaData = serializer.Serialize(metaData);
            string to   = "Bob";
            var    info = new EventInfo(eventName, to, string.Empty);

            Func <string, object, object, object, object, Guid> action = (serverAction, o1, o2, o3, o4) => {
                Assert.Equal(EventNames.SendBinaryTo, serverAction);
                Assert.Equal(eventName, o1);
                Assert.Equal(info.To, o2);
                Assert.Equal(serializedMetaData, o3);
                Assert.Equal(message, o4);
                return(eventId);
            };

            ConnectionMock connection = new ConnectionMock(action);
            EventSender    sender     = new EventSender(connection, serializer);

            Task <Guid> id = sender.Binary(info, message);

            id = sender.Binary(info, new BinaryMessage(message, metaData));
            id = sender.Binary(info, new BinaryMessage(message, metaData), serializer);

            Assert.Equal(eventId, id.GetAwaiter().GetResult());
        }
        public void GetSerializedGenericMetaData()
        {
            string eventName = "Person";
            Person message   = new Person()
            {
                Name = "Alice", Age = 27
            };
            var metaData = new List <KeyValue>();

            var    serializer         = new JsonSerializer();
            string serializedMetaData = serializer.Serialize(metaData);
            string serializedData     = serializer.Serialize(message);

            int serializationCounter = 0;

            Action <string, object> onDeserialized = (json, cSharpObj) => {
                if (cSharpObj is Person)
                {
                    Assert.Equal(serializedData, json);
                }
                if (cSharpObj is List <KeyValue> )
                {
                    Assert.Equal(serializedMetaData, json);
                }
                Assert.True(cSharpObj is List <KeyValue> || cSharpObj is Person);

                serializationCounter++;
            };
            var customSerializer = new SerializationMock(onDeserialized);

            ConnectionMock connection = new ConnectionMock();
            var            factory    = new ObservableFactory(connection, serializer);
            // Sending generic MetaData
            var observable = factory.GetSerialized <Person, List <KeyValue> >(eventName, customSerializer);

            observable.Subscribe(msg => {
                Assert.Equal(message.Name, msg.Data.Name);
                Assert.Equal(message.Age, msg.Data.Age);
                Assert.Equal(metaData, msg.MetaData);
            });


            var serverEvent = connection.ServerEvents[eventName] as Action <string, string>;

            Assert.NotNull(serverEvent);
            serverEvent(serializedMetaData, serializedData);

            Assert.Equal(2, serializationCounter);
        }
Exemplo n.º 5
0
        public void GetStringGenericMetaData()
        {
            var    serializer         = new JsonSerializer();
            string eventName          = "Chat";
            string message            = "Hola";
            var    metaData           = new List <KeyValue>();
            string serializedMetaData = serializer.Serialize(metaData);

            ConnectionMock connection = new ConnectionMock();
            var            factory    = new ObservableFactory(connection, serializer);
            // Sending generic T
            var observable = factory.GetString <List <KeyValue> >(eventName, serializer);

            observable.Subscribe(msg => {
                Assert.Equal(message, msg.Data);
                Assert.Equal(metaData, msg.MetaData);
            });


            var serverEvent = connection.ServerEvents[eventName] as Action <string, string>;

            Assert.NotNull(serverEvent);
            serverEvent(serializedMetaData, message);
        }
Exemplo n.º 6
0
        public void ShouldSendSerialized()
        {
            var    serializer = new JsonSerializer();
            Guid   eventId    = Guid.NewGuid();
            string eventName  = "Person";
            var    message    = new Person()
            {
                Name = "Jhon Doe", Age = 55
            };
            var    metaData           = new List <KeyValue>();
            string serializedMetaData = serializer.Serialize(metaData);
            string serializedData     = serializer.Serialize(message);
            string to   = "Bob";
            var    info = new EventInfo(eventName, to, string.Empty);
            int    serializationCounter = 0;

            Func <string, object, object, object, object, Guid> action = (serverAction, o1, o2, o3, o4) => {
                Assert.Equal(EventNames.SendStringTo, serverAction);
                Assert.Equal(eventName, o1);
                Assert.Equal(info.To, o2);
                Assert.Equal(serializedMetaData, o3);
                Assert.Equal(serializer.Serialize(message), o4);
                return(eventId);
            };

            Action <object, string> onDataSerialized = (cSharpObj, json) => {
                Assert.Equal(serializedData, json);
                Assert.True(cSharpObj is Person);
            };
            var dataSerializer = new SerializationMock(onDataSerialized);

            Action <object, string> onMetaSerialized = (cSharpObj, json) => {
                Assert.Equal(serializedMetaData, json);
                Assert.True(cSharpObj is List <KeyValue>);
            };
            var metaSerializer = new SerializationMock(onMetaSerialized);

            Action <object, string> bothAction = (cSharpObj, json) => {
                if (cSharpObj is Person)
                {
                    Assert.Equal(serializedData, json);
                }
                if (cSharpObj is List <KeyValue> )
                {
                    Assert.Equal(serializedMetaData, json);
                }
                Assert.True(cSharpObj is List <KeyValue> || cSharpObj is Person);

                serializationCounter++;
            };
            var bothSerializer = new SerializationMock(bothAction);

            ConnectionMock connection = new ConnectionMock(action);
            EventSender    sender     = new EventSender(connection, serializer);

            Task <Guid> id = sender.Serialized <Person>(info, message, dataSerializer);

            id = sender.Serialized <Person>(info, new StringSerializedMessage <Person>(message, metaData), dataSerializer);
            id = sender.Serialized <Person, List <KeyValue> >(info, new StringSerializedMessage <Person, List <KeyValue> >(message, metaData), bothSerializer);
            id = sender.Serialized <Person, List <KeyValue> >(info, new StringSerializedMessage <Person, List <KeyValue> >(message, metaData), dataSerializer, metaSerializer);

            Assert.Equal(eventId, id.GetAwaiter().GetResult());
            Assert.Equal(2, serializationCounter);
        }