63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
// TestCachedStoreOpensOnceAndSharesTheSameStore (review round 1 finding 2, issue 1517/1518): two separate
|
|
// callers (as the Bundle's own tool calls and hearLoop's handler are, in the running bundle) each calling
|
|
// get get back the exact same Store, and open — the thing that would have opened a fresh *pgxpool.Pool
|
|
// every time — is called exactly once.
|
|
func TestCachedStoreOpensOnceAndSharesTheSameStore(t *testing.T) {
|
|
opens := 0
|
|
want := newMemStore()
|
|
c := newCachedStore(func(context.Context) (Store, error) {
|
|
opens++
|
|
return want, nil
|
|
})
|
|
first, err := c.get(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := c.get(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first != want || second != want {
|
|
t.Fatalf("get returned %v and %v, want both to be %v", first, second, want)
|
|
}
|
|
if opens != 1 {
|
|
t.Fatalf("open was called %d time(s), want 1 (opened once, shared everywhere)", opens)
|
|
}
|
|
}
|
|
|
|
// TestCachedStoreRetriesAfterAFailedOpen: a failed open is retried on the NEXT call, never looped on its
|
|
// own — the caller already retries (hearLoop's own backoff, a tool call's own caller).
|
|
func TestCachedStoreRetriesAfterAFailedOpen(t *testing.T) {
|
|
opens := 0
|
|
want := newMemStore()
|
|
failWanted := errors.New("the index cannot be reached")
|
|
c := newCachedStore(func(context.Context) (Store, error) {
|
|
opens++
|
|
if opens == 1 {
|
|
return nil, failWanted
|
|
}
|
|
return want, nil
|
|
})
|
|
if _, err := c.get(context.Background()); err != failWanted {
|
|
t.Fatalf("first get: %v, want %v", err, failWanted)
|
|
}
|
|
got, err := c.get(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != want {
|
|
t.Fatalf("second get = %v, want %v", got, want)
|
|
}
|
|
if opens != 2 {
|
|
t.Fatalf("open was called %d time(s), want 2 (the failed attempt, then the one that succeeded)", opens)
|
|
}
|
|
}
|