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

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

            // 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        = 317
            };

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

            // time out the event and process
            Thread.Sleep(150);
            _eventBus.ProcessEvents();
            Assert.AreEqual(1, _helper.EventCounter);

            // now, add the event again
            _eventBus.AddOrResetTimedEvent(e, TimePeriod.NewMilliseconds(100));

            // time out the event and process again
            Thread.Sleep(150);
            _eventBus.ProcessEvents();
            Assert.AreEqual(2, _helper.EventCounter);

            // add the event one more time,
            // but this time with a longer time period
            _eventBus.AddOrResetTimedEvent(e, TimePeriod.NewMilliseconds(2000));

            // now reset it to a short time period
            _eventBus.AddOrResetTimedEvent(e, TimePeriod.NewMilliseconds(100));

            // time out the event and process again,
            // and test that the reset worked
            Thread.Sleep(150);
            _eventBus.ProcessEvents();
            Assert.AreEqual(3, _helper.EventCounter);

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