public void TextWriterTraceListener_FindPrevMessagePositionTest() { var testLog = @"SampleApp Information: 0 : No free data file found. Going sleep. ProcessId=4756 ThreadId=7 DateTime=2011-07-12T12:10:00.0000000Z SampleApp Information: 0 : Searching for data files ProcessId=4756 ThreadId=7 DateTime=2011-07-12T12:12:00.0000000Z SampleApp Information: 0 : No free data file found. Going sleep. ProcessId=4756 ThreadId=7 DateTime=2011-07-12T12:14:00.0000000Z "; using (StringStreamMedia media = new StringStreamMedia(testLog, Encoding.ASCII)) using (ILogSourceThreadsInternal threads = new LogSourceThreads()) using (IPositionedMessagesReader reader = CreateFactory().CreateMessagesReader(new MediaBasedReaderParams(threads, media, new TempFilesManager(), new TraceSourceFactory()))) { reader.UpdateAvailableBounds(false); long?prevMessagePos = PositionedMessagesUtils.FindPrevMessagePosition(reader, 0x0000004A); Assert.IsTrue(prevMessagePos.HasValue); Assert.AreEqual(0, prevMessagePos.Value); } }
public async Task LocateDateBound_LowerRevBound_Test1() { TestReader reader = CreateTestReader1(); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(30), ValueBound.LowerReversed) == 30); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(8), ValueBound.LowerReversed) == 6); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(0), ValueBound.LowerReversed) == 0); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(-1), ValueBound.LowerReversed) == -1); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(-5), ValueBound.LowerReversed) == -1); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(50), ValueBound.LowerReversed) == 50); Assert.IsTrue(await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(55), ValueBound.LowerReversed) == 50); }
void TestSyncResults(Range cachedRange, params string[] expectedSyncResults) { IPositionedMessagesReader reader = new PositionedMessagesUtilsTests.TestReader(new long[] { 0, 10, 20, 30, 40, 50, 60 }); IMessage firstMsg, lastMsg; PositionedMessagesUtils.GetBoundaryMessages(reader, null, out firstMsg, out lastMsg); var availableTime = DateRange.MakeFromBoundaryValues( firstMsg.Time.ToLocalDateTime(), lastMsg.Time.ToLocalDateTime() ); var availableRange = new Range(reader.BeginPosition, reader.EndPosition); var ctx = new CommandContext() { Cache = new AsyncLogProviderDataCache() { Messages = new LogJoint.MessagesContainers.ListBasedCollection(), MessagesRange = cachedRange, }, Stats = new LogProviderStats() { PositionsRange = availableRange, AvailableTime = availableTime, }, Cancellation = CancellationToken.None, Preemption = CancellationToken.None, Reader = reader, Tracer = LJTraceSource.EmptyTracer }; using (var parser = reader.CreateParser(new CreateParserParams() { Direction = MessagesParserDirection.Forward, StartPosition = cachedRange.Begin, Range = cachedRange })) { for (; ;) { IMessage msg = parser.ReadNext(); if (msg == null) { break; } else { ctx.Cache.Messages.Add(msg); } } } TestSyncResults(ctx, expectedSyncResults); }
public async Task NormalizeMessagePosition_Test1() { TestReader reader = CreateTestReader1(); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 0) == 0); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 4) == 4); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 2) == 4); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 3) == 4); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, -1) == 0); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, -5) == 0); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 16) == 20); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 50) == 50); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 51) == 51); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 52) == 51); Assert.IsTrue(await PositionedMessagesUtils.NormalizeMessagePosition(reader, 888) == 51); }
public async Task ReadNearestDate_Test1() { TestReader reader = CreateTestReader1(); // Exact hit to a position Assert.AreEqual(TestReader.PositionToDate(5), (await PositionedMessagesUtils.ReadNearestMessageTimestamp(reader, 5)).Value.ToLocalDateTime()); // Needing to move forward to find the nearest position Assert.AreEqual(TestReader.PositionToDate(10), (await PositionedMessagesUtils.ReadNearestMessageTimestamp(reader, 7)).Value.ToLocalDateTime()); // Over-the-end position Assert.AreEqual(new MessageTimestamp?(), await PositionedMessagesUtils.ReadNearestMessageTimestamp(reader, 55)); //Begore the begin position Assert.AreEqual(TestReader.PositionToDate(0), (await PositionedMessagesUtils.ReadNearestMessageTimestamp(reader, -5)).Value.ToLocalDateTime()); }
public async Task LocateDateBound_UpperBound_Test1() { TestReader reader = CreateTestReader1(); Assert.AreEqual(16, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(15), ValueBound.Upper)); Assert.AreEqual(16, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(16), ValueBound.Upper)); Assert.AreEqual(1, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(0), ValueBound.Upper)); Assert.AreEqual(5, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(4), ValueBound.Upper)); // Before begin position Assert.AreEqual(0, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(-5), ValueBound.Upper)); // After end position Assert.AreEqual(51, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(55), ValueBound.Upper)); }
public async Task LocateDateBound_LowerBound_Test1() { TestReader reader = CreateTestReader1(); // Exact hit to a position Assert.AreEqual(5, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(5), ValueBound.Lower)); // Test that LowerBound returns the first (smallest) position that yields the messages with date <= date in question Assert.AreEqual(7, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(7), ValueBound.Lower)); Assert.AreEqual(7, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(8), ValueBound.Lower)); Assert.AreEqual(7, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(9), ValueBound.Lower)); // Before begin position Assert.AreEqual(0, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(-5), ValueBound.Lower)); // After end position Assert.AreEqual(51, await PositionedMessagesUtils.LocateDateBound(reader, TestReader.PositionToDate(55), ValueBound.Lower)); }
void TestSyncResults(CommandContext ctx, params string[] expectedSyncResults) { var cache = ctx.Cache; var cachedRange = cache.Messages.DatesRange; var fullLogRange = ctx.Stats.AvailableTime; var datesToTest = new[] { fullLogRange.Begin.AddSeconds(-1), fullLogRange.Begin.AddTicks(-1), fullLogRange.Begin, fullLogRange.Begin.AddTicks(+1), Mid(fullLogRange.Begin, cachedRange.Begin), cachedRange.Begin.AddTicks(-1), cachedRange.Begin, cachedRange.Begin.AddTicks(+1), Mid(cachedRange.Begin, cachedRange.End), cachedRange.End.AddTicks(-1), cachedRange.End, cachedRange.End.AddTicks(+1), Mid(cachedRange.End, fullLogRange.End), fullLogRange.End.AddTicks(-1), fullLogRange.End, fullLogRange.End.AddTicks(+1), fullLogRange.End.AddSeconds(+1) }; var boundsCache = Substitute.For <IDateBoundsCache>(); boundsCache.Get(new DateTime()).ReturnsForAnyArgs((DateBoundPositionResponseData)null); int dateIdx = 0; foreach (var dateToTest in datesToTest) { foreach (var bound in new[] { ListUtils.ValueBound.Lower, ListUtils.ValueBound.LowerReversed, ListUtils.ValueBound.Upper, ListUtils.ValueBound.UpperReversed }) { var testId = string.Format("{0}{1}", dateIdx, ToString(bound)); var cmd = new GetDateBoundCommand(dateToTest, false, bound, boundsCache); IAsyncLogProviderCommandHandler cmdIntf = cmd; DateBoundPositionResponseData syncResult = null; if (cmdIntf.RunSynchronously(ctx)) { cmdIntf.Complete(null); Assert.IsTrue(cmd.Task.IsCompleted); syncResult = cmd.Task.Result; } bool expectSyncResult = expectedSyncResults.IndexOf(i => i == testId) != null; Assert.AreEqual(expectSyncResult, syncResult != null, "Result must be sync for test " + testId); cmd = new GetDateBoundCommand(dateToTest, false, bound, boundsCache); cmdIntf = cmd; cmdIntf.ContinueAsynchronously(ctx); cmdIntf.Complete(null); Assert.IsTrue(cmd.Task.IsCompleted); DateBoundPositionResponseData asyncResult = cmd.Task.Result; if (syncResult != null) { Assert.AreEqual( PositionedMessagesUtils.NormalizeMessagePosition(ctx.Reader, syncResult.Position), PositionedMessagesUtils.NormalizeMessagePosition(ctx.Reader, asyncResult.Position), "Posision mismatch " + testId ); Assert.AreEqual(syncResult.IsBeforeBeginPosition, asyncResult.IsBeforeBeginPosition, "IsBeforeBeginPosition mismatch " + testId); Assert.AreEqual(syncResult.IsEndPosition, asyncResult.IsEndPosition, "IsEndPosition mismatch " + testId); } } ++dateIdx; } }
public async Task FindPrevMessagePosition_Test1() { TestReader reader = CreateTestReader1(); Assert.IsTrue(await PositionedMessagesUtils.FindPrevMessagePosition(reader, 1) == 0); }
public void FindPrevMessagePosition_Test1() { TestReader reader = CreateTestReader1(); Assert.IsTrue(PositionedMessagesUtils.FindPrevMessagePosition(reader, 1) == 0); }