コード例 #1
0
        public void TestInitialPositionBOS(bool acknowledge)
        {
            var records  = new ListEventSink();
            var sourceId = "TestEvtInitialPositionTimeStamp";
            var eventId  = (int)(DateTime.Now.Ticks % ushort.MaxValue);
            var msg      = "A fresh message";

            // Write some events before the source is created
            for (int i = 0; i < 3; i++)
            {
                EventLog.WriteEntry(LogSource, msg, EventLogEntryType.Information, eventId);
            }

            var now = DateTime.UtcNow;

            using (var source = CreateSource(sourceId, eventId))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition = InitialPositionEnum.BOS;
                source.Start();

                for (int i = 0; i < 5; i++)
                {
                    EventLog.WriteEntry(LogSource, msg, EventLogEntryType.Information, eventId);
                }

                Thread.Sleep(5000);

                if (acknowledge)
                {
                    // Send the acknowledgements as if they had come from sources.
                    var bookmark = Assert.IsType <BookmarkInfo>(BookmarkManager.GetBookmark(sourceId));
                    BookmarkManager.SaveBookmark(bookmark.Id, records.Last().Position, null);
                }

                source.Stop();
                Thread.Sleep(1000);

                Assert.True(records[0].Timestamp < now, $"First record should have been written before {now}, but was written at {records[0].Timestamp}");

                var lastRecordTimestamp = records.Last().Timestamp;
                Assert.True(lastRecordTimestamp > now, $"Last record should have been written after {now}, but was written at {records.Last().Timestamp}");

                records.Clear();

                //Write some new logs after the source stop
                var newmsg = "A fresh message after source stop";
                EventLog.WriteEntry(LogSource, newmsg, EventLogEntryType.Information, eventId);
                Thread.Sleep(1000);
                source.Start();
                Thread.Sleep(3000);

                IEnvelope lastRecord;
                if (acknowledge)
                {
                    lastRecord = Assert.Single(records);
                }
                else
                {
                    Assert.Equal(9, records.Count);
                    lastRecord = records.Last();
                }

                Assert.True(lastRecord.Timestamp > lastRecordTimestamp);
                Assert.Matches("after source stop", lastRecord.GetMessage("string"));
            }
        }
コード例 #2
0
        public void TestInitialPositionBookMark(bool acknowledge)
        {
            var records  = new ListEventSink();
            var sourceId = "TestEvtInitialPositionBookMark";
            var eventId  = (int)(DateTime.Now.Ticks % ushort.MaxValue);
            var msg      = "A fresh message";

            // Write some events before the source is created
            for (int i = 0; i < 3; i++)
            {
                EventLog.WriteEntry(LogSource, msg, EventLogEntryType.Information, eventId);
            }

            var now = DateTime.UtcNow;

            using (var source = CreateSource(sourceId, eventId))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition = InitialPositionEnum.Bookmark;
                source.Start();

                Thread.Sleep(2000);

                // When using Bookmark as Initial position, and there is no bookmark, it should not process old events.
                Assert.Empty(records);

                EventLog.WriteEntry(LogSource, msg, EventLogEntryType.Information, eventId);

                Thread.Sleep(1000);

                if (acknowledge)
                {
                    // Send the acknowledgements as if they had come from sources.
                    var bookmark = Assert.IsType <BookmarkInfo>(BookmarkManager.GetBookmark(sourceId));
                    BookmarkManager.SaveBookmark(bookmark.Id, records.Last().Position, null);
                }

                source.Stop();
                Thread.Sleep(1000);

                var lastRecordTimestamp = Assert.Single(records).Timestamp;
                Assert.True(lastRecordTimestamp > now);

                records.Clear();

                //Write some new logs after the source stop
                var newmsg = "A fresh message after source stop";
                EventLog.WriteEntry(LogSource, newmsg, EventLogEntryType.Information, eventId);
                Thread.Sleep(1000);
                source.Start();
                Thread.Sleep(1000);

                // If it's a clean shutdown (i.e. config reload), the bookmark isn't removed from BookmarkManager,
                // so the source should pick up where it left off according to the previous bookmark value.
                if (acknowledge)
                {
                    var lastRecord = Assert.Single(records);
                    Assert.True(lastRecord.Timestamp > lastRecordTimestamp);
                    Assert.Matches("after source stop", lastRecord.GetMessage("string"));
                }
                else
                {
                    // When using Bookmark as Initial position, and there is no bookmark, it should not process old events.
                    // Since we didn't commit the bookmark in this theory, no records should be returned.
                    Assert.Empty(records);
                }
            }
        }