forked from choria-io/asyncjobs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_reply_handler_test.go
169 lines (138 loc) · 4.49 KB
/
request_reply_handler_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
// Copyright (c) 2022, R.I. Pienaar and the Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package asyncjobs
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("RequestReplyHandler", func() {
var (
ctx context.Context
cancel context.CancelFunc
)
BeforeEach(func() {
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
log.SetOutput(GinkgoWriter)
})
AfterEach(func() { cancel() })
It("Should handle support catchall", func() {
withJetStream(func(nc *nats.Conn, _ *jsm.Manager) {
task, err := NewTask("email:new", "testing")
go func() {
nc.Subscribe(fmt.Sprintf(RequestReplyTaskHandlerPattern, "catchall"), func(msg *nats.Msg) {
resp := nats.NewMsg(msg.Subject)
resp.Header.Add(RequestReplyError, "test error")
resp.Data = []byte("error")
msg.RespondMsg(resp)
})
<-ctx.Done()
}()
time.Sleep(20 * time.Millisecond)
rrh := newRequestReplyHandleFunc(nc, "")
Expect(err).ToNot(HaveOccurred())
payload, err := rrh(ctx, &defaultLogger{}, task)
Expect(err).To(MatchError(fmt.Errorf("test error")))
Expect(payload).To(Equal([]byte("error")))
})
})
It("Should handle normal headers", func() {
withJetStream(func(nc *nats.Conn, _ *jsm.Manager) {
task, err := NewTask("email:new", "testing")
go func() {
nc.Subscribe(fmt.Sprintf(RequestReplyTaskHandlerPattern, "email:new"), func(msg *nats.Msg) {
resp := nats.NewMsg(msg.Subject)
resp.Header.Add(RequestReplyError, "test error")
resp.Data = []byte("error")
msg.RespondMsg(resp)
})
<-ctx.Done()
}()
time.Sleep(20 * time.Millisecond)
rrh := newRequestReplyHandleFunc(nc, "email:new")
Expect(err).ToNot(HaveOccurred())
payload, err := rrh(ctx, &defaultLogger{}, task)
Expect(err).To(MatchError(fmt.Errorf("test error")))
Expect(payload).To(Equal([]byte("error")))
})
})
It("Should handle termination headers", func() {
withJetStream(func(nc *nats.Conn, _ *jsm.Manager) {
task, err := NewTask("email:new", "testing")
go func() {
nc.Subscribe(fmt.Sprintf(RequestReplyTaskHandlerPattern, "email:new"), func(msg *nats.Msg) {
resp := nats.NewMsg(msg.Subject)
resp.Header.Add(RequestReplyTerminateError, "test termination")
resp.Data = []byte("error")
msg.RespondMsg(resp)
})
<-ctx.Done()
}()
time.Sleep(20 * time.Millisecond)
rrh := newRequestReplyHandleFunc(nc, "email:new")
Expect(err).ToNot(HaveOccurred())
payload, err := rrh(ctx, &defaultLogger{}, task)
Expect(err).To(MatchError(ErrTerminateTask))
Expect(payload).To(Equal([]byte("error")))
})
})
It("Should call correctly with the right headers", func() {
withJetStream(func(nc *nats.Conn, _ *jsm.Manager) {
task, err := NewTask("email:new", "testing")
go func() {
nc.Subscribe(fmt.Sprintf(RequestReplyTaskHandlerPattern, "email:new"), func(msg *nats.Msg) {
fail := func(format string, a ...any) {
resp := nats.NewMsg(msg.Subject)
resp.Header.Add(RequestReplyError, fmt.Sprintf(format, a...))
msg.RespondMsg(resp)
}
ct := msg.Header.Get(RequestReplyContentTypeHeader)
deadline, err := time.Parse(time.RFC3339, msg.Header.Get(RequestReplyDeadlineHeader))
if err != nil {
fail("invalid deadline: %v", deadline)
return
}
if ct != RequestReplyTaskType {
fail("invalid content type %v", ct)
return
}
if time.Until(deadline) < 1500*time.Millisecond {
fail("invalid deadline %v (%v)", deadline, time.Until(deadline))
return
}
t := &Task{}
err = json.Unmarshal(msg.Data, t)
if err != nil {
fail("invalid task json: %v", err)
return
}
if t.ID != task.ID {
fail("invalid task content: %#v", t)
return
}
msg.Respond([]byte("ok"))
})
<-ctx.Done()
}()
time.Sleep(20 * time.Millisecond)
rrh := newRequestReplyHandleFunc(nc, "email:new")
Expect(err).ToNot(HaveOccurred())
_, err = rrh(context.Background(), &defaultLogger{}, task)
Expect(err).To(MatchError(ErrRequestReplyNoDeadline))
short, cancel := context.WithTimeout(ctx, time.Second)
_, err = rrh(short, &defaultLogger{}, task)
cancel()
Expect(err).To(MatchError(ErrRequestReplyShortDeadline))
payload, err := rrh(ctx, &defaultLogger{}, task)
Expect(err).ToNot(HaveOccurred())
Expect(payload).To(Equal([]byte("ok")))
})
})
})