-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
123 lines (100 loc) · 3.45 KB
/
context_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package tegenaria
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestWithDeadline(t *testing.T) {
convey.Convey("test context dead line", t, func() {
server := NewTestServer()
spider1 := &TestSpider{
NewBaseSpider("testSpider", []string{"https://www.example.com"}),
}
request := NewRequest(server.URL+"/testGET", GET, testParser)
deadLine, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*2))
defer cancel()
t1 := time.Now() // get current time
ctx := NewContext(request, spider1, WithContext(deadLine))
<-ctx.Done()
elapsed := time.Since(t1)
convey.So(elapsed.Seconds(), convey.ShouldBeLessThan, 3.0)
convey.So(elapsed.Seconds(), convey.ShouldBeGreaterThan, 1.0)
})
}
func TestWithTimeout(t *testing.T) {
convey.Convey("test time context", t, func() {
server := NewTestServer()
spider1 := &TestSpider{
NewBaseSpider("testspider", []string{"https://www.example.com"}),
}
request := NewRequest(server.URL+"/testGET", GET, testParser)
timeout, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
ctx := NewContext(request, spider1, WithContext(timeout))
time.Sleep(time.Second * 5)
<-ctx.Done()
msg := ctx.Err().Error()
convey.So(msg, convey.ShouldContainSubstring, "context deadline exceeded")
})
}
func TestWithValue(t *testing.T) {
convey.Convey("test value context", t, func() {
type ContextKey string
k := ContextKey("test_key")
spider1 := &TestSpider{
NewBaseSpider("testSpider", []string{"https://www.example.com"}),
}
server := NewTestServer()
request := NewRequest(server.URL+"/testGET", GET, testParser)
valueCtx := context.WithValue(context.Background(), k, "tegenaria")
ctx := NewContext(request, spider1, WithContext(valueCtx))
convey.So(ctx.Value(k).(string), convey.ShouldContainSubstring, "tegenaria")
})
}
func TestWithContextID(t *testing.T) {
convey.Convey("test value context", t, func() {
spider1 := &TestSpider{
NewBaseSpider("testSpider", []string{"https://www.example.com"}),
}
server := NewTestServer()
request := NewRequest(server.URL+"/testGET", GET, testParser)
ctx := NewContext(request, spider1, WithContextID("1234567890"))
convey.So(ctx.GetCtxID(), convey.ShouldContainSubstring, "1234567890")
})
}
func TestWithEmptyContext(t *testing.T) {
convey.Convey("test empty context", t, func() {
server := NewTestServer()
spider1 := &TestSpider{
NewBaseSpider("testSpider", []string{"https://www.example.com"}),
}
request := NewRequest(server.URL+"/testGET", GET, testParser)
ctx := NewContext(request, spider1)
c := ctx.Done()
convey.So(c, convey.ShouldNotBeNil)
_, ok := ctx.Deadline()
convey.So(ok, convey.ShouldBeFalse)
convey.So(ctx.Err(), convey.ShouldBeNil)
type ContextKey string
k := ContextKey("test_key")
convey.So(ctx.Value(k), convey.ShouldBeNil)
})
}
func TestContextWithRequestNil(t *testing.T) {
convey.Convey("test context with nil empty", t, func() {
server := NewTestServer()
spider1 := &TestSpider{
NewBaseSpider("testSpider", []string{"https://www.example.com"}),
}
request := NewRequest(server.URL+"/testGET", GET, testParser)
ctx := NewContext(request, spider1)
ctx.Request = nil
t, d := ctx.Deadline()
convey.So(d, convey.ShouldBeFalse)
convey.So(t.String(), convey.ShouldContainSubstring, "0001-01-01")
c := ctx.Done()
convey.So(c, convey.ShouldBeNil)
convey.So(ctx.Err(), convey.ShouldBeNil)
})
}