Skip to content

Commit

Permalink
test: refactor tests to improve reliability and maintainability
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Jan 21, 2025
1 parent 05e742a commit 6914a8b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ jobs:
go-build: ~/.cache/go-build
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
env:
GO111MODULE: on
GOPROXY: https://proxy.golang.org

# Service containers to run with `container-job`
services:
nsqd:
image: appleboy/nsqd
ports:
- 4150:4150
- 4151:4151

steps:
- name: Set up Go ${{ matrix.go }}
Expand Down
47 changes: 36 additions & 11 deletions nsq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"go.uber.org/goleak"
)

var host = "127.0.0.1"

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Expand Down Expand Up @@ -86,8 +84,11 @@ func TestNSQDefaultFlow(t *testing.T) {
}

func TestNSQShutdown(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("test2"),
)
q, err := queue.NewQueue(
Expand All @@ -105,11 +106,14 @@ func TestNSQShutdown(t *testing.T) {
}

func TestNSQCustomFuncAndWait(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := &mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("test3"),
WithMaxInFlight(10),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
Expand All @@ -134,11 +138,14 @@ func TestNSQCustomFuncAndWait(t *testing.T) {
}

func TestEnqueueJobAfterShutdown(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host + ":4150"),
WithAddr(endpoint),
)
q, err := queue.NewQueue(
queue.WithWorker(w),
Expand All @@ -156,11 +163,14 @@ func TestEnqueueJobAfterShutdown(t *testing.T) {
}

func TestJobReachTimeout(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("timeout"),
WithMaxInFlight(2),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
Expand Down Expand Up @@ -195,11 +205,14 @@ func TestJobReachTimeout(t *testing.T) {
}

func TestCancelJobAfterShutdown(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "test",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("cancel"),
WithLogger(queue.NewLogger()),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
Expand Down Expand Up @@ -234,11 +247,14 @@ func TestCancelJobAfterShutdown(t *testing.T) {
}

func TestGoroutineLeak(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("GoroutineLeak"),
WithLogger(queue.NewEmptyLogger()),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
Expand Down Expand Up @@ -279,11 +295,14 @@ func TestGoroutineLeak(t *testing.T) {
}

func TestGoroutinePanic(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("GoroutinePanic"),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
panic("missing something")
Expand All @@ -305,11 +324,14 @@ func TestGoroutinePanic(t *testing.T) {
}

func TestNSQStatsinQueue(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("nsq_stats"),
WithRunFunc(func(ctx context.Context, m core.TaskMessage) error {
log.Println("get message")
Expand All @@ -334,11 +356,14 @@ func TestNSQStatsinQueue(t *testing.T) {
}

func TestNSQStatsInWorker(t *testing.T) {
ctx := context.Background()
natsC, endpoint := setupNSQContainer(ctx, t)
defer testcontainers.CleanupContainer(t, natsC)
m := mockMessage{
Message: "foo",
}
w := NewWorker(
WithAddr(host+":4150"),
WithAddr(endpoint),
WithTopic("nsq_stats_queue"),
)

Expand Down

0 comments on commit 6914a8b

Please sign in to comment.