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 void GivenInValidWithNoThrow_Parse_DoesNotThrow() { var uut = new MessageParser(Mock.Of <ITagToPropertyMapper>(), Mock.Of <IPropertySetter>(), new MockValidator(false), Mock.Of <MessageParserOptions>()); var result = uut.Parse <TestTypeParent>(TestFixMessageBuilder.CreateDefaultMessage()); Assert.Null(result); }
public void GivenNoMessageContext_IsValid_ThrowsArgumentNullException() { var uut = new ValidatorCollection(IntegerToFixConverter.Instance); var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(); Assert.Throws <ArgumentNullException>(() => uut.PreValidate(message, null)); }
public async Task GivenStreamedMessages_Observable_PushesParsedMessages() { using (var stream = new MemoryStream()) { var mockMessageParser = new MockMessageParser(); int messages = 0; var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5)); TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); for (int i = 0; i < 10; i++) { foreach (var sampleParams in SampleFixMessagesSource.GetTestTypeParentMessageBodies()) { byte[] msg = new TestFixMessageBuilder(sampleParams[0] as string).Build(); await stream.WriteAsync(msg); } } stream.Position = 0; var uut = new StreamParser <TestTypeParent>(stream, mockMessageParser, SupportedFixVersion.Fix44); uut.Subscribe(parsedObject => messages++, ex => taskCompletion.SetResult(false), () => taskCompletion.SetResult(true)); var listener = Task.Run(() => uut.ListenAsync(cancellation.Token)); Assert.True(await taskCompletion.Task); Assert.Equal(10 * SampleFixMessagesSource.GetTestTypeParentMessageBodies().Count(), messages); await listener; } }
public void GivenNoChecksum_Validate_ReturnsFalse() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).AddChecksum("3").Build(); var uut = new ChecksumValidator(IntegerToFixConverter.Instance); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.False(result); }
public void GivenIncorrectLength_Validate_ReturnsFalse() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).AddLength("023").Build(); var uut = new LengthValidator(IntegerToFixConverter.Instance); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.False(result); }
public void GivenCorrectLength_Validate_ReturnsTrue(string input) { byte[] message = new TestFixMessageBuilder(input).Build(); var uut = new LengthValidator(IntegerToFixConverter.Instance); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.True(result); }
public void GivenFix5WithNoSenderCompId_Validate_ReturnsFalse() { var message = new TestFixMessageBuilder("35=A|56=CLIENT|34=177|52=20090107-18:15:16|98=0|108=30|").AddBeginString(SupportedFixVersion.Fix50).Build(); var uut = new FixVersionValidator(); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.False(result); }
public void GivenInValidWithThrow_Parse_ThrowsArgumentException() { var uut = new MessageParser(Mock.Of <ITagToPropertyMapper>(), Mock.Of <IPropertySetter>(), new MockValidator(false), new MessageParserOptions() { ThrowIfInvalidMessage = true }); Assert.Throws <ArgumentException>(() => uut.Parse <TestTypeParent>(TestFixMessageBuilder.CreateDefaultMessage())); }
public void GivenNoBeginString_Validate_ReturnsFalse() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).AddBeginString(string.Empty).Build(); var uut = new FixVersionValidator(); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.False(result); }
public void GivenValidFixVersion_Validate_ReturnsTrue(string input) { byte[] message = new TestFixMessageBuilder(input).Build(); var uut = new FixVersionValidator(); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Assert.True(result); }
public void GivenSampleMessage_Setup_ReturnsMessageContext() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(); var uut = new FixMessageContext(); var result = uut.Setup(message.AsSpan()); Assert.NotEqual(-1, result.ChecksumTagStartIndex); Assert.NotEqual(-1, result.LengthTagStartIndex); Assert.NotEqual(-1, result.MessageTypeTagStartIndex); }
public void GivenMessage_Parse_ParsesReferenceType(string input) { byte[] message = new TestFixMessageBuilder(input).Build(); var uut = new TypedMessageParser <TestTypeParent>(new TagToPropertyMapper(new SubPropertySetterFactory()), new CompositePropertySetter(), new MockValidator(), Mock.Of <MessageParserOptions>()); var result = uut.Parse(message); Assert.NotNull(result); Assert.NotNull(result.Tag55); }
public void GivenMessageContext_IsValid_ReturnsTrue() { var uut = new ValidatorCollection(IntegerToFixConverter.Instance); var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(out byte checksum, out var dummy); var msgCtx = new FixMessageContext().Setup(message); msgCtx.ChecksumValue = checksum; Assert.True(uut.PreValidate(message, msgCtx)); Assert.True(uut.PostValidate(message, msgCtx)); }
public void GivenEncoding_Setup_ReturnsMessageContext() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(); var uut = new FixMessageContext(); var result = uut.Setup(message.AsSpan(), MessageEncoding.JIS); Assert.NotEqual(-1, result.ChecksumTagStartIndex); Assert.NotEqual(-1, result.LengthTagStartIndex); Assert.NotEqual(-1, result.MessageTypeTagStartIndex); Assert.Equal(MessageEncoding.JIS, result.EncodedFields); }
public void GivenCorrectChecksum_Validate_ReturnsTrue(string input) { byte[] message = new TestFixMessageBuilder(input).Build(out byte checksumValue, out int checksumStart); var uut = new ChecksumValidator(IntegerToFixConverter.Instance); var msgContext = new FixMessageContext() { ChecksumValue = checksumValue, ChecksumTagStartIndex = checksumStart }; var result = uut.IsValid(message.AsSpan(), msgContext); Assert.True(result); }
public void GivenMessage_Parse_ParsesObject() { var propertyMapper = new TagToPropertyMapper(new SubPropertySetterFactory()); propertyMapper.Map <TestTypeParent>(); var compositeSetter = new CompositePropertySetter(); var uut = new MessageParser(propertyMapper, compositeSetter, new MockValidator(), Mock.Of <MessageParserOptions>()); var result = uut.Parse <TestTypeParent>(TestFixMessageBuilder.CreateDefaultMessage()); Assert.NotNull(result); }
public void GivenSampleMessage_Parse_ParsesObject(string input) { byte[] message = new TestFixMessageBuilder(input).Build(); var propertyMapper = new TagToPropertyMapper(new SubPropertySetterFactory()); propertyMapper.Map <TestTypeParent>(); var uut = new MessageParser(propertyMapper, new CompositePropertySetter(), new MockValidator(), Mock.Of <MessageParserOptions>()); var result = uut.Parse(message) as TestTypeParent; Assert.NotNull(result); Assert.NotNull(result.Tag55); }
public void GivenMessageContext_SetupTwice_ResetsProperties() { var message = new TestFixMessageBuilder(TestFixMessageBuilder.DefaultBody).Build(); var uut = new FixMessageContext(); uut.CreatedParentTypes.Add(1); uut.RepeatingGroupCounters.Add(1, new FixMessageContext.RepeatingCounter(1)); var result = uut.Setup(message.AsSpan(), MessageEncoding.JIS).Setup(message.AsSpan(), MessageEncoding.EUC); Assert.NotEqual(-1, result.ChecksumTagStartIndex); Assert.NotEqual(-1, result.LengthTagStartIndex); Assert.NotEqual(-1, result.MessageTypeTagStartIndex); Assert.Equal(MessageEncoding.EUC, result.EncodedFields); Assert.Empty(result.RepeatingGroupCounters); Assert.Empty(result.CreatedParentTypes); }
public void GivenCorrectLength_Validate_Performance() { byte[] message = TestFixMessageBuilder.CreateDefaultMessage(); var uut = new LengthValidator(IntegerToFixConverter.Instance); var msgContext = new FixMessageContext().Setup(message); var result = uut.IsValid(message.AsSpan(), msgContext); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 100; i++) { result = uut.IsValid(message.AsSpan(), msgContext); } sw.Stop(); _output.WriteLine($"==================={sw.ElapsedMilliseconds}======================"); }
public async Task GivenStreamedMessage_Observable_PushesParsedMessages() { using (var stream = new MemoryStream()) { var mockMessageParser = new MockMessageParser(); int messages = 0; var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5)); TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>(); byte[] msg = TestFixMessageBuilder.CreateDefaultMessage(); await stream.WriteAsync(msg); stream.Position = 0; var uut = new StreamParser <TestTypeParent>(stream, mockMessageParser, SupportedFixVersion.Fix44); uut.Subscribe(parsedObject => messages++, ex => taskCompletion.SetResult(false), () => taskCompletion.SetResult(true)); var listener = Task.Run(() => uut.ListenAsync(cancellation.Token)); Assert.True(await taskCompletion.Task); Assert.Equal(1, messages); await listener; } }