Skip to content

Commit 6914a8b

Browse files
committed
test: refactor tests to improve reliability and maintainability
- Remove environment variables and service container configuration from GitHub Actions workflow - Remove unused `host` variable in `nsq_test.go` - Replace hardcoded address with dynamic endpoint in multiple tests - Add setup and cleanup of NSQ container in multiple tests Signed-off-by: appleboy <[email protected]>
1 parent 05e742a commit 6914a8b

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

.github/workflows/go.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ jobs:
2929
go-build: ~/.cache/go-build
3030
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
3131
runs-on: ${{ matrix.os }}
32-
env:
33-
GO111MODULE: on
34-
GOPROXY: https://proxy.golang.org
35-
36-
# Service containers to run with `container-job`
37-
services:
38-
nsqd:
39-
image: appleboy/nsqd
40-
ports:
41-
- 4150:4150
42-
- 4151:4151
4332

4433
steps:
4534
- name: Set up Go ${{ matrix.go }}

nsq_test.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"go.uber.org/goleak"
2121
)
2222

23-
var host = "127.0.0.1"
24-
2523
func TestMain(m *testing.M) {
2624
goleak.VerifyTestMain(m)
2725
}
@@ -86,8 +84,11 @@ func TestNSQDefaultFlow(t *testing.T) {
8684
}
8785

8886
func TestNSQShutdown(t *testing.T) {
87+
ctx := context.Background()
88+
natsC, endpoint := setupNSQContainer(ctx, t)
89+
defer testcontainers.CleanupContainer(t, natsC)
8990
w := NewWorker(
90-
WithAddr(host+":4150"),
91+
WithAddr(endpoint),
9192
WithTopic("test2"),
9293
)
9394
q, err := queue.NewQueue(
@@ -105,11 +106,14 @@ func TestNSQShutdown(t *testing.T) {
105106
}
106107

107108
func TestNSQCustomFuncAndWait(t *testing.T) {
109+
ctx := context.Background()
110+
natsC, endpoint := setupNSQContainer(ctx, t)
111+
defer testcontainers.CleanupContainer(t, natsC)
108112
m := &mockMessage{
109113
Message: "foo",
110114
}
111115
w := NewWorker(
112-
WithAddr(host+":4150"),
116+
WithAddr(endpoint),
113117
WithTopic("test3"),
114118
WithMaxInFlight(10),
115119
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
@@ -134,11 +138,14 @@ func TestNSQCustomFuncAndWait(t *testing.T) {
134138
}
135139

136140
func TestEnqueueJobAfterShutdown(t *testing.T) {
141+
ctx := context.Background()
142+
natsC, endpoint := setupNSQContainer(ctx, t)
143+
defer testcontainers.CleanupContainer(t, natsC)
137144
m := mockMessage{
138145
Message: "foo",
139146
}
140147
w := NewWorker(
141-
WithAddr(host + ":4150"),
148+
WithAddr(endpoint),
142149
)
143150
q, err := queue.NewQueue(
144151
queue.WithWorker(w),
@@ -156,11 +163,14 @@ func TestEnqueueJobAfterShutdown(t *testing.T) {
156163
}
157164

158165
func TestJobReachTimeout(t *testing.T) {
166+
ctx := context.Background()
167+
natsC, endpoint := setupNSQContainer(ctx, t)
168+
defer testcontainers.CleanupContainer(t, natsC)
159169
m := mockMessage{
160170
Message: "foo",
161171
}
162172
w := NewWorker(
163-
WithAddr(host+":4150"),
173+
WithAddr(endpoint),
164174
WithTopic("timeout"),
165175
WithMaxInFlight(2),
166176
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
@@ -195,11 +205,14 @@ func TestJobReachTimeout(t *testing.T) {
195205
}
196206

197207
func TestCancelJobAfterShutdown(t *testing.T) {
208+
ctx := context.Background()
209+
natsC, endpoint := setupNSQContainer(ctx, t)
210+
defer testcontainers.CleanupContainer(t, natsC)
198211
m := mockMessage{
199212
Message: "test",
200213
}
201214
w := NewWorker(
202-
WithAddr(host+":4150"),
215+
WithAddr(endpoint),
203216
WithTopic("cancel"),
204217
WithLogger(queue.NewLogger()),
205218
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
@@ -234,11 +247,14 @@ func TestCancelJobAfterShutdown(t *testing.T) {
234247
}
235248

236249
func TestGoroutineLeak(t *testing.T) {
250+
ctx := context.Background()
251+
natsC, endpoint := setupNSQContainer(ctx, t)
252+
defer testcontainers.CleanupContainer(t, natsC)
237253
m := mockMessage{
238254
Message: "foo",
239255
}
240256
w := NewWorker(
241-
WithAddr(host+":4150"),
257+
WithAddr(endpoint),
242258
WithTopic("GoroutineLeak"),
243259
WithLogger(queue.NewEmptyLogger()),
244260
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
@@ -279,11 +295,14 @@ func TestGoroutineLeak(t *testing.T) {
279295
}
280296

281297
func TestGoroutinePanic(t *testing.T) {
298+
ctx := context.Background()
299+
natsC, endpoint := setupNSQContainer(ctx, t)
300+
defer testcontainers.CleanupContainer(t, natsC)
282301
m := mockMessage{
283302
Message: "foo",
284303
}
285304
w := NewWorker(
286-
WithAddr(host+":4150"),
305+
WithAddr(endpoint),
287306
WithTopic("GoroutinePanic"),
288307
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
289308
panic("missing something")
@@ -305,11 +324,14 @@ func TestGoroutinePanic(t *testing.T) {
305324
}
306325

307326
func TestNSQStatsinQueue(t *testing.T) {
327+
ctx := context.Background()
328+
natsC, endpoint := setupNSQContainer(ctx, t)
329+
defer testcontainers.CleanupContainer(t, natsC)
308330
m := mockMessage{
309331
Message: "foo",
310332
}
311333
w := NewWorker(
312-
WithAddr(host+":4150"),
334+
WithAddr(endpoint),
313335
WithTopic("nsq_stats"),
314336
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
315337
log.Println("get message")
@@ -334,11 +356,14 @@ func TestNSQStatsinQueue(t *testing.T) {
334356
}
335357

336358
func TestNSQStatsInWorker(t *testing.T) {
359+
ctx := context.Background()
360+
natsC, endpoint := setupNSQContainer(ctx, t)
361+
defer testcontainers.CleanupContainer(t, natsC)
337362
m := mockMessage{
338363
Message: "foo",
339364
}
340365
w := NewWorker(
341-
WithAddr(host+":4150"),
366+
WithAddr(endpoint),
342367
WithTopic("nsq_stats_queue"),
343368
)
344369

0 commit comments

Comments
 (0)