2017-09-27 13:29:00 +00:00
|
|
|
package signal_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "v2ray.com/core/common/signal"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2017-09-27 13:29:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestActivityTimer(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-09-27 13:29:00 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, time.Second*5)
|
2017-09-27 13:29:00 +00:00
|
|
|
time.Sleep(time.Second * 6)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(ctx.Err(), IsNotNil)
|
2017-09-27 13:29:00 +00:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivityTimerUpdate(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2017-09-27 13:29:00 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, time.Second*10)
|
2017-09-27 13:29:00 +00:00
|
|
|
time.Sleep(time.Second * 3)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(ctx.Err(), IsNil)
|
2017-09-27 13:29:00 +00:00
|
|
|
timer.SetTimeout(time.Second * 1)
|
|
|
|
time.Sleep(time.Second * 2)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(ctx.Err(), IsNotNil)
|
2017-09-27 13:29:00 +00:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|
2018-02-06 10:16:49 +00:00
|
|
|
|
|
|
|
func TestActivityTimerNonBlocking(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, 0)
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
assert(ctx, HasDone)
|
|
|
|
timer.SetTimeout(0)
|
|
|
|
timer.SetTimeout(1)
|
|
|
|
timer.SetTimeout(2)
|
|
|
|
}
|
2018-05-28 12:02:09 +00:00
|
|
|
|
|
|
|
func TestActivityTimerZeroTimeout(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := CancelAfterInactivity(ctx, cancel, 0)
|
|
|
|
assert(ctx, HasDone)
|
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
}
|