Skip to content

Event Tests ​

Use Events to add photos.SavedEvent to your App. This test verifies that the local bus delivers the saved photo ID to a typed subscriber.

Local Driver ​

Include inproc in EVENTS_SUPPORTED_DRIVERS when building the App. The test selects it with t.Setenv before creating the bus.

text
EVENTS_DRIVER=inproc
EVENTS_SUPPORTED_DRIVERS=inproc

Add a test across processes with the selected backend when the App relies on distributed delivery.

Publish and Subscribe ​

Add this test to internal/photos/saved_event_test.go. Replace your/module with the module path in go.mod.

go
package photos

import (
	"testing"
	"time"

	"your/module/internal/events"
)

// TestInprocPublishAndSubscribe proves typed delivery without a broker.
func TestInprocPublishAndSubscribe(t *testing.T) {
	t.Setenv("EVENTS_DRIVER", "inproc")

	ctx := t.Context()
	bus := events.NewBus(ctx)
	defer func() { _ = bus.Close(ctx) }()

	received := make(chan SavedEvent, 1)
	sub, err := bus.WithContext(ctx).Subscribe(func(event SavedEvent) {
		received <- event
	})
	if err != nil {
		t.Fatalf("subscribe: %v", err)
	}
	defer func() { _ = sub.Close() }()

	want := SavedEvent{PhotoID: 42}
	if err := bus.WithContext(ctx).Publish(want); err != nil {
		t.Fatalf("publish: %v", err)
	}

	select {
	case event := <-received:
		if event.PhotoID != want.PhotoID {
			t.Fatalf("photo ID = %d, want %d", event.PhotoID, want.PhotoID)
		}
	case <-time.After(500 * time.Millisecond):
		t.Fatal("timed out waiting for in-process event delivery")
	}
}

NewBus returns one lifecycle-aware Bus. Use NewManagerWithContext instead when the test specifically needs to assert manager initialization errors.

Subscriber Tests ​

Call Handle directly when the subscriber delegates to a service. If it dispatches a job, assert the job type and payload using Job and Queue Tests. Use the subscriber smoke check to cover Wire registration and publication from the HTTP request.

Verify ​

Run the photo package test.

bash
go test ./internal/photos -run TestInprocPublishAndSubscribe

The package reports ok when the subscriber receives photo ID 42. A missing delivery fails with a timeout.

Next Steps ​