1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/common/signal/timer_test.go

61 lines
1.4 KiB
Go
Raw Normal View History

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