コード例 #1
0
        public void SerializeToString_WhenBatchedVersionedMessage_ExpectCorrectString()
        {
            // Arrange
            var serializerSettings = new SerializerSettings {
                SerializerType = SerializerType.Json
            };
            var messageSerializer = new MessageSerializer(serializerSettings);

            var testEntity = new TestEntity {
                Data = "val"
            }.SerializeToVersionedMessage(SerializerType.Json);

            var batchedVersionedMessage = new BatchedVersionedMessage {
                Messages = new List <VersionedMessage> {
                    testEntity
                }
            };

            // Act
            var stopwatch               = Stopwatch.StartNew();
            var serializeToString       = messageSerializer.SerializeToString(batchedVersionedMessage);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(serializeToString, Is.EqualTo("{\"m\":[{\"v\":{\"T\":\"TestEntity\",\"Ma\":1,\"Mi\":0,\"P\":0,\"VT\":0,\"VTV\":0},\"d\":\"{\\\"Data\\\":\\\"val\\\"}\"}]}"));
        }
コード例 #2
0
        public void VersionedMessageHandler_WhenBatchedVersionedMessage_ExpectAllProcessed()
        {
            // Arrange
            var entityOne    = default(TestEntityOne);
            var entityTwo    = default(TestEntityTwo);
            var defaultValue = default(string);
            var exception    = default(Exception);

            var versionedMessageHandler = new VersionedMessageHandler(SerializerType.ProtocolBuffers, true)
                                          .On <TestEntityOne>(e => entityOne = e)
                                          .On <TestEntityTwo>(e => entityTwo = e)
                                          .Else(s => defaultValue            = s)
                                          .OnError(e => exception            = e);

            var batchedVersionedMessage = new BatchedVersionedMessage
            {
                Messages = new List <VersionedMessage>
                {
                    new TestEntityOne {
                        Value1 = "test", Value2 = 42
                    }.SerializeToVersionedMessage(),
                    new TestEntityTwo {
                        Value1 = "Value1", Value2 = new DateTime(2018, 1, 1)
                    }.SerializeToVersionedMessage(),
                    new TestEntityThree {
                        Value1 = 3
                    }.SerializeToVersionedMessage(),
                },
            };

            // Act
            var stopwatch = Stopwatch.StartNew();

            versionedMessageHandler.Post(batchedVersionedMessage);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(entityOne, Is.Not.Null);
            Assert.That(entityTwo, Is.Not.Null);
            Assert.That(defaultValue, Is.Not.Null);
            Assert.That(exception, Is.Null);

            Assert.That(entityOne.Value1, Is.EqualTo("test"));
            Assert.That(entityOne.Value2, Is.EqualTo(42));

            Assert.That(entityTwo.Value1, Is.EqualTo("Value1"));
            Assert.That(entityTwo.Value2, Is.EqualTo(new DateTime(2018, 1, 1)));

            Assert.That(defaultValue.Deserialize <TestEntityThree>().Value1, Is.EqualTo(3));
        }
コード例 #3
0
        public void VersionedMessageHandler_WhenBatchedVersionedMessageWithNoHandler_ExpectNoAction()
        {
            // Arrange
            var entityOne    = default(TestEntityOne);
            var entityTwo    = default(TestEntityTwo);
            var defaultValue = default(string);
            var exception    = default(Exception);

            var versionedMessageHandler = new VersionedMessageHandler(SerializerType.ProtocolBuffers, false);

            var batchedVersionedMessage = new BatchedVersionedMessage
            {
                Messages = new List <VersionedMessage>
                {
                    new TestEntityOne {
                        Value1 = "test", Value2 = 42
                    }.SerializeToVersionedMessage(),
                    new TestEntityTwo {
                        Value1 = "Value1", Value2 = new DateTime(2018, 1, 1)
                    }.SerializeToVersionedMessage(),
                    new TestEntityThree {
                        Value1 = 3
                    }.SerializeToVersionedMessage(),
                },
            };

            // Act
            var stopwatch = Stopwatch.StartNew();

            versionedMessageHandler.Post(batchedVersionedMessage);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(entityOne, Is.Null);
            Assert.That(entityTwo, Is.Null);
            Assert.That(defaultValue, Is.Null);
            Assert.That(exception, Is.Null);
        }
コード例 #4
0
        public void SerializeToString_WhenBatchedVersionedMessageWithProtoBuf_ExpectData()
        {
            // Arrange
            var serializerSettings = new SerializerSettings {
                SerializerType = SerializerType.ProtocolBuffers
            };
            var messageSerializer = new MessageSerializer(serializerSettings);

            var testEntity = new TestEntity {
                Data = "val"
            }.SerializeToVersionedMessage(SerializerType.ProtocolBuffers);

            var batchedVersionedMessage = new BatchedVersionedMessage {
                Messages = new List <VersionedMessage> {
                    testEntity
                }
            };

            // Act
            var stopwatch               = Stopwatch.StartNew();
            var serializeToString       = messageSerializer.SerializeToString(batchedVersionedMessage);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var deserialize = serializeToString.Deserialize <BatchedVersionedMessage>();

            Assert.That(deserialize, Is.Not.Null);

            var message = deserialize.Messages.First();

            Assert.That(message, Is.Not.Null);
            Assert.That(message.Version, Is.EqualTo(new DtoVersion(typeof(TestEntity), 1, 0, 0)));
        }
コード例 #5
0
        /// <summary>
        /// Serializes to string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>
        /// The <see cref="string" />
        /// </returns>
        public string SerializeToString(BatchedVersionedMessage source)
        {
            var serializerType = this.serializerSettings.SerializerType;

            return(source.SerializeToString(serializerType));
        }
        public void VersionedMessageHandler_WhenBatchedVersionedMessage_ExpectAllProcessed()
        {
            // Arrange
            var exception = default(Exception);

            var versionedMessageHandler =
                new VersionedMessageHandler <TestEntityOne>(SerializerType.ProtocolBuffers, true)
                .On <TestEntityOne>(e => e)
                .On <TestEntityTwo>(e => new TestEntityOne {
                Value1 = e.Value1, Value2 = 5
            })
                .Else(s => new TestEntityOne {
                Value2 = 42, Value1 = "DEFAULT"
            })
                .OnError(
                    e =>
            {
                exception = e;
                return(default(TestEntityOne));
            });

            var batchedVersionedMessage = new BatchedVersionedMessage
            {
                Messages = new List <VersionedMessage>
                {
                    new TestEntityOne {
                        Value1 = "test", Value2 = 42
                    }.SerializeToVersionedMessage(),
                    new TestEntityTwo {
                        Value1 = "Value1", Value2 = new DateTime(2018, 1, 1)
                    }.SerializeToVersionedMessage(),
                    new TestEntityThree {
                        Value1 = 3
                    }.SerializeToVersionedMessage(),
                },
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var results   = versionedMessageHandler.Post(batchedVersionedMessage);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var resultsList = results.ToList();

            Assert.That(resultsList[0], Is.Not.Null);
            Assert.That(resultsList[1], Is.Not.Null);
            Assert.That(resultsList[2], Is.Not.Null);
            Assert.That(exception, Is.Null);

            Assert.That(resultsList[0].Value1, Is.EqualTo("test"));
            Assert.That(resultsList[0].Value2, Is.EqualTo(42));

            Assert.That(resultsList[1].Value1, Is.EqualTo("Value1"));
            Assert.That(resultsList[1].Value2, Is.EqualTo(5));

            Assert.That(resultsList[2].Value1, Is.EqualTo("DEFAULT"));
            Assert.That(resultsList[2].Value2, Is.EqualTo(42));
        }