2017-09-27 09:29:00 -04:00
|
|
|
package signal_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/common/signal"
|
2017-09-27 09:29:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestActivityTimer(t *testing.T) {
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-10-25 03:49:13 -04:00
|
|
|
timer := CancelAfterInactivity(ctx, cancel, time.Second*4)
|
2017-09-27 09:29:00 -04:00
|
|
|
time.Sleep(time.Second * 6)
|
2019-02-02 16:19:30 -05:00
|
|
|
if ctx.Err() == nil {
|
|
|
|
t.Error("expected some error, but got nil")
|
|
|
|
}
|
2017-09-27 09:29:00 -04:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivityTimerUpdate(t *testing.T) {
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, time.Second*10)
|
2017-09-27 09:29:00 -04:00
|
|
|
time.Sleep(time.Second * 3)
|
2019-02-02 16:19:30 -05:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
t.Error("expected nil, but got ", ctx.Err().Error())
|
|
|
|
}
|
2017-09-27 09:29:00 -04:00
|
|
|
timer.SetTimeout(time.Second * 1)
|
|
|
|
time.Sleep(time.Second * 2)
|
2019-02-02 16:19:30 -05:00
|
|
|
if ctx.Err() == nil {
|
|
|
|
t.Error("expcted some error, but got nil")
|
|
|
|
}
|
2017-09-27 09:29:00 -04:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|
2018-02-06 05:16:49 -05:00
|
|
|
|
|
|
|
func TestActivityTimerNonBlocking(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, 0)
|
|
|
|
time.Sleep(time.Second * 1)
|
2019-02-02 16:19:30 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
|
|
|
t.Error("context not done")
|
|
|
|
}
|
2018-02-06 05:16:49 -05:00
|
|
|
timer.SetTimeout(0)
|
|
|
|
timer.SetTimeout(1)
|
|
|
|
timer.SetTimeout(2)
|
|
|
|
}
|
2018-05-28 08:02:09 -04:00
|
|
|
|
|
|
|
func TestActivityTimerZeroTimeout(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, 0)
|
2019-02-02 16:19:30 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
|
|
|
t.Error("context not done")
|
|
|
|
}
|
2018-05-28 08:02:09 -04:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|