static IMessage MakeMessageInternal( TextMessageCapture capture, IRegex headRe, IRegex bodyRe, ref IMatch bodyMatch, IFieldsProcessor fieldsProcessor, MakeMessageFlags makeMessageFlags, DateTime sourceTime, ITimeOffsets timeOffsets, MessagesBuilderCallback threadLocalCallbackImpl ) { if (bodyRe != null) { if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyMatch)) { return(null); } } int idx = 0; Group[] groups; fieldsProcessor.Reset(); fieldsProcessor.SetSourceTime(sourceTime); fieldsProcessor.SetPosition(capture.BeginPosition); fieldsProcessor.SetTimeOffsets(timeOffsets); groups = capture.HeaderMatch.Groups; for (int i = 1; i < groups.Length; ++i) { var g = groups[i]; fieldsProcessor.SetInputField(idx++, new StringSlice(capture.HeaderBuffer, g.Index, g.Length)); } if (bodyRe != null) { groups = bodyMatch.Groups; for (int i = 1; i < groups.Length; ++i) { var g = groups[i]; fieldsProcessor.SetInputField(idx++, new StringSlice(capture.BodyBuffer, g.Index, g.Length)); } } else { fieldsProcessor.SetInputField(idx++, new StringSlice(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength)); } threadLocalCallbackImpl.SetCurrentPosition(capture.BeginPosition, capture.EndPosition); IMessage ret; ret = fieldsProcessor.MakeMessage(threadLocalCallbackImpl, makeMessageFlags); ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim()); return(ret); }
public void MessageIsNotReadBecauseItStartsAtTheEndOfTheRange_Forward() { // reading from position 0 with range 0-6 // _msg1_msg2_msg3 // | | // 0 6 // range ends at pos 6 that is past-the-end position. msg2 shouldn't be read. ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"msg", ReOptions.None)); ta.OpenIterator(0, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(0).Returns(0); it.CharIndexToPosition(1).Returns((long)1); it.CurrentBuffer.Returns("_msg1_msg2_msg3"); target.BeginSplittingSession(new Range(0, 6), 0, MessagesParserDirection.Forward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(6).Returns((long)6); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("1_", capt.MessageBody); Assert.AreEqual(1L, capt.BeginPosition); Assert.AreEqual(6L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); }
public void HeaderRegexMatchesPartOfAMessage_Forward() { var capt = new TextMessageCapture(); ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.AverageBufferLength.Returns(100); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"ab(c)?", ReOptions.None), MessagesSplitterFlags.PreventBufferUnderflow); ta.OpenIterator(0, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(0).Returns(0); it.CurrentBuffer.Returns("ab"); it.Advance(0).Returns(_ => { it.CurrentBuffer.Returns("abc_"); it.CharIndexToPosition(0).Returns((long)0); return(true); }); target.BeginSplittingSession(new Range(0, 10), 0, MessagesParserDirection.Forward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(3).Returns(false); it.CharIndexToPosition(4).Returns((long)3); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.MessageHeader); Assert.AreEqual("_", capt.MessageBody); Assert.IsTrue(target.CurrentMessageIsEmpty); }
public void FirstTextBufferIsEmpty_Backward() { var capt = new TextMessageCapture(); int aveBufSize = 100; ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); ta.AverageBufferLength.Returns(aveBufSize); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None), MessagesSplitterFlags.PreventBufferUnderflow); // _abc // || | // pos: 11| 15 // 12 ta.OpenIterator(15, TextAccessDirection.Forward).Returns(it); it.CurrentBuffer.Returns(""); it.PositionToCharIndex(15).Returns(0); // querying past-the end position is allowed it.Advance(0).Returns(_ => { it.CurrentBuffer.Returns("_abc"); it.CharIndexToPosition(1).Returns((long)12); return(true); }); target.BeginSplittingSession(new Range(10, 20), 15, MessagesParserDirection.Forward); Assert.IsFalse(target.CurrentMessageIsEmpty); }
public void StartBackwardReadingFromAlmostEndPosition() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); ta.OpenIterator(99, TextAccessDirection.Backward).Returns(it); it.PositionToCharIndex(99).Returns(28); it.CurrentBuffer.Returns( "123456 abc 283147948 abc 3498"); // | | | | | || // 0 7 10 21 24 28\29 - char idx // 50 61 67 85 87 99\100 - position it.CharIndexToPosition(21).Returns((long)85); target.BeginSplittingSession(new Range(0, 100), 99, MessagesParserDirection.Backward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(7).Returns((long)61); it.CharIndexToPosition(28).Returns((long)99); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.MessageHeader); Assert.AreEqual(" 349", capt.MessageBody); Assert.AreEqual(85L, capt.BeginPosition); Assert.AreEqual(99L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); }
public void GetCurrentMessageAndMoveToNextOneWhenNoOpenSession() { var ta = Substitute.For <ITextAccess>(); ta.MaximumSequentialAdvancesAllowed.Returns(100); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); TextMessageCapture capt = new TextMessageCapture(); Assert.Throws <InvalidOperationException>(() => { target.GetCurrentMessageAndMoveToNextOne(capt); }); }
public void MessageIsNotReadBecauseItEndsAtTheBeginningOfTheRange_Backward() { MockRepository repo = new MockRepository(); // reading from position 11 with range 6-15 // _msg1_msg2_msg3 // | | | | // 1 6 11 15 // range begins at pos 6. msg1_ ends at pos 6 (its past-the-end position = 6). msg1_ shouldn't be read. ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"msg", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(11, TextAccessDirection.Backward)).Return(it); Expect.Call(it.PositionToCharIndex(11)).Return(11); Expect.Call(it.CharIndexToPosition(6)).Return((long)6); Expect.Call(it.CurrentBuffer).Return("_msg1_msg2_msg3"); repo.ReplayAll(); target.BeginSplittingSession(new Range(6, 15), 11, MessagesParserDirection.Backward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.CharIndexToPosition(11)).Return((long)11); Expect.Call(it.CharIndexToPosition(1)).Return((long)1); repo.ReplayAll(); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("2_", capt.MessageBody); Assert.AreEqual(6L, capt.BeginPosition); Assert.AreEqual(11L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); }
public async Task MessageIsReadBecauseItEndsRightAfterTheBeginningOfTheRange_Backward() { // reading from position 11 with range 5-15 // _msg1_msg2_msg3 // | | | | // 1 5 11 15 // range begins at pos 5. msg1_ ends at pos 6 (its past-the-end position = 6). msg1_ must be read. ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"msg", ReOptions.None)); ta.OpenIterator(11, TextAccessDirection.Backward).Returns(it); it.PositionToCharIndex(11).Returns(11); it.CharIndexToPosition(6).Returns((long)6); it.CurrentBuffer.Returns("_msg1_msg2_msg3"); await target.BeginSplittingSession(new Range(5, 15), 11, MessagesParserDirection.Backward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(11).Returns((long)11); it.CharIndexToPosition(1).Returns((long)1); var capt = new TextMessageCapture(); Assert.IsTrue(await target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("2_", capt.MessageBody); Assert.AreEqual(6L, capt.BeginPosition); Assert.AreEqual(11L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); // in backward mode the first message that was read is "IsLastMessage" Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(9).Returns(_ => { it.CurrentBuffer.Returns("_msg1_"); return(trueTask); }); Assert.IsTrue(await target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("1_", capt.MessageBody); Assert.AreEqual(1L, capt.BeginPosition); Assert.AreEqual(6L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); Assert.IsFalse(await target.GetCurrentMessageAndMoveToNextOne(capt)); }
public void GetCurrentMessageAndMoveToNextOneWhenNoOpenSession() { MockRepository repo = new MockRepository(); var ta = repo.CreateMock <ITextAccess>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(100); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); TextMessageCapture capt = new TextMessageCapture(); Assert.Throws <InvalidOperationException>(() => { target.GetCurrentMessageAndMoveToNextOne(capt); }); }
public void HeaderRegexMatchesPartOfAMessage_Forward() { MockRepository repo = new MockRepository(); var capt = new TextMessageCapture(); ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.AverageBufferLength).Return(100); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"ab(c)?", ReOptions.None), MessagesSplitterFlags.PreventBufferUnderflow); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(0, TextAccessDirection.Forward)).Return(it); Expect.Call(it.PositionToCharIndex(0)).Return(0); Expect.Call(it.CurrentBuffer).Return("ab"); Expect.Call(it.Advance(0)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return("abc_"); Expect.Call(it.CharIndexToPosition(0)).Return((long)0); repo.Replay(it); return(true); }); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 10), 0, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.Advance(3)).Return(false); Expect.Call(it.CharIndexToPosition(4)).Return((long)3); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.MessageHeader); Assert.AreEqual("_", capt.MessageBody); Assert.IsTrue(target.CurrentMessageIsEmpty); }
public void BeginSplittingSession_WithStartPositionOutOfRange() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); target.BeginSplittingSession(new Range(0, 100), 110, MessagesParserDirection.Forward); Assert.IsTrue(target.CurrentMessageIsEmpty); TextMessageCapture capt = new TextMessageCapture(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); }
public void FirstTextBufferIsEmpty_Backward() { MockRepository repo = new MockRepository(); var capt = new TextMessageCapture(); int aveBufSize = 100; ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); Expect.Call(ta.AverageBufferLength).Return(aveBufSize); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None), MessagesSplitterFlags.PreventBufferUnderflow); repo.VerifyAll(); // _abc // || | // pos: 11| 15 // 12 repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(15, TextAccessDirection.Forward)).Return(it); Expect.Call(it.CurrentBuffer).Return(""); Expect.Call(it.PositionToCharIndex(15)).Return(0); // querying past-the end position is allowed Expect.Call(it.Advance(0)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return("_abc"); Expect.Call(it.CharIndexToPosition(1)).Return((long)12); repo.Replay(it); return(true); }); repo.ReplayAll(); target.BeginSplittingSession(new Range(10, 20), 15, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); }
public void BeginSplittingSession_WithStartPositionThatDoesntGetMappedToCharacterByTextAccess() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); ta.OpenIterator(90, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(90).Returns(_ => throw new ArgumentOutOfRangeException()); target.BeginSplittingSession(new Range(0, 100), 90, MessagesParserDirection.Forward); it.Received(1).Dispose(); Assert.IsTrue(target.CurrentMessageIsEmpty); TextMessageCapture capt = new TextMessageCapture(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); }
public void BeginSplittingSession_WithStartPositionThatDoesntGetMappedToCharacterByTextAccess() { MockRepository repo = new MockRepository(); ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(90, TextAccessDirection.Forward)).Return(it); Expect.Call(it.PositionToCharIndex(90)).Throw(new ArgumentOutOfRangeException()); it.Dispose(); Expect.On(it); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 100), 90, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); TextMessageCapture capt = new TextMessageCapture(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); target.EndSplittingSession(); repo.VerifyAll(); }
public void BeginSplittingSession_WithStartPositionOutOfRange() { MockRepository repo = new MockRepository(); ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"111", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 100), 110, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); TextMessageCapture capt = new TextMessageCapture(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); target.EndSplittingSession(); repo.VerifyAll(); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Forward() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); ta.OpenIterator(0, TextAccessDirection.Forward).Returns(it); it.PositionToCharIndex(0).Returns(0); it.CurrentBuffer.Returns( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 28 - char idx // 0 8 16 45 56 67 - positions it.CharIndexToPosition(7).Returns((long)8); target.BeginSplittingSession(new Range(0, 100), 0, MessagesParserDirection.Forward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(21).Returns((long)45); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(8L, capt.BeginPosition); Assert.AreEqual(45L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(24).Returns(_ => { it.CurrentBuffer.Returns( " 3498 abc 2626277"); // | | | // 0 6 9 - char idx // 56 72 81 - position it.CharIndexToPosition(6).Returns((long)72); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(45L, capt.BeginPosition); Assert.AreEqual(72L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(9).Returns(_ => { it.CurrentBuffer.Returns( " 2626277"); // | | // 0 8 - char idx // 81 90 - position it.CharIndexToPosition(8).Returns((long)90); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 2626277", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(72L, capt.BeginPosition); Assert.AreEqual(90L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); it.Received(1).Dispose(); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Backward() { MockRepository repo = new MockRepository(); ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); repo.BackToRecordAll(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(100, TextAccessDirection.Backward)).Return(it); Expect.Call(it.PositionToCharIndex(100)).Return(29); Expect.Call(it.CurrentBuffer).Return( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 29 - char idx // 50 61 67 85 87 100 - position Expect.Call(it.CharIndexToPosition(21)).Return((long)85); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 100), 100, MessagesParserDirection.Backward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.CharIndexToPosition(7)).Return((long)61); Expect.Call(it.CharIndexToPosition(29)).Return((long)100); repo.ReplayAll(); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(85L, capt.BeginPosition); Assert.AreEqual(100L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.Advance(8)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return( "11 abc 123456 abc 283147948 "); // | | | | | | // 0 3 6 14 17 27 - char idx // 20 33 50 61 67 85 - positions Expect.Call(it.CharIndexToPosition(3)).Return((long)33); repo.Replay(it); return(true); }); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(61L, capt.BeginPosition); Assert.AreEqual(85L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.Advance(14)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return( "11 abc 123456 "); // | | | | // 0 3 6 13 - char idx // 20 33 61 - pos repo.Replay(it); return(true); }); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 123456 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(33L, capt.BeginPosition); Assert.AreEqual(61L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); it.Dispose(); Expect.On(it); repo.ReplayAll(); target.EndSplittingSession(); repo.VerifyAll(); }
protected override IMessage MakeMessage(TextMessageCapture capture) { return(MakeMessageInternal(capture, reader.formatInfo, bodyRegex, ref bodyMatch, callback, reader.transformArgs, media.LastModified, reader.TimeOffsets)); }
public void MessageIsReadBecauseItStartsRightBeforeTheEndOfTheRange_Forward() { MockRepository repo = new MockRepository(); // reading from position 0 with range 0-7 // _msg1_msg2_msg3 // | | | // 0 7 11 // range ends at pos 7. msg2 starts at pos 6. msg2 must be read. ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"msg", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(0, TextAccessDirection.Forward)).Return(it); Expect.Call(it.PositionToCharIndex(0)).Return(0); Expect.Call(it.CharIndexToPosition(1)).Return((long)1); Expect.Call(it.CurrentBuffer).Return("_msg1_msg2_msg3"); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 7), 0, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.CharIndexToPosition(6)).Return((long)6); repo.ReplayAll(); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("1_", capt.MessageBody); Assert.AreEqual(1L, capt.BeginPosition); Assert.AreEqual(6L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.CharIndexToPosition(11)).Return((long)11); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("msg", capt.MessageHeader); Assert.AreEqual("2_", capt.MessageBody); Assert.AreEqual(6L, capt.BeginPosition); Assert.AreEqual(11L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Backward() { ITextAccess ta = Substitute.For <ITextAccess>(); ITextAccessIterator it = Substitute.For <ITextAccessIterator>(); ta.MaximumSequentialAdvancesAllowed.Returns(3); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); ta.OpenIterator(100, TextAccessDirection.Backward).Returns(it); it.PositionToCharIndex(100).Returns(29); it.CurrentBuffer.Returns( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 29 - char idx // 50 61 67 85 87 100 - position it.CharIndexToPosition(21).Returns((long)85); target.BeginSplittingSession(new Range(0, 100), 100, MessagesParserDirection.Backward); Assert.IsFalse(target.CurrentMessageIsEmpty); it.CharIndexToPosition(7).Returns((long)61); it.CharIndexToPosition(29).Returns((long)100); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(85L, capt.BeginPosition); Assert.AreEqual(100L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(8).Returns(_ => { it.CurrentBuffer.Returns( "11 abc 123456 abc 283147948 "); // | | | | | | // 0 3 6 14 17 27 - char idx // 20 33 50 61 67 85 - positions it.CharIndexToPosition(3).Returns((long)33); return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(61L, capt.BeginPosition); Assert.AreEqual(85L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); it.Advance(14).Returns(_ => { it.CurrentBuffer.Returns( "11 abc 123456 "); // | | | | // 0 3 6 13 - char idx // 20 33 61 - pos return(true); }); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 123456 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(33L, capt.BeginPosition); Assert.AreEqual(61L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); Assert.IsTrue(target.CurrentMessageIsEmpty); target.EndSplittingSession(); it.Received(1).Dispose(); }
public override IMessage MakeMessage(TextMessageCapture capture, ProcessingThreadLocalData threadLocal) { return(MakeMessageInternal(capture, threadLocal.headRe.Regex, threadLocal.bodyRe.Regex, ref threadLocal.bodyMatch, threadLocal.fieldsProcessor, flags, media.LastModified, reader.TimeOffsets, threadLocal.callback)); }
static IMessage MakeMessageInternal(TextMessageCapture capture, JsonFormatInfo formatInfo, IRegex bodyRe, ref IMatch bodyReMatch, MessagesBuilderCallback callback, DateTime sourceTime, ITimeOffsets timeOffsets) { StringBuilder messageBuf = new StringBuilder(); messageBuf.Append(capture.HeaderBuffer, capture.HeaderMatch.Index, capture.HeaderMatch.Length); if (bodyRe != null) { if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyReMatch)) { return(null); } messageBuf.Append(capture.BodyBuffer, bodyReMatch.Index, bodyReMatch.Length); } else { messageBuf.Append(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength); } callback.SetCurrentPosition(capture.BeginPosition, capture.EndPosition); string messageStr = messageBuf.ToString(); var transfromed = JsonTransformer.Transform( formatInfo.Transform.DeepClone() as JObject, JObject.Parse(messageStr) ); var d = transfromed.Property("d")?.Value; DateTime date; if (d != null && d.Type == JTokenType.String) { date = DateTime.Parse(d.ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind); } else if (d != null && d.Type == JTokenType.Date) { date = (DateTime)((JValue)d).Value; } else { throw new Exception("Bad time property \"d\""); } var t = transfromed.Property("t")?.Value?.ToString(); var m = transfromed.Property("m")?.Value; var msg = ""; if (m != null) { msg = m.ToString(); } var s = transfromed.Property("s")?.Value?.ToString(); var sev = !string.IsNullOrEmpty(s) ? char.ToLower(s[0]) : 'i'; IMessage ret = new Content( capture.BeginPosition, capture.EndPosition, callback.GetThread(new StringSlice(t ?? "")), new MessageTimestamp(date), new StringSlice(msg), sev == 'i' ? SeverityFlag.Info : sev == 'e' ? SeverityFlag.Error : sev == 'w' ? SeverityFlag.Warning : SeverityFlag.Info ); if (formatInfo.ViewOptions.RawViewAllowed) { ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim()); } if (formatInfo.ViewOptions.WrapLineLength.HasValue) { ret.WrapsTexts(formatInfo.ViewOptions.WrapLineLength.Value); } return(ret); }
public void GetCurrentMessageAndMoveToNextOne_MainScenario_Forward() { MockRepository repo = new MockRepository(); ITextAccess ta = repo.CreateMock <ITextAccess>(); ITextAccessIterator it = repo.CreateMock <ITextAccessIterator>(); Expect.Call(ta.MaximumSequentialAdvancesAllowed).Return(3); repo.ReplayAll(); MessagesSplitter target = new MessagesSplitter(ta, reFactory.Create(@"abc", ReOptions.None)); repo.VerifyAll(); repo.BackToRecordAll(); Expect.Call(ta.OpenIterator(0, TextAccessDirection.Forward)).Return(it); Expect.Call(it.PositionToCharIndex(0)).Return(0); Expect.Call(it.CurrentBuffer).Return( "123456 abc 283147948 abc 3498"); // | | | | | | // 0 7 10 21 24 28 - char idx // 0 8 16 45 56 67 - positions Expect.Call(it.CharIndexToPosition(7)).Return((long)8); repo.ReplayAll(); target.BeginSplittingSession(new Range(0, 100), 0, MessagesParserDirection.Forward); repo.VerifyAll(); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.CharIndexToPosition(21)).Return((long)45); repo.ReplayAll(); var capt = new TextMessageCapture(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 283147948 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(8L, capt.BeginPosition); Assert.AreEqual(45L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.Advance(24)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return( " 3498 abc 2626277"); // | | | // 0 6 9 - char idx // 56 72 81 - position Expect.Call(it.CharIndexToPosition(6)).Return((long)72); repo.Replay(it); return(true); }); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 3498 ", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(45L, capt.BeginPosition); Assert.AreEqual(72L, capt.EndPosition); Assert.IsFalse(capt.IsLastMessage); Assert.IsFalse(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); Expect.Call(it.Advance(9)).Repeat.Once().Do((Predicate <int>) delegate(int i) { repo.Verify(it); repo.BackToRecord(it); Expect.Call(it.CurrentBuffer).Return( " 2626277"); // | | // 0 8 - char idx // 81 90 - position Expect.Call(it.CharIndexToPosition(8)).Return((long)90); repo.Replay(it); return(true); }); repo.ReplayAll(); Assert.IsTrue(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.AreEqual("abc", capt.HeaderBuffer.Substring(capt.HeaderMatch.Index, capt.HeaderMatch.Length)); Assert.AreEqual(" 2626277", capt.BodyBuffer.Substring(capt.BodyIndex, capt.BodyLength)); Assert.AreEqual(72L, capt.BeginPosition); Assert.AreEqual(90L, capt.EndPosition); Assert.IsTrue(capt.IsLastMessage); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); repo.ReplayAll(); Assert.IsFalse(target.GetCurrentMessageAndMoveToNextOne(capt)); repo.VerifyAll(); Assert.IsTrue(target.CurrentMessageIsEmpty); repo.BackToRecordAll(); it.Dispose(); Expect.On(it); repo.ReplayAll(); target.EndSplittingSession(); repo.VerifyAll(); }
protected override IMessage MakeMessage(TextMessageCapture capture) { return(MakeMessageInternal(capture, headerRegex, bodyRegex, ref bodyMatch, fieldsProcessor, currentParserFlags, media.LastModified, reader.TimeOffsets, callback)); }
protected override async Task LiveLogListen(CancellationToken stopEvt, LiveLogXMLWriter output) { using (ILogMedia media = await SimpleFileMedia.Create( host.FileSystem, SimpleFileMedia.CreateConnectionParamsFromFileName(fileName))) using (FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(fileName), Path.GetFileName(fileName))) using (AutoResetEvent fileChangedEvt = new AutoResetEvent(true)) { IMessagesSplitter splitter = new MessagesSplitter( new StreamTextAccess(media.DataStream, Encoding.ASCII, TextStreamPositioningParams.Default), host.RegexFactory.Create(@"^(?<body>.+)$", ReOptions.Multiline) ); watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size; watcher.Changed += delegate(object sender, FileSystemEventArgs e) { fileChangedEvt.Set(); }; //watcher.EnableRaisingEvents = true; long lastLinePosition = 0; long lastStreamLength = 0; WaitHandle[] events = new WaitHandle[] { stopEvt.WaitHandle, fileChangedEvt }; var capture = new TextMessageCapture(); for (; ;) { if (WaitHandle.WaitAny(events, 250, false) == 0) { break; } await media.Update(); if (media.Size == lastStreamLength) { continue; } lastStreamLength = media.Size; sizeInBytesStat = lastStreamLength; DateTime lastModified = media.LastModified; await splitter.BeginSplittingSession(new FileRange.Range(0, lastStreamLength), lastLinePosition, MessagesParserDirection.Forward); try { for (; ;) { if (!await splitter.GetCurrentMessageAndMoveToNextOne(capture)) { break; } lastLinePosition = capture.BeginPosition; XmlWriter writer = output.BeginWriteMessage(false); writer.WriteStartElement("m"); writer.WriteAttributeString("d", Listener.FormatDate(lastModified)); writer.WriteString(XmlUtils.RemoveInvalidXMLChars(capture.MessageHeader)); writer.WriteEndElement(); output.EndWriteMessage(); } } finally { splitter.EndSplittingSession(); } } } }
static IMessage MakeMessageInternal(TextMessageCapture capture, XmlFormatInfo formatInfo, IRegex bodyRe, ref IMatch bodyReMatch, MessagesBuilderCallback callback, XsltArgumentList transformArgs, DateTime sourceTime, ITimeOffsets timeOffsets) { int nrOfSequentialFailures = 0; int maxNrOfSequentialFailures = 10; for (; ;) { StringBuilder messageBuf = new StringBuilder(); messageBuf.AppendFormat("<root {0}>", formatInfo.NSDeclaration.ToString()); messageBuf.Append(capture.HeaderBuffer, capture.HeaderMatch.Index, capture.HeaderMatch.Length); if (bodyRe != null) { if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyReMatch)) { return(null); } messageBuf.Append(capture.BodyBuffer, bodyReMatch.Index, bodyReMatch.Length); } else { messageBuf.Append(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength); } messageBuf.Append("</root>"); callback.SetCurrentPosition(capture.BeginPosition, capture.EndPosition); //this.owner.xslExt.SetSourceTime(this.owner.MediaLastModified); todo? string messageStr = messageBuf.ToString(); using (FactoryWriter factoryWriter = new FactoryWriter(callback, timeOffsets)) using (XmlReader xmlReader = XmlReader.Create(new StringReader(messageStr), xmlReaderSettings)) { try { if (formatInfo.IsNativeFormat) { factoryWriter.WriteNode(xmlReader, false); } else { formatInfo.Transform.Transform(xmlReader, transformArgs, factoryWriter); } nrOfSequentialFailures = 0; } catch (XmlException) { if (capture.IsLastMessage) { // There might be incomplete XML at the end of the stream. Ignore it. return(null); } else { if (nrOfSequentialFailures < maxNrOfSequentialFailures) { ++nrOfSequentialFailures; // Try to parse the next messsage if it's not the end of the stream continue; } else { throw; } } } var ret = factoryWriter.GetOutput(); if (ret == null) { throw new XsltException( "Normalization XSLT produced no output"); } if (formatInfo.ViewOptions.RawViewAllowed) { ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim()); } if (formatInfo.ViewOptions.WrapLineLength.HasValue) { ret.WrapsTexts(formatInfo.ViewOptions.WrapLineLength.Value); } return(ret); } } }
public abstract IMessage MakeMessage(TextMessageCapture capture, UserThreadLocalData threadLocal);
public override IMessage MakeMessage(TextMessageCapture capture, ProcessingThreadLocalData threadLocal) { return(MakeMessageInternal(capture, reader.formatInfo, threadLocal.bodyRe.Regex, ref threadLocal.bodyMatch, threadLocal.callback, reader.transformArgs, media.LastModified, reader.TimeOffsets)); }
protected abstract IMessage MakeMessage(TextMessageCapture capture);