mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
60
common/signal/timer_test.go
Normal file
60
common/signal/timer_test.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package signal_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/common/signal"
|
||||
)
|
||||
|
||||
func TestActivityTimer(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
timer := CancelAfterInactivity(ctx, cancel, time.Second*4)
|
||||
time.Sleep(time.Second * 6)
|
||||
if ctx.Err() == nil {
|
||||
t.Error("expected some error, but got nil")
|
||||
}
|
||||
runtime.KeepAlive(timer)
|
||||
}
|
||||
|
||||
func TestActivityTimerUpdate(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
timer := CancelAfterInactivity(ctx, cancel, time.Second*10)
|
||||
time.Sleep(time.Second * 3)
|
||||
if ctx.Err() != nil {
|
||||
t.Error("expected nil, but got ", ctx.Err().Error())
|
||||
}
|
||||
timer.SetTimeout(time.Second * 1)
|
||||
time.Sleep(time.Second * 2)
|
||||
if ctx.Err() == nil {
|
||||
t.Error("expcted some error, but got nil")
|
||||
}
|
||||
runtime.KeepAlive(timer)
|
||||
}
|
||||
|
||||
func TestActivityTimerNonBlocking(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
timer := CancelAfterInactivity(ctx, cancel, 0)
|
||||
time.Sleep(time.Second * 1)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
default:
|
||||
t.Error("context not done")
|
||||
}
|
||||
timer.SetTimeout(0)
|
||||
timer.SetTimeout(1)
|
||||
timer.SetTimeout(2)
|
||||
}
|
||||
|
||||
func TestActivityTimerZeroTimeout(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
timer := CancelAfterInactivity(ctx, cancel, 0)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
default:
|
||||
t.Error("context not done")
|
||||
}
|
||||
runtime.KeepAlive(timer)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue