-
Notifications
You must be signed in to change notification settings - Fork 10
/
context_test.go
171 lines (156 loc) · 5.86 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package goa_test
import (
"context"
"net/http"
"net/url"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/shogo82148/goa-v1"
)
// contextKey is a value for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation.
type contextKey struct {
name string
}
func (k *contextKey) String() string { return "goa-v1 context value " + k.name }
var _ = Describe("ResponseData", func() {
var data *goa.ResponseData
var rw http.ResponseWriter
var req *http.Request
var params url.Values
BeforeEach(func() {
var err error
req, err = http.NewRequest("GET", "google.com", nil)
Ω(err).ShouldNot(HaveOccurred())
rw = &TestResponseWriter{Status: 42}
params = url.Values{"query": []string{"value"}}
ctx := goa.NewContext(context.Background(), rw, req, params)
data = goa.ContextResponse(ctx)
})
Context("SwitchWriter", func() {
var rwo http.ResponseWriter
It("sets the response writer and returns the previous one", func() {
Ω(rwo).Should(BeNil())
rwo = data.SwitchWriter(&TestResponseWriter{Status: 43})
Ω(rwo).ShouldNot(BeNil())
Ω(rwo).Should(BeAssignableToTypeOf(&TestResponseWriter{}))
trw := rwo.(*TestResponseWriter)
Ω(trw.Status).Should(Equal(42))
})
})
Context("Write", func() {
It("should call WriteHeader(http.StatusOK) if WriteHeader has not yet been called", func() {
_, err := data.Write(nil)
Ω(err).Should(BeNil())
Ω(data.Status).Should(Equal(http.StatusOK))
})
It("should not affect Status if WriteHeader has been called", func() {
status := http.StatusBadRequest
data.WriteHeader(status)
_, err := data.Write(nil)
Ω(err).Should(BeNil())
Ω(data.Status).Should(Equal(status))
})
})
Context("Context", func() {
mergeContext := func(parent, child context.Context) context.Context {
req, err := http.NewRequestWithContext(child, "GET", "google.com", nil)
Ω(err).Should(BeNil())
return goa.NewContext(parent, &TestResponseWriter{Status: 42}, req, url.Values{})
}
Context("Deadline", func() {
It("should be empty if the parent and the child have no deadline", func() {
ctx := mergeContext(context.Background(), context.Background())
_, ok := ctx.Deadline()
Ω(ok).Should(Equal(false))
})
It("should return the parent's deadline if the child have no deadline", func() {
deadline := time.Now().Add(time.Second)
parent, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
ctx := mergeContext(parent, context.Background())
got, ok := ctx.Deadline()
Ω(ok).Should(Equal(true))
Ω(got).Should(BeTemporally("~", deadline, 100*time.Millisecond))
})
It("should return the child's deadline if the parent have no deadline", func() {
deadline := time.Now().Add(time.Second)
child, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
ctx := mergeContext(context.Background(), child)
got, ok := ctx.Deadline()
Ω(ok).Should(Equal(true))
Ω(got).Should(BeTemporally("~", deadline, 100*time.Millisecond))
})
It("should return the child's deadline if it is earlier than the parent's one", func() {
deadline1 := time.Now().Add(time.Second)
child, cancel := context.WithDeadline(context.Background(), deadline1)
defer cancel()
deadline2 := time.Now().Add(2 * time.Second)
parent, cancel := context.WithDeadline(context.Background(), deadline2)
defer cancel()
ctx := mergeContext(parent, child)
got, ok := ctx.Deadline()
Ω(ok).Should(Equal(true))
Ω(got).Should(BeTemporally("~", deadline1, 100*time.Millisecond))
})
It("should return the parent's deadline if it is earlier than the child's one", func() {
deadline1 := time.Now().Add(2 * time.Second)
child, cancel := context.WithDeadline(context.Background(), deadline1)
defer cancel()
deadline2 := time.Now().Add(time.Second)
parent, cancel := context.WithDeadline(context.Background(), deadline2)
defer cancel()
ctx := mergeContext(parent, child)
got, ok := ctx.Deadline()
Ω(ok).Should(Equal(true))
Ω(got).Should(BeTemporally("~", deadline2, 100*time.Millisecond))
})
})
Context("Done", func() {
It("should be canceled when the parent is canceled", func() {
deadline := time.Now().Add(time.Second)
parent, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
ctx := mergeContext(parent, context.Background())
select {
case <-ctx.Done():
case <-time.After(5 * time.Second):
Fail("timeout")
}
Ω(ctx.Err()).ShouldNot(BeNil())
Ω(time.Now()).Should(BeTemporally("~", deadline, 500*time.Millisecond))
})
It("should be canceled when the child is canceled", func() {
deadline := time.Now().Add(time.Second)
child, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
ctx := mergeContext(context.Background(), child)
select {
case <-ctx.Done():
case <-time.After(5 * time.Second):
Fail("timeout")
}
Ω(ctx.Err()).ShouldNot(BeNil())
Ω(time.Now()).Should(BeTemporally("~", deadline, 500*time.Millisecond))
})
})
Context("Value", func() {
key := &contextKey{"key"}
otherKey := &contextKey{"other-key"}
It("should return the value associated with the child if it exists", func() {
parent := context.WithValue(context.Background(), key, "parent value")
child := context.WithValue(context.Background(), key, "child value")
ctx := mergeContext(parent, child)
Ω(ctx.Value(key)).Should(Equal("child value"))
})
It("should return the value associated with the parent if the child associates nothing", func() {
parent := context.WithValue(context.Background(), key, "parent value")
child := context.WithValue(context.Background(), otherKey, "child value")
ctx := mergeContext(parent, child)
Ω(ctx.Value(key)).Should(Equal("parent value"))
})
})
})
})