fix: fail closed on cancelled Connect and timeout RPC fallback

This commit is contained in:
2026-09-01 14:00:28 -05:00
parent eb3077ae2a
commit f52a19181d
2 changed files with 50 additions and 3 deletions
+43
View File
@@ -1,11 +1,14 @@
package rh_test
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"s1d3sw1ped/robinhood-agentic-mcp"
@@ -44,8 +47,48 @@ func TestConnect_rpcFallback(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if api.Client.HTTP == nil {
t.Fatal("nil HTTP client")
}
if api.Client.HTTP.Timeout != 60*time.Second {
t.Fatalf("HTTP.Timeout = %v", api.Client.HTTP.Timeout)
}
_, err = api.Equity.Quotes(t.Context(), equity.QuotesRequest{Symbols: []string{"MU"}})
if err != nil {
t.Fatal(err)
}
}
func TestConnect_canceledContext(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "tokens.json")
if err := auth.WriteTokens(path, "tok", ""); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(t.Context())
cancel()
api, err := rh.Connect(ctx, rh.Config{URL: "http://127.0.0.1:1", TokenFile: path, Name: "tradey"})
if api != nil {
t.Fatal("expected nil API")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("%v", err)
}
}
func TestConnect_deadlineExceeded(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "tokens.json")
if err := auth.WriteTokens(path, "tok", ""); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-time.Second))
defer cancel()
api, err := rh.Connect(ctx, rh.Config{URL: "http://127.0.0.1:1", TokenFile: path, Name: "tradey"})
if api != nil {
t.Fatal("expected nil API")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("%v", err)
}
}