-
Notifications
You must be signed in to change notification settings - Fork 6
/
http_worker_test.go
169 lines (149 loc) · 4.44 KB
/
http_worker_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
package main
import (
"context"
"strings"
"testing"
"time"
)
var httpContext, _ = func() (context.Context, context.CancelFunc) {
return context.WithCancel(context.Background())
}()
var httpMux = NewHttpWorker(httpContext, "TestQueue", "TestTopic", 20, &TestStorage{}, &TestSuccessRemote{})
func TestHttpMuxRun(t *testing.T) {
// todo, have not find an available method for test running go proc, so just test the code is running as normal
httpMux.Serve()
t.Log("no news")
}
func TestHttpMuxClose(t *testing.T) {
// todo, have not find an available method for test running go proc, so just test the code is running as normal
httpMux.Close()
t.Log("no news")
}
func TestHttpMuxAddJobs(t *testing.T) {
err := httpMux.AddJobs(&Jobs{queueName: "TestQueue"}, 1)
if err != nil {
t.Fatalf("AddJobs Case want err = nil returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpMuxAddJobsErrorCase(t *testing.T) {
err := httpMux.AddJobs(&Jobs{queueName: "TestError"}, 1)
if err == nil {
t.Fatalf("AddJobs Case want err = TestError returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpMuxGetJobsFromQueue(t *testing.T) {
_, err := httpMux.GetJobsFromQueue("TestQueue", "TestTopic", 1)
if err != nil {
t.Fatalf("GetJobsFromQueue Case want err = nil returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpMuxGetJobsFromQueueErrorCase(t *testing.T) {
_, err := httpMux.GetJobsFromQueue("TestError", "TestTopic", 1)
if err == nil {
t.Fatalf("GetJobsFromQueue Case want err = TestError returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpMuxSendJobs(t *testing.T) {
resp := httpMux.SendJobs(&Jobs{queueName: "TestQueue"})
if resp.Code != "200" {
t.Fatalf("SendJobs Case want resp.Code = nil returns, but receive resp = %v", resp)
} else {
t.Log(resp)
}
}
func TestHttpMuxSendJobsErrorCase(t *testing.T) {
resp := httpMux.SendJobs(&Jobs{queueName: "TestError"})
if resp.Msg != "TestError" {
t.Fatalf("SendJobs Case want resp.Msg = TestError returns, but receive resp = %v", resp)
} else {
t.Log(resp)
}
}
func TestHttpMuxSendJobsFailedCase(t *testing.T) {
resp := httpMux.SendJobs(&Jobs{queueName: "TestFailed"})
if resp.Msg != "TestFailed" {
t.Fatalf("SendJobs Case want resp.Msg = TestFailed returns, but receive resp = %v", resp)
} else {
t.Log(resp)
}
}
func TestHttpMuxSendToRetryQueue(t *testing.T) {
err := httpMux.sendToRetryQueue(&Jobs{queueName: "TestQueue", outTraceId: "1"})
if err != nil {
t.Fatalf("SendToRetryQueue Case want err = nil returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpMuxSendToRetryQueueErrorCase(t *testing.T) {
err := httpMux.sendToRetryQueue(&Jobs{queueName: "TestError", outTraceId: "1"})
if err != nil && err.Error() == "TestError" {
t.Log(err)
} else {
t.Fatalf("SendToRetryQueue Error Case want err = TestError returns, but receive err = %v", err)
}
}
func TestHttpLoopPing(t *testing.T) {
var sigChan = make(chan string, 1)
go func() {
httpMux.loopPing()
close(sigChan)
}()
timer := time.NewTicker(1 * time.Second)
for {
select {
case _, ok := <-sigChan:
// channel关闭,RobotGiftPromoteBehaviour停止
if !ok {
t.Log(nil)
return
}
case <-timer.C:
t.Fatalf("LoopPingCGI case should be finished in 1 second, but it haven't finish now!!!")
return
}
}
}
func TestHttpSendPing(t *testing.T) {
err := httpMux.SendPing()
if err != nil {
t.Fatalf("SendPing Case want err = nil returns, but receive err = %v", err)
} else {
t.Log(err)
}
}
func TestHttpSendPingError(t *testing.T) {
httpMux.RemoteSender = &TestErrorRemote{}
err := httpMux.SendPing()
if err != nil && err.Error() == "TestError" {
t.Log(err)
} else {
t.Fatalf("SendPing Case want err = nil returns, but receive err = %v", err)
}
}
func TestHttpSendPingFailed(t *testing.T) {
httpMux.RemoteSender = &TestFailedRemote{}
err := httpMux.SendPing()
if err != nil && strings.Contains(err.Error(), "TestFailed") {
t.Log(err)
} else {
t.Fatalf("SendPing Case want err = nil returns, but receive err = %v", err)
}
}
func TestHttpPingTopicNotRegisted(t *testing.T) {
httpMux.RemoteSender = &TestTopicNotRegistedRemote{}
err := httpMux.SendPing()
if err != nil && err.Error() == "topic no register: TestQueue/TestTopic response is [Queue/TopicNotRegisted]" {
t.Log(err)
} else {
t.Fatalf("SendPing Case want err = 'topic no register: TestQueue/TestTopic' returns, but receive err = %v", err)
}
}