Beispiel #1
0
        public async Task GivenPipedMessages_Observable_PushesParsedMessages()
        {
            var pipe = new Pipe();
            var mockMessageParser = new MockMessageParser();
            int messages          = 0;
            var cancellation      = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            var uut = new PipeParser <TestTypeParent>(pipe.Reader, mockMessageParser, SupportedFixVersion.Fix44);

            uut.Subscribe(parsedObject => messages++, ex => taskCompletion.SetResult(false), () => taskCompletion.SetResult(true));
            var listener = Task.Run(() => uut.ListenAsync(cancellation.Token));

            // 10 loops to make sure we have more message then the default buffer can hold.
            for (int i = 0; i < 10; i++)
            {
                foreach (var sampleParams in SampleFixMessagesSource.GetTestTypeParentMessageBodies())
                {
                    byte[] msg = new TestFixMessageBuilder(sampleParams[0] as string).Build();
                    await pipe.Writer.WriteAsync(msg);
                }
            }
            await pipe.Writer.FlushAsync();

            pipe.Writer.Complete();

            Assert.True(await taskCompletion.Task);
            Assert.Equal(10 * SampleFixMessagesSource.GetTestTypeParentMessageBodies().Count(), messages);
            await listener;
        }
        public async Task ParseMultipleMessages()
        {
            // Construct the pipe
            var pipe = new Pipe();

            // Create a message parser that can parser bytes into Order
            var messageParser = new ParserBuilder <Order>().Build <byte>(new MessageParserOptions()
            {
                ThrowIfInvalidMessage = false
            });

            // Create the piped parser, providing the pipe and an IMessageParser
            var parser = new PipeParser <Order>(pipe.Reader, messageParser, SupportedFixVersion.Fix44);

            // We subscribe to the observable to print the parsed messages
            parser.Subscribe(
                order => Console.WriteLine($"Order {order.Symbol} - Px {order.Price}, Qty {order.Quantity}"),
                ex => Console.WriteLine($"Error: {ex.Message}"));

            // We create sample messages and write them into the pipe asnyc
            var inputTask = Task.Run(() => CreateSimulatedInput(pipe));

            // Start observing and await until all messages observed
            await parser.ListenAsync();

            // Should be completed by now
            await inputTask;
        }
Beispiel #3
0
        public async Task GivenPipedMessage_Observable_PushesParsedMessages()
        {
            var pipe = new Pipe();
            var mockMessageParser = new MockMessageParser();
            int messages          = 0;
            var cancellation      = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            var uut = new PipeParser <TestTypeParent>(pipe.Reader, mockMessageParser, SupportedFixVersion.Fix44);

            uut.Subscribe(parsedObject => messages++, ex => taskCompletion.SetResult(false), () => taskCompletion.SetResult(true));
            var listener = Task.Run(() => uut.ListenAsync(cancellation.Token));

            byte[] msg = TestFixMessageBuilder.CreateDefaultMessage();
            await pipe.Writer.WriteAsync(msg);

            await pipe.Writer.FlushAsync();

            pipe.Writer.Complete();

            Assert.True(await taskCompletion.Task);
            Assert.Equal(1, messages);
            await listener;
        }