Skip to content

Commit

Permalink
manager: static must be passed to LoadRealtime
Browse files Browse the repository at this point in the history
  • Loading branch information
matslina committed Jan 6, 2024
1 parent e30f5c6 commit 7de879b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 42 deletions.
10 changes: 3 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,10 @@ func LoadRealtimeFeed() (*gtfs.Realtime, error) {
if realtimeURL == "" {
return nil, fmt.Errorf("realtime URL is required")
}
if staticURL == "" {
return nil, fmt.Errorf("static URL is required")
}

sh, err := parseHeaders(staticHeaders)
static, err := LoadStaticFeed()
if err != nil {
return nil, fmt.Errorf("invalid static header: %w", err)
return nil, fmt.Errorf("loading static feed: %w", err)
}

rh, err := parseHeaders(realtimeHeaders)
Expand All @@ -140,7 +137,6 @@ func LoadRealtimeFeed() (*gtfs.Realtime, error) {
}

for k, v := range shared {
sh[k] = v
rh[k] = v
}

Expand All @@ -156,7 +152,7 @@ func LoadRealtimeFeed() (*gtfs.Realtime, error) {
manager := gtfs.NewManager(s)
manager.Downloader = fs

realtime, err := manager.LoadRealtime("cli", staticURL, sh, realtimeURL, rh, time.Now())
realtime, err := manager.LoadRealtime("cli", static, realtimeURL, rh, time.Now())
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,12 @@ func (m *Manager) LoadStaticAsync(
// Loads realtime GTFS data from a static and realtime feed.
func (m *Manager) LoadRealtime(
consumer string,
staticURL string,
staticHeaders map[string]string,
static *Static,
realtimeURL string,
realtimeHeaders map[string]string,
when time.Time,
) (*Realtime, error) {

static, err := m.LoadStaticAsync(consumer, staticURL, staticHeaders, when)
if err != nil {
return nil, fmt.Errorf("loading static: %w", err)
}

feedData, err := m.Downloader.Get(
context.Background(),
realtimeURL,
Expand Down
42 changes: 14 additions & 28 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,17 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
server := managerFixture()
defer server.Server.Close()

server.Feeds["/static.zip"] = testutil.BuildZip(t, validFeed())
server.Feeds["/realtime.pb"] = validRealtimeFeed(t, time.Unix(12345, 0))
server.Feeds["/static.zip"] = testutil.BuildZip(t, validFeed())

when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)

_, err := m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
require.NoError(t, m.Refresh(context.Background()))
static, err := m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)

// Mock clock on the downloader to control caching
now := time.Now()
dl := downloader.NewMemory()
Expand All @@ -746,24 +752,9 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
}
m.Downloader = dl

// Realtime feed will initially not load, as static feed is
// not yet loaded.
realtime, err := m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
server.Server.URL+"/realtime.pb", nil,
when,
)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, realtime)

// A Manager.Refresh() will load the static feed
assert.NoError(t, m.Refresh(context.Background()))

// Realtime feed can now be loaded
realtime, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
realtime, err := m.LoadRealtime(
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
Expand All @@ -775,8 +766,7 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {

// Old is still served from cache
realtime, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
Expand All @@ -787,8 +777,7 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
// will be retrieved
now = now.Add(3 * time.Minute)
realtime, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
Expand All @@ -798,17 +787,15 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
// Bad data results in error
server.Feeds["/bad.pb"] = []byte("this isn't protobuf")
_, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
"app1", static,
server.Server.URL+"/bad.pb", nil,
when,
)
assert.Error(t, err, "umarshaling protobuf")

// Missing data is also error
_, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
"app1", static,
server.Server.URL+"/missing.pb", nil,
when,
)
Expand All @@ -817,8 +804,7 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
// 404 isn't cached
server.Feeds["/missing.pb"] = validRealtimeFeed(t, time.Unix(12348, 0))
realtime, err = m.LoadRealtime(
"app1",
server.Server.URL+"/static.zip", nil,
"app1", static,
server.Server.URL+"/missing.pb", nil,
when,
)
Expand Down

0 comments on commit 7de879b

Please sign in to comment.