Exemplo n.º 1
0
        public void TestJsonEncoder_EncodeSuccess()
        {
            /* PRECONDITION */
            Debug.Assert(encoder != null);
            Debug.Assert(fileSystem != null);
            Debug.Assert(config != null);
            Debug.Assert(container != null);
            Debug.Assert(input != null);

            container.ComposeExportedValue <IEncodableEventQueue <JsonIntermediateFormatSample> >(input);
            container.ComposeExportedValue(config);
            container.ComposeParts(encoder);

            /* GIVEN */
            var random    = new Random();
            var eventList = new List <TestEvent>
            {
                new TestEvent(random.Next()),
                new TestEvent(random.Next())
            };

            var serializedEvent = JsonManualSerialization.GenerateSerializedEvents(eventList);

            config.RelativeFilePath = new FilePath(fileLocation, true);

            /* WHEN */
            encoder.Encode(new DirectoryPath(recordingFolderLocation, true));

            // As the input queue is correctly attached we can now queue our events.
            Assert.IsTrue(input.ConsumerAttachedEvent.WaitOne(maxWaitTime), "Encoder did not attach to input queue in time!");

            // We enqueue now all events we want to serialize.
            foreach (var @event in eventList)
            {
                var sample = new JsonIntermediateFormatSample()
                {
                    Data          = JsonSerializer.SerializeToUtf8Bytes(@event),
                    IssuingModule = @event.IssuingModule,
                    Timestamp     = @event.Timestamp,
                    Type          = @event.GetType()
                };

                input.Enqueue(sample);
            }

            input.Close();

            Assert.IsTrue(encoder.EncodeFinished.WaitOne(maxWaitTime), "Encoder did not finish in time!");

            /* THEN */
            // We get the encoded string which the encoder produced.
            const string path = recordingFolderLocation + fileLocation;

            Assert.IsTrue(fileSystem.File.Exists(path));

            var encodedString      = fileSystem.File.ReadAllText(recordingFolderLocation + fileLocation, Encoding.Default);
            var plusResolvedString = encodedString.Replace(@"\u002B", "+");

            Assert.AreEqual(serializedEvent, plusResolvedString);
        }
Exemplo n.º 2
0
        public void TestJsonIntermediateFormatDeserializer_SetActive()
        {
            /* PRECONDITION */
            Debug.Assert(inputQueue != null);
            Debug.Assert(outputQueue != null);
            Debug.Assert(deserializer != null);
            Debug.Assert(container != null);
            Debug.Assert(!deserializer.IsActive);

            /* GIVEN */
            // Load all MEF instances correctly
            container.ComposeExportedValue <ISupportDeserializationEventQueue <Event> >(outputQueue);
            container.ComposeExportedValue <IDecodableEventQueue <JsonIntermediateFormatSample> >(inputQueue);
            container.ComposeParts(deserializer);

            const int identifier = 404;
            var       @event     = new TestEvent(identifier);

            using var outputReceivedEvent = new ManualResetEvent(false);
            var sample = new JsonIntermediateFormatSample
            {
                Data      = JsonSerializer.SerializeToUtf8Bytes(@event),
                Timestamp = @event.Timestamp,
                Type      = @event.GetType()
            };

            /* WHEN */
            deserializer.IsActive = true;
            Assert.IsTrue(deserializer.IsActive, "Deserializer did not activate correctly!");

            ExpectOutput(outputQueue, (received) =>
            {
                if (received == null || !received.Equals(@event))
                {
                    return(false);
                }
                outputReceivedEvent.Set();
                return(true);
            });

            inputQueue.Enqueue(sample);

            /* THEN */
            Assert.IsTrue(outputReceivedEvent.WaitOne(maxWaitTime), "Did not receive serialized event in time!");
        }
Exemplo n.º 3
0
        public void TestJsonIntermediateFormatDeserializer_DeactivatesOnClose()
        {
            /* PRECONDITION */
            Debug.Assert(inputQueue != null);
            Debug.Assert(outputQueue != null);
            Debug.Assert(deserializer != null);
            Debug.Assert(container != null);
            Debug.Assert(!deserializer.IsActive);

            /* GIVEN */
            // Load all MEF instances correctly
            container.ComposeExportedValue <ISupportDeserializationEventQueue <Event> >(outputQueue);
            container.ComposeExportedValue <IDecodableEventQueue <JsonIntermediateFormatSample> >(inputQueue);
            container.ComposeParts(deserializer);

            var @event = new TestEvent();
            var sample = new JsonIntermediateFormatSample
            {
                Data      = JsonSerializer.SerializeToUtf8Bytes(@event),
                Timestamp = @event.Timestamp,
                Type      = @event.GetType()
            };

            using var outputReceivedEvent = new ManualResetEvent(false);
            using var didCloseEvent       = new ManualResetEvent(false);

            /* WHEN */
            deserializer.IsActive = true;
            Assert.IsTrue(deserializer.IsActive, "Deserializer did not activate correctly!");

            ExpectOutput(outputQueue, (_) => outputReceivedEvent.Set(), () => didCloseEvent.Set());

            inputQueue.Enqueue(sample);
            Assert.IsTrue(outputReceivedEvent.WaitOne(maxWaitTime), "Did not receive serialized event in time!");

            inputQueue.Close();

            /* THEN */
            Assert.IsTrue(inputQueue.IsClosed, "InputQueue failed to close!");
            Assert.IsTrue(didCloseEvent.WaitOne(maxWaitTime), "Did not close output queue in time!");
        }