Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package signal_test
 
import (
    "context"
    "runtime"
    "testing"
    "time"
 
    . "github.com/v2fly/v2ray-core/v5/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("expected 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)
}