Ejemplo n.º 1
0
        public void TestResetTimedEvent()
        {
            // event for reset testing
            var e = new GameEvent {
                EventType = GameEventType.TimedEvent,
                From      = this,
                To        = _helper,
                Id        = 587
            };

            _eventBus.RegisterTimedEvent(e, TimePeriod.NewMilliseconds(1000));

            // event which should stay in event bus for a very long time,
            // and not be disturbed by resetting the other event multiple times.
            var longWaitEvent = new GameEvent {
                EventType = GameEventType.TimedEvent,
                From      = this,
                To        = _helper,
                Id        = 315
            };

            _eventBus.RegisterTimedEvent(longWaitEvent, TimePeriod.NewSeconds(30.0));

            // time has not passed yet, so event should not have been processed
            Thread.Sleep(100);
            _eventBus.ProcessEvents();
            Assert.AreEqual(0, _helper.EventCounter);

            // reset event timer
            bool reset = _eventBus.ResetTimedEvent(587, TimePeriod.NewMilliseconds(100));

            Assert.IsTrue(reset);

            Thread.Sleep(150);
            _eventBus.ProcessEvents();
            Assert.AreEqual(1, _helper.EventCounter);

            // try to reset event timer again,
            // should fail because event has been processed
            bool resetFail = _eventBus.ResetTimedEvent(587, TimePeriod.NewMilliseconds(100));

            Assert.IsFalse(resetFail);

            // because event was not contained, we could not reset it.
            // Thus, event should not be contained, and if we call ProcessEvents
            // it should NOT be processed again - verified by the EventCounter.
            Thread.Sleep(150);
            _eventBus.ProcessEvents();
            Assert.AreEqual(1, _helper.EventCounter);

            // test that resetting did not disturb the other event
            Assert.IsTrue(_eventBus.HasTimedEvent(longWaitEvent.Id));
        }