コード例 #1
0
        public void TestJsonDecoder_ParseSuccess()
        {
            /* PRECONDITION */
            Debug.Assert(decoder != null);
            Debug.Assert(fileSystem != null);
            Debug.Assert(config != null);
            Debug.Assert(container != null);

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

            container.ComposeExportedValue(config);
            container.ComposeParts(decoder);

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

            var serializedString = JsonManualSerialization.GenerateSerializedEvents(eventList);
            var mockFileData     = new MockFileData(serializedString, Encoding.UTF8);

            fileSystem.AddFile(recordingFolderLocation + fileLocation, mockFileData);

            using var awaitThreadEvent   = new ManualResetEvent(false);
            using var openEvent          = new ManualResetEvent(false);
            using var didDecodeAllEvents = new ManualResetEvent(false);

            /* WHEN */
            var thread = new Thread(async() =>
            {
                await foreach (var @event in Awaitable.Await(decoder.GetEvents(), awaitThreadEvent))
                {
                    var deserialized = JsonSerializer.Deserialize(@event.Data, @event.Type);

                    if ((!(deserialized is TestEvent receivedEvent)))
                    {
                        continue;
                    }

                    var index = eventList.FindIndex((compareEvent) => compareEvent.Identifier == receivedEvent.Identifier);
                    if (index == -1)
                    {
                        continue;
                    }

                    eventList.RemoveAt(index);

                    if (eventList.Count == 0)
                    {
                        didDecodeAllEvents.Set();
                    }
                }
            });

            decoder.Open();
            thread.Start();
            Assert.IsTrue(awaitThreadEvent.WaitOne(maxWaitTime), "Thread did not start in time!");

            decoder.Decode(new DirectoryPath(recordingFolderLocation, true));

            /* THEN */
            Assert.IsTrue(decoder.DecodeFinished.WaitOne(maxWaitTime), "Decoder did not finish in time!");
            Assert.IsTrue(didDecodeAllEvents.WaitOne(maxWaitTime), "Could not find all encoded events.");
        }