Skip to content

Commit c6d47dc

Browse files
committed
feat: refactor GitHub Actions and modify NATS tests
- Modify the `go.yml` file in the `.github/workflows` directory - Add a new job named `nats01` in the `go.yml` file - Add a new job named `nats02` in the `go.yml` file - Modify the `nats_test.go` file - Change the value of the `host` variable in the `nats_test.go` file - Modify the `TestDefaultFlow` function in the `nats_test.go` file - Modify the `TestShutdown` function in the `nats_test.go` file - Modify the `TestCustomFuncAndWait` function in the `nats_test.go` file - Modify the `TestEnqueueJobAfterShutdown` function in the `nats_test.go` file - Modify the `TestJobReachTimeout` function in the `nats_test.go` file - Modify the `TestCancelJobAfterShutdown` function in the `nats_test.go` file - Modify the `TestGoroutineLeak` function in the `nats_test.go` file - Modify the `TestGoroutinePanic` function in the `nats_test.go` file - Modify the `TestReQueueTaskInWorkerBeforeShutdown` function in the `nats_test.go` file - Modify the `options.go` file - Change the `WithAddr` function in the `options.go` file Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 0ddabe8 commit c6d47dc

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

.github/workflows/go.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ jobs:
3333

3434
# Service containers to run with `container-job`
3535
services:
36-
nats:
36+
nats01:
3737
image: nats
3838
ports:
3939
- 4222:4222
40+
nats02:
41+
image: nats
42+
ports:
43+
- 4223:4222
4044

4145
steps:
4246
- name: Set up Go ${{ matrix.go }}

nats_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"github.com/golang-queue/queue"
1313
"github.com/golang-queue/queue/core"
1414
"github.com/golang-queue/queue/job"
15+
"github.com/nats-io/nats.go"
1516

1617
"github.com/stretchr/testify/assert"
1718
"go.uber.org/goleak"
1819
)
1920

20-
var host = "127.0.0.1"
21+
var host = nats.DefaultURL
2122

2223
func TestMain(m *testing.M) {
2324
goleak.VerifyTestMain(m)
@@ -36,7 +37,7 @@ func TestDefaultFlow(t *testing.T) {
3637
Message: "foo",
3738
}
3839
w := NewWorker(
39-
WithAddr(host+":4222"),
40+
WithAddr(host),
4041
WithSubj("test"),
4142
WithQueue("test"),
4243
)
@@ -54,7 +55,7 @@ func TestDefaultFlow(t *testing.T) {
5455

5556
func TestShutdown(t *testing.T) {
5657
w := NewWorker(
57-
WithAddr(host+":4222"),
58+
WithAddr(host),
5859
WithSubj("test"),
5960
WithQueue("test"),
6061
)
@@ -76,7 +77,7 @@ func TestCustomFuncAndWait(t *testing.T) {
7677
Message: "foo",
7778
}
7879
w := NewWorker(
79-
WithAddr(host+":4222"),
80+
WithAddr(host),
8081
WithSubj("test"),
8182
WithQueue("test"),
8283
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
@@ -107,7 +108,7 @@ func TestEnqueueJobAfterShutdown(t *testing.T) {
107108
Message: "foo",
108109
}
109110
w := NewWorker(
110-
WithAddr(host + ":4222"),
111+
WithAddr(host),
111112
)
112113
q, err := queue.NewQueue(
113114
queue.WithWorker(w),
@@ -129,7 +130,7 @@ func TestJobReachTimeout(t *testing.T) {
129130
Message: "foo",
130131
}
131132
w := NewWorker(
132-
WithAddr(host+":4222"),
133+
WithAddr(host),
133134
WithSubj("JobReachTimeout"),
134135
WithQueue("test"),
135136
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
@@ -169,7 +170,7 @@ func TestCancelJobAfterShutdown(t *testing.T) {
169170
Message: "test",
170171
}
171172
w := NewWorker(
172-
WithAddr(host+":4222"),
173+
WithAddr(host),
173174
WithSubj("CancelJob"),
174175
WithQueue("test"),
175176
WithLogger(queue.NewLogger()),
@@ -210,7 +211,7 @@ func TestGoroutineLeak(t *testing.T) {
210211
Message: "foo",
211212
}
212213
w := NewWorker(
213-
WithAddr(host+":4222"),
214+
WithAddr(host),
214215
WithSubj("GoroutineLeak"),
215216
WithQueue("test"),
216217
WithLogger(queue.NewEmptyLogger()),
@@ -256,7 +257,7 @@ func TestGoroutinePanic(t *testing.T) {
256257
Message: "foo",
257258
}
258259
w := NewWorker(
259-
WithAddr(host+":4222"),
260+
WithAddr(host),
260261
WithSubj("GoroutinePanic"),
261262
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
262263
panic("missing something")
@@ -282,7 +283,7 @@ func TestReQueueTaskInWorkerBeforeShutdown(t *testing.T) {
282283
Payload: []byte("foo"),
283284
}
284285
w := NewWorker(
285-
WithAddr(host+":4222"),
286+
WithAddr(host),
286287
WithSubj("test02"),
287288
WithQueue("test02"),
288289
)

options.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package nats
22

33
import (
44
"context"
5+
"strings"
56

67
"github.com/golang-queue/queue"
78
"github.com/golang-queue/queue/core"
9+
10+
"github.com/nats-io/nats.go"
811
)
912

1013
// Option for queue system
@@ -19,9 +22,11 @@ type options struct {
1922
}
2023

2124
// WithAddr setup the addr of NATS
22-
func WithAddr(addr string) Option {
25+
func WithAddr(addrs ...string) Option {
2326
return func(w *options) {
24-
w.addr = "nats://" + addr
27+
if len(addrs) > 0 {
28+
w.addr = strings.Join(addrs, ",")
29+
}
2530
}
2631
}
2732

@@ -55,7 +60,7 @@ func WithLogger(l queue.Logger) Option {
5560

5661
func newOptions(opts ...Option) options {
5762
defaultOpts := options{
58-
addr: "127.0.0.1:4222",
63+
addr: nats.DefaultURL,
5964
subj: "foobar",
6065
queue: "foobar",
6166
logger: queue.NewLogger(),

0 commit comments

Comments
 (0)