From ae9bbc9a6adbef57a7c5eab9e9aeda2daa4a596d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 23:22:50 +0000 Subject: [PATCH] Stock quotes: the Alpaca test runs on a fixed clock TestAlpacaStream read the real clock, so once Monday 9 November 2026 had passed the book called the session closed and gave Monday's quote a close. Alpaca takes a Now clock (time.Now if nil), and the test sets it to Monday 9:33 Eastern. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRSh3B3Gq4bN4yoC4QQ --- internal/quotes/alpaca.go | 14 +++++++++++--- internal/quotes/alpaca_test.go | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/quotes/alpaca.go b/internal/quotes/alpaca.go index 48df5ee..a67cab1 100644 --- a/internal/quotes/alpaca.go +++ b/internal/quotes/alpaca.go @@ -40,6 +40,7 @@ type Alpaca struct { StreamURL string // AlpacaStream if "" (the feed is added) DataURL string // AlpacaData if "" Log func(format string, args ...any) + Now func() time.Time // the clock (time.Now if nil): what session it is, how fresh a snapshot is mu sync.Mutex wanted map[string]time.Time // when each symbol was last wanted @@ -49,6 +50,13 @@ type Alpaca struct { err error // the stream's last failure (nil: connected) } +func (a *Alpaca) now() time.Time { + if a.Now != nil { + return a.Now() + } + return time.Now() +} + func (a *Alpaca) init() { if a.wanted == nil { a.wanted, a.fetched, a.subbed = map[string]time.Time{}, map[string]time.Time{}, map[string]bool{} @@ -100,7 +108,7 @@ func (a *Alpaca) Err() error { func (a *Alpaca) Want(ctx context.Context, syms []string) { a.mu.Lock() a.init() - now := time.Now() + now := a.now() var fetch []string for _, s := range syms { if s = Normalize(s); s == "" { @@ -255,7 +263,7 @@ func (a *Alpaca) fetch(ctx context.Context, syms []string) error { if err := json.Unmarshal(body, &snaps); err != nil { return fmt.Errorf("Alpaca: %w", err) } - now := time.Now() + now := a.now() a.mu.Lock() for _, s := range syms { a.fetched[s] = now @@ -358,7 +366,7 @@ func (a *Alpaca) clock(ctx context.Context) { defer t.Stop() n := 0 for { - a.Book.Clock(time.Now()) + a.Book.Clock(a.now()) if n++; n%5 == 0 { var syms []string for s := range a.following() { diff --git a/internal/quotes/alpaca_test.go b/internal/quotes/alpaca_test.go index 6bf3d42..2b05b0a 100644 --- a/internal/quotes/alpaca_test.go +++ b/internal/quotes/alpaca_test.go @@ -117,12 +117,14 @@ func TestAlpacaSnapshots(t *testing.T) { // TestAlpacaStream: Friday's quote from the snapshot, then Monday's trades // on the stream begin the quote again, with Friday's close the change is -// from. +// from. The clock is Monday 9:33, just after the open, whatever day the +// test runs (later, with no clock tick since, the session would be closed). func TestAlpacaStream(t *testing.T) { srv, subs := fakeAlpaca(t) ws := "ws" + strings.TrimPrefix(srv.URL, "http") + "/v2/" + monday := time.Date(2026, 11, 9, 9, 33, 0, 0, eastern) a := &Alpaca{Key: "KEY", Secret: "SECRET", Book: NewBook(nil), Pinned: []string{"AAPL"}, - DataURL: srv.URL + "/v2/", StreamURL: ws} + DataURL: srv.URL + "/v2/", StreamURL: ws, Now: func() time.Time { return monday }} ctx, cancel := context.WithCancel(context.Background()) defer cancel() go a.Run(ctx)