The builder kept the catalog's versions-filled in memory only and its durable consumer took the event once, so a restart left every build wait-held. It now reads the last message of the subject from the events stream at its start (a read that fails is retried with backoff in the background). Builds: design 47 G A-408, §369, §372
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestTheLastVersionsFilledIsReadAtStart(t *testing.T) {
|
|
boom := errors.New("bus down")
|
|
for _, tc := range []struct {
|
|
name string
|
|
results []error // per read: nil found, boom fails
|
|
notFound bool
|
|
wantFilled bool
|
|
wantReads int
|
|
waits []time.Duration
|
|
}{
|
|
{"kept", []error{nil}, false, true, 1, nil},
|
|
{"none kept", []error{nil}, true, false, 1, nil},
|
|
{"read fails then kept", []error{boom, boom, nil}, false, true, 3, []time.Duration{time.Second, 2 * time.Second}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
rv := newReservations(nil, nil, nil, nil, nil, t.Logf)
|
|
var mu sync.Mutex
|
|
woke := make(chan struct{}, 1)
|
|
rv.wake = func(context.Context) { woke <- struct{}{} }
|
|
reads := 0
|
|
read := func(_ context.Context, subject string) (bool, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
err := tc.results[reads]
|
|
reads++
|
|
return !tc.notFound, err
|
|
}
|
|
var waits []time.Duration
|
|
wait := func(_ context.Context, d time.Duration) bool {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
waits = append(waits, d)
|
|
return true
|
|
}
|
|
rv.readFilled(context.Background(), read, wait)
|
|
if tc.wantFilled {
|
|
select {
|
|
case <-woke:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the read did not take the fill")
|
|
}
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if rv.Filled() != tc.wantFilled || reads != tc.wantReads {
|
|
t.Fatalf("filled %v reads %d", rv.Filled(), reads)
|
|
}
|
|
if !reflect.DeepEqual(waits, tc.waits) {
|
|
t.Fatalf("backoff %v, want %v", waits, tc.waits)
|
|
}
|
|
})
|
|
}
|
|
}
|