109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/backend/config"
|
|
"lincodex/backend/internal/adapter/secondary/prometheus"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/worker/outboxrelay"
|
|
"lincodex/backend/internal/worker/retry"
|
|
)
|
|
|
|
const relayLifecycleTimeout = 5 * time.Second
|
|
|
|
func TestNewOutboxRelaySkipsWithoutWork(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
settings config.Config
|
|
}{
|
|
{name: "disabled", settings: config.Config{RelayEnabled: false, DatabaseURL: "postgres://app@db/app"}},
|
|
{name: "database not configured", settings: config.Config{RelayEnabled: true}},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
relay, cleanup, err := newOutboxRelay(t.Context(), test.settings, slog.New(slog.DiscardHandler), prometheus.New())
|
|
if err != nil {
|
|
t.Fatalf("newOutboxRelay() error = %v", err)
|
|
}
|
|
cleanup()
|
|
if relay != nil {
|
|
t.Fatal("newOutboxRelay() built a relay without work to do")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// claimCounter is an outbox store with no pending events that counts passes.
|
|
type claimCounter struct {
|
|
passes atomic.Int64
|
|
}
|
|
|
|
func (store *claimCounter) ClaimPending(context.Context, int, outbound.PublishOutboxBatch) error {
|
|
store.passes.Add(1)
|
|
return nil
|
|
}
|
|
|
|
func (*claimCounter) Backlog(context.Context) (outbound.OutboxBacklog, error) {
|
|
return outbound.OutboxBacklog{}, nil
|
|
}
|
|
|
|
type readyPublisher struct{}
|
|
|
|
func (readyPublisher) Prepare(context.Context) error { return nil }
|
|
|
|
func (readyPublisher) Publish(_ context.Context, events []outbound.OutboxEvent) []error {
|
|
return make([]error, len(events))
|
|
}
|
|
|
|
func TestApplicationRunsTheRelayWhileServing(t *testing.T) {
|
|
store := &claimCounter{}
|
|
metrics := prometheus.New()
|
|
logger := slog.New(slog.DiscardHandler)
|
|
relay := outboxrelay.New(store, readyPublisher{}, metrics, logger, outboxrelay.Settings{
|
|
BatchSize: 1,
|
|
PollInterval: time.Millisecond,
|
|
Backoff: retry.Backoff{Initial: time.Millisecond, Max: time.Millisecond},
|
|
ShutdownTimeout: time.Second,
|
|
BacklogInterval: time.Second,
|
|
})
|
|
application := NewApplication(config.Config{
|
|
HTTPAddr: "127.0.0.1:0",
|
|
MetricsAddr: "127.0.0.1:0",
|
|
ShutdownTimeout: time.Second,
|
|
}, http.NotFoundHandler(), metrics, logger, nil, relay, nil, nil, nil, nil, nil)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
finished := make(chan error, 1)
|
|
go func() { finished <- application.run(ctx) }()
|
|
|
|
deadline := time.Now().Add(relayLifecycleTimeout)
|
|
for store.passes.Load() < 2 {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the relay did not poll the outbox while the servers ran")
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-finished:
|
|
if err != nil {
|
|
t.Fatalf("run() error = %v", err)
|
|
}
|
|
case <-time.After(relayLifecycleTimeout):
|
|
t.Fatal("run() did not return after cancellation")
|
|
}
|
|
// run returns only after the relay stopped, so no pass follows.
|
|
passes := store.passes.Load()
|
|
time.Sleep(10 * time.Millisecond)
|
|
if after := store.passes.Load(); after != passes {
|
|
t.Fatalf("the relay ran %d passes after run() returned", after-passes)
|
|
}
|
|
}
|