-
Notifications
You must be signed in to change notification settings - Fork 20
/
mock_stdlib_test.go
102 lines (91 loc) · 2.65 KB
/
mock_stdlib_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package mock_stdlib
import (
"context"
"errors"
"net"
"net/http"
"testing"
"time"
"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/mock"
)
// go run ./cmd/xgo test --project-dir runtime -run TestMockTimeNow -v ./test/mock_stdlib
// go run ./script/run-test/ --include go1.22.1 --xgo-runtime-test-only -run TestMockTimeNow -v ./test/mock_stdlib
func TestMockTimeNow(t *testing.T) {
now1 := time.Now()
time.Sleep(1 * time.Millisecond)
now2 := time.Now()
d1 := now2.Sub(now1)
if d1 <= 0 {
t.Fatalf("expect now2-now1 > 0 , actual: %v", d1)
}
cancel := mock.Mock(time.Now, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error {
results.GetFieldIndex(0).Set(now1)
return nil
})
now3 := time.Now()
if now3 != now1 {
t.Fatalf("expect now3 equals to now1 exactly, actual diff: %v", now3.Sub(now1))
}
cancel()
now4 := time.Now()
d4 := now4.Sub(now1)
if d4 <= 0 {
t.Fatalf("expect now4-now1 > 0 after cancelling mock, actual: %v", d4)
}
}
// go run ./cmd/xgo test --project-dir runtime -run TestMockHTTP -v ./test/mock_stdlib
func TestMockHTTP(t *testing.T) {
var haveMocked bool
mock.Mock(http.DefaultClient.Do, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error {
haveMocked = true
return nil
})
http.DefaultClient.Do(nil)
if !haveMocked {
t.Fatalf("expect http.DefaultClient.Do to have been mocked, actually not mocked")
}
}
// execution log(NOTE the cost is not 1s):
//
// === RUN TestMockTimeSleep
// --- PASS: TestMockTimeSleep (0.00s)
// PASS
// ok github.com/xhd2015/xgo/runtime/test/mock_stdlib 0.725s
func TestMockTimeSleep(t *testing.T) {
begin := time.Now()
sleepDur := 1 * time.Second
var haveCalledMock bool
var mockArg time.Duration
mock.Mock(time.Sleep, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error {
haveCalledMock = true
mockArg = args.GetFieldIndex(0).Value().(time.Duration)
return nil
})
time.Sleep(sleepDur)
// 37.275µs
cost := time.Since(begin)
if !haveCalledMock {
t.Fatalf("expect haveCalledMock, actually not")
}
if mockArg != sleepDur {
t.Fatalf("expect mockArg to be %v, actual: %v", sleepDur, mockArg)
}
// t.Logf("cost: %v", cost)
if cost > 100*time.Millisecond {
t.Fatalf("expect time.Sleep mocked, actual cost: %v", cost)
}
}
// see issue https://github.com/xhd2015/xgo/issues/225
func TestDialer(t *testing.T) {
var mocked bool
mock.Patch((*net.Dialer).Dial, func(_ *net.Dialer, network, address string) (net.Conn, error) {
mocked = true
return nil, errors.New("dial error")
})
dialer := net.Dialer{}
dialer.Dial("", "")
if !mocked {
t.Fatalf("expected mocked, actually not")
}
}