private static void expired(Object state) { TimerTester timerTester = (TimerTester)state; lock (timerTester.testTimer) { // // Did somebody else fail? // if (timerTester.testTimer.vectorIndex < 0) { return; } // // Were we supposed to go off? // if (!timerTester.armed || timerTester.testTimer.vectorIndex > timerTester.id) { timerTester.testTimer.vectorIndex = -timerTester.id; return; } // // We were supposed to be next. // timerTester.testTimer.vectorIndex = timerTester.id + 1; timerTester.testTimer.firedCount -= 1; if (timerTester.testTimer.firedCount == 0) { Monitor.Pulse(timerTester.testTimer); } } }
public void Run_With_External_Thread() { TimerTester timed_test = new TimerTester("External thread test"); // need this for a subscriber for ticks. LazyTimer t1 = new LazyTimer(TENTH_OF_SEC); timed_test.Subscribe(t1); // Normally I would keep Run private. This is just for show. Thread worker = new Thread(new ThreadStart(t1.Run)); worker.Start(); Thread.Sleep(TWO_SECONDS); t1.Stop(); worker.Join(); Assert.True( timed_test.HasTirggered() ); Assert.GreaterOrEqual(timed_test.GetTriggerCount(), 39); // we should have at least 2000 / 10 = 40 Console.WriteLine("------------------Resetting---------------------"); // a visual timed_test.Reset(); // reset the test helper's count and triggered flag. worker = new Thread(new ThreadStart(t1.Run)); t1.ResetTo(TEN_MILLISEC); worker.Start(); Thread.Sleep(TWO_SECONDS); t1.Stop(); worker.Join(); Assert.True(timed_test.HasTirggered()); // Was going to put a binominal Assert.GreaterOrEqual(timed_test.GetTriggerCount(), (TWO_SECONDS / TEN_MILLISEC)); // we should have at least 2000 / 10 = 400 }
public void Internal_Thread_Start_Stop_test() { LazyTimer lazybones = new LazyTimer(ONE_SECOND); TimerTester evaluator = new TimerTester("Internal thread test"); // need this for a subscriber for ticks. evaluator.Subscribe(lazybones); Assert.IsFalse(evaluator.HasTirggered()); // make sure our internal thread works. lazybones.Start(); Thread.Sleep(ONE_SECOND); lazybones.Stop(); Assert.IsTrue(evaluator.HasTirggered()); }
private void permuteTimeoutOrdering(int index) { // // Have we finished permuting this.timerVector? // if (index == this.timerVector.Length) { // // Arm all the timers and do the test. // lock (this) { this.vectorIndex = 1; this.firedCount = this.timerVector.Length; for (int i = 0; i < this.timerVector.Length; i += 1) { this.timerVector[i].Arm(); } Assert(Monitor.Wait(this, SHORT_TIMEOUT * 2 + this.firedCount * MIN_TIMEOUT)); Assert(this.firedCount == 0); } return; } // // Permute the vector at the passed index. // TimerTester first = this.timerVector[index]; for (int i = index; i < this.timerVector.Length; i += 1) { this.timerVector[index] = this.timerVector[i]; this.timerVector[i] = first; this.permuteTimeoutOrdering(index + 1); this.timerVector[i] = this.timerVector[index]; this.timerVector[index] = first; } }
public void Test_Multi_Subscribers() { LazyTimer lazybones = new LazyTimer(ONE_SECOND); TimerTester evaluator1 = new TimerTester("Subscriber 1"); TimerTester evaluator2 = new TimerTester("Subscriber 2"); evaluator1.Subscribe(lazybones); evaluator2.Subscribe(lazybones); Assert.IsFalse(evaluator1.HasTirggered()); Assert.IsFalse(evaluator2.HasTirggered()); // make sure our internal thread works. lazybones.Start(); Thread.Sleep(ONE_SECOND); lazybones.Stop(); Assert.IsTrue(evaluator1.HasTirggered()); Assert.IsTrue(evaluator2.HasTirggered()); }