mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-05 08:07:18 +08:00

* Don't run clock add in eventually loop * Fix busy loop * Fix scheduling bug * Add new mock clock * Add busy loop test * With comments * Fix comment * Move mockclock to separate file * Fix race * Fix potential deadlock * Fix flaky TestBackoff * Fix import * Fix how mock implements interface
45 lines
713 B
Go
45 lines
713 B
Go
package test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMockClock(t *testing.T) {
|
|
cl := NewMockClock()
|
|
t1 := cl.InstantTimer(cl.Now().Add(2 * time.Second))
|
|
t2 := cl.InstantTimer(cl.Now().Add(time.Second))
|
|
|
|
// Advance the clock by 500ms
|
|
cl.AdvanceBy(time.Millisecond * 500)
|
|
|
|
// No event
|
|
select {
|
|
case <-t1.Ch():
|
|
t.Fatal("t1 fired early")
|
|
case <-t2.Ch():
|
|
t.Fatal("t2 fired early")
|
|
default:
|
|
}
|
|
|
|
// Advance the clock by 500ms
|
|
cl.AdvanceBy(time.Millisecond * 500)
|
|
|
|
// t2 fires
|
|
select {
|
|
case <-t1.Ch():
|
|
t.Fatal("t1 fired early")
|
|
case <-t2.Ch():
|
|
}
|
|
|
|
// Advance the clock by 2s
|
|
cl.AdvanceBy(time.Second * 2)
|
|
|
|
// t1 fires
|
|
select {
|
|
case <-t1.Ch():
|
|
case <-t2.Ch():
|
|
t.Fatal("t2 fired again")
|
|
}
|
|
}
|