mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSh3B3Gq4bN4yoC4QQ
This commit is contained in:
2 files changed
+15
-5
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user