The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/internal/utils/timer_test.go

42 lines
915 B

package utils
import (
"testing"
"time"
)
func TestNewTimeout(t *testing.T) {
timer := NewTimeout(time.Second)
if timer == nil || timer.Duration() != time.Second || timer.IsActive() {
t.Fatalf("timer initialization error")
}
}
func TestCheckExpire(t *testing.T) {
timer := NewTimeout(time.Second)
timer.Start()
now := time.Now()
if timer.Expired(now) {
t.Fatalf("Timer shouldn't be expired")
}
if !timer.Expired(now.Add(2 * time.Second)) {
t.Fatalf("Timer should be expired")
}
// start again
timer.Start()
if timer.Expired(now) {
t.Fatalf("Timer shouldn't be expired")
}
if !timer.Expired(now.Add(2 * time.Second)) {
t.Fatalf("Timer should be expired")
}
// stop
timer.Stop()
if timer.Expired(now) {
t.Fatalf("Timer shouldn't be expired because it is stopped")
}
if timer.Expired(now.Add(2 * time.Second)) {
t.Fatalf("Timer shouldn't be expired because it is stopped")
}
}