Skip to content

Test requeuing #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pool/worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pool

import (
"strings"
"testing"
"time"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWorkerRequeueJobs(t *testing.T) {
var (
ctx = testContext(t)
testName = strings.Replace(t.Name(), "/", "_", -1)
rdb = redis.NewClient(&redis.Options{Addr: "localhost:6379", Password: redisPwd})
node = newTestNode(t, ctx, rdb, testName)
)
defer cleanup(t, rdb, false, testName)

// Create a worker and dispatch a job
worker := newTestWorker(t, ctx, node)
handler := worker.handler.(*mockHandler)
assert.NoError(t, node.DispatchJob(ctx, testName, []byte("payload")))

// Wait for the job to start
require.Eventually(t, func() bool { return len(handler.jobs) == 1 }, max, delay)
assert.Equal(t, []byte("payload"), handler.jobs[testName].Payload)

// stop refreshing keep-alive and remove the worker from the node's keep-alive map
worker.workerTTL = time.Hour
_, err := node.keepAliveMap.Delete(ctx, worker.ID)
require.NoError(t, err)
require.Eventually(t, func() bool { return len(node.keepAliveMap.Map()) == 0 }, time.Second, delay)

// Create a new worker to pick up the requeued job
newWorker := newTestWorker(t, ctx, node)
newHandler := newWorker.handler.(*mockHandler)

// Wait for the job to be requeued and started by the new worker
// Increase 'max' to cover the time until requeue happens
require.Eventually(t, func() bool { return len(newHandler.jobs) == 1 }, time.Second, delay)
assert.Equal(t, []byte("payload"), newHandler.jobs[testName].Payload)

// Clean up
assert.NoError(t, node.Shutdown(ctx))
}