Пример #1
0
        public async Task PerformanceTest_HundredsOfTimingEventsFinishesInAFewSeconds()
        {
            // create the orchestrator
            var context = new EventContext();

            context.AddLogAction(
                (m) => output.WriteLine(m.ToString())
                );

            var time = DateTime.Parse("01/01/2018 10:00AM");

            // create a consumer that when it receives a time event, that matches it's schedule, it will trigger a log write event
            var timeEventConsumer = new TimeEventConsumer();

            timeEventConsumer.EventAction = (triggerTime) => context.RaiseEvent(
                new LogEvent(context)
            {
                Message = $"Log of: TimeEvent hit at: {triggerTime.Ticks}",
                Time    = context.GetTimeNow()
            }, timeEventConsumer
                );

            // create a schedule that will only fire the Action when the time matches the event time
            timeEventConsumer.Schedules.Add(new Schedule {
                Frequency = ScheduleFrequency.OncePerDay, TimeToRun = time
            });

            var totalEventsSent = 100;

            // create a consumer that will see the evenets and write to the log
            var logWriteConsumer = new LogEventConsumer();

            context.RegisterConsumer <TimeEvent>(timeEventConsumer);
            context.RegisterConsumer <LogEvent>(logWriteConsumer);

            // run and ensure the listeners are all responding
            context.Initialize();

            var tasks = Enumerable.Range(1, totalEventsSent)
                        .Select(index => Task.Run(() => context.RaiseEvent(new TimeEvent(context, time.AddDays(-1 * index)), null)));

            await Task.WhenAll(tasks);

            context.AssertEventExists <TimeEvent>(totalEventsSent);
            context.AssertEventExists <LogEvent>(totalEventsSent);

            output.WriteLine(Environment.NewLine + "Warehouse Contents:");

            Parallel.ForEach(logWriteConsumer.LogLines,
                             new ParallelOptions {
                MaxDegreeOfParallelism = 10
            },
                             (line) =>
            {
                output.WriteLine(line);
            }
                             );

            logWriteConsumer.LogLines.Count().Should().Be(totalEventsSent + 1);
        }
Пример #2
0
        public void TimeEventShouldTriggerOneAndOnlyOneSchedule()
        {
            // create the orchestrator
            var context = new EventContext();

            context.AddLogAction(
                (m) => output.WriteLine(m.ToString())
                );

            var time = DateTime.Parse("01/01/2018 10:00AM");

            // create a consumer that when it receives a time event, that matches it's schedule, it will trigger a log write event
            var timeEventConsumer = new TimeEventConsumer();

            timeEventConsumer.EventAction = (triggerTime) => context.RaiseEvent(
                new LogEvent(context)
            {
                Message = $"Log of: TimeEvent hit at: {triggerTime.Ticks}",
                Time    = context.GetTimeNow()
            }, timeEventConsumer
                );

            // create a schedule that will only fire the Action when the time matches the event time
            timeEventConsumer.Schedules.Add(new Schedule {
                Frequency = ScheduleFrequency.OncePerDay, TimeToRun = time
            });

            // create a consumer that will see the evenets and write to the log
            var logWriteConsumer = new LogEventConsumer();

            context.RegisterConsumer <TimeEvent>(timeEventConsumer);
            context.RegisterConsumer <LogEvent>(logWriteConsumer);

            // run and ensure the listeners are all responding
            context.Initialize();

            context.RaiseEvent(new TimeEvent(context, time), null);
            context.RaiseEvent(new TimeEvent(context, time.AddDays(-10)), null);
            context.RaiseEvent(new TimeEvent(context, time.AddDays(10)), null);

            Thread.Sleep(500);             // just wait a bit for the events to be handled

            context.AssertEventExists <TimeEvent>(3);
            context.AssertEventExists <LogEvent>(3);

            output.WriteLine(Environment.NewLine + "Warehouse Contents:");
            logWriteConsumer.LogLines.ForEach(output.WriteLine);
            logWriteConsumer.LogLines.Count().Should().Be(4);
        }
Пример #3
0
        public void Context_AddSchedule_ShouldAddCauseEventToBeTriggeredFromSchedule()
        {
            bool reached = false;

            // create the orchestrator
            var context = new EventContext(defaultScheduleTimeIntervalInMilliseconds: 25);

            context.AddLogAction(
                (m) => output.WriteLine(m.ToString())
                );

            var timeSecond = DateTime.Now.Second;

            // create a schedule that will only fire the Action when the time matches the event time
            context.AddScheduledAction(new Schedule {
                Frequency = ScheduleFrequency.OnceEveryUnitsMinute, FrequencyUnit = timeSecond
            },
                                       (hitTime) => { output.WriteLine($"EVENT HIT: {hitTime}"); reached = true; });

            // run and ensure the listeners are all responding
            context.Initialize();

            Thread.Sleep(50);             // just wait a bit for the events to be handled

            // a few time e vents may be release
            context.AssertEventExists <TimeEvent>(atLeast: 1);
            reached.Should().BeTrue();
        }
Пример #4
0
        public void QueueEventProducerShouldRetrieveEventAndPutIntoContext()
        {
            var memQueue = new MemoryEventQueue();
            var context  = new EventContext(eventQueue: memQueue);

            context.AddLogAction(
                (m) => output.WriteLine(m.ToString())
                );

            context.Initialize();

            var testEvent = new TestEvent(context);

            // raise the event
            //context.RaiseEvent(testEvent, null);
            memQueue.Enqueue(testEvent);

            // give the system enough time to react to the event showing up
            Thread.Sleep(200);

            // look for non-time events
            context.AssertEventExists <TestEvent>();
        }