Skip to content

Job and Queue Tests ​

Continue from Run a Photo Job. Test payload encoding and job dispatch separately, then verify the real handler through the App. This keeps a broken queue contract distinguishable from a database or runtime failure.

Payload Tests ​

Create internal/photos/check_job_test.go. This test uses the job's actual payload type and identifier:

go
package photos

import (
	"testing"

	"github.com/goforj/queue"
)

// TestCheckJobPayload verifies the photo reference survives queue encoding.
func TestCheckJobPayload(t *testing.T) {
	job := queue.NewJob(CheckJobTypeName).Payload(CheckJobPayload{PhotoID: 42})
	if job.Type != "photos:check" || string(job.PayloadBytes()) != `{"photo_id":42}` {
		t.Fatalf("job = %s %s, want photos:check with numeric photo_id", job.Type, job.PayloadBytes())
	}
	var payload CheckJobPayload
	if err := job.Bind(&payload); err != nil {
		t.Fatal(err)
	}
	if payload.PhotoID != 42 {
		t.Fatalf("photo ID = %d, want 42", payload.PhotoID)
	}
}

This checks the stable job name, numeric photo_id wire format, and decoding. It does not enqueue work or prove that the App registered CheckJob.HandleTask.

Dispatch Tests ​

Add these imports and the following test to the same file. Replace your/module with your Project's module path:

go
import (
	"context"
	"time"

	"your/module/internal/database"
	"your/module/internal/inspects"
	"your/module/internal/queues"
)

// TestCheckJobDispatch verifies the real Queue method reaches the generated default queue.
func TestCheckJobDispatch(t *testing.T) {
	t.Setenv("QUEUE_DRIVER", "workerpool")
	t.Setenv("QUEUE_WORKERS", "1")
	t.Setenv("QUEUE_NAME", "default")
	accepted := make(chan queue.Event, 1)
	observer := queue.ObserverFunc(func(_ context.Context, event queue.Event) {
		if event.Kind == queue.EventEnqueueAccepted {
			accepted <- event
		}
	})
	manager, err := queues.NewManagerWithObserver(observer, nil, nil)
	if err != nil {
		t.Fatal(err)
	}
	runtime := manager.Default()
	t.Cleanup(func() {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()
		if err := runtime.Shutdown(ctx); err != nil {
			t.Error(err)
		}
	})
	received := make(chan CheckJobPayload, 1)
	manager.Register(CheckJobTypeName, func(_ context.Context, msg queue.Message) error {
		var payload CheckJobPayload
		if err := msg.Bind(&payload); err != nil {
			return err
		}
		received <- payload
		return nil
	})
	if err := runtime.StartWorkers(context.Background()); err != nil {
		t.Fatal(err)
	}

	connections := database.NewConnections(inspects.NewManager())
	t.Cleanup(func() { _ = connections.Close(context.Background()) })
	job := NewCheckJob(manager, NewPhotoRepo(connections))
	if err := job.Queue(context.Background(), CheckJobPayload{PhotoID: 42}); err != nil {
		t.Fatal(err)
	}
	select {
	case payload := <-received:
		if payload.PhotoID != 42 {
			t.Fatalf("photo ID = %d, want 42", payload.PhotoID)
		}
	case <-time.After(5 * time.Second):
		t.Fatal("photo job was not delivered")
	}
	select {
	case event := <-accepted:
		if event.Queue != "default" || event.MaxRetry != 1 {
			t.Fatalf("enqueue = %+v, want default queue with one retry", event)
		}
	case <-time.After(5 * time.Second):
		t.Fatal("photo job had no enqueue event")
	}
}

The generated manager uses workerpool, already supported by the Photo Job App. The test constructs the repository but registers a recording handler in place of the real database lookup. It proves the CheckJob.Queue method sends the photo ID to the default queue with a one-retry budget. It does not prove the handler loads a saved record or that a retry actually runs.

Changing QUEUE_DRIVER selects an already compiled driver. Add a driver to QUEUE_SUPPORTED_DRIVERS and run forj build before testing a driver your generated manager does not yet support. The null driver deliberately skips execution, so use it only when that is the contract under test.

Worker Tests ​

Use the Photo Job verification to exercise generated registration, the real repository lookup, and completion after the HTTP response. Its retry demonstration proves one failure followed by success with the local driver.

For a production broker, add backend integration coverage for acknowledgement, redelivery, restart recovery, and shutdown with work in flight. Keep retry, backoff, timeout, and idempotency checks tied to the selected driver's behavior. The local test above establishes none of those broker guarantees.

Verify ​

From the Project root:

bash
go test ./internal/photos -run 'TestCheckJob(Payload|Dispatch)$'

Expected result: both tests pass without a broker or a database connection. make:job photos:check places the job in internal/photos, so run that feature package rather than the framework-owned internal/jobs package.

Next Steps ​