Official cwtch.im peer implementation. https://cwtch.im
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.
 
 

42 lines
776 B

package utils
import (
"runtime"
"testing"
"time"
)
// Test timeout policy, checking for goroutine leaks in addition to successful timeouts
func TestTimeoutPolicy(t *testing.T) {
gonumStart := runtime.NumGoroutine()
tp := TimeoutPolicy(time.Second)
// test with timeout
err := tp.ExecuteAction(func() error {
time.Sleep(time.Second * 2)
return nil
})
if err == nil {
t.Fatalf("timeout should have occurred")
}
// test without timeout
err = tp.ExecuteAction(func() error {
return nil
})
if err != nil {
t.Fatalf("timeout should not have occurred")
}
// wait for gorutine clean up
time.Sleep(time.Second * 4)
// final check
gonumEnd := runtime.NumGoroutine()
if gonumStart != gonumEnd {
t.Fatalf("goroutine leak in execute action")
}
}