public void InterruptTest() { EventClass longEvent = new EventClass(); EventClass shortEvent = new EventClass(); Exception err = null; using (InterruptibleEventExecutor uut = new InterruptibleEventExecutor(5000)) { uut.OnError += delegate(Exception e) { err = e; }; uut.Start(); uut.AddEvent( delegate() { Thread.Sleep(10000); longEvent.Execute(); } ); // Ensure getting a TheadInterruptedException doesn't // napalm our event executor by ensuring a second event // will execute. uut.AddEvent( delegate() { shortEvent.Execute(); } ); Assert.IsTrue(shortEvent.Join(30 * 1000)); Assert.IsTrue(shortEvent.Executed); Assert.IsFalse(longEvent.Executed); // Should have been interrupted. } Assert.IsNotNull(err); Assert.IsTrue(err is ThreadInterruptedException); }
public void NoInterruptTest() { EventClass longEvent = new EventClass(); EventClass shortEvent = new EventClass(); Exception err = null; using (InterruptibleEventExecutor uut = new InterruptibleEventExecutor(int.MaxValue)) { uut.OnError += delegate(Exception e) { err = e; }; uut.Start(); uut.AddEvent( delegate() { Thread.Sleep(10000); longEvent.Execute(); } ); uut.AddEvent( delegate() { shortEvent.Execute(); } ); Assert.IsTrue(longEvent.Join(30 * 1000)); Assert.IsTrue(longEvent.Executed); Assert.IsTrue(shortEvent.Join(30 * 1000)); Assert.IsTrue(shortEvent.Executed); } Assert.IsNull(err); }
public void ThousandEventTest() { using (InterruptibleEventExecutor executor = new InterruptibleEventExecutor(5000)) { executor.Start(); EventClass[] events = new EventClass[1000]; for (int i = 0; i < 1000; ++i) { EventClass ec = new EventClass(); events[i] = ec; executor.AddEvent(() => ec.Execute()); } foreach (EventClass ec in events) { // Wait for everything to complete before calling Asset. Assert.IsTrue(ec.Join(30 * 1000)); Assert.IsTrue(ec.Executed); } } }