From 493f4590f969387d7baebf5d7db35d91767cf472 Mon Sep 17 00:00:00 2001 From: Mats Linander Date: Sat, 6 Jan 2024 14:37:28 -0500 Subject: [PATCH] manager: static must be passed to LoadRealtime (#24) --- cmd/main.go | 10 +++------- manager.go | 8 +------- manager_test.go | 42 ++++++++++++++---------------------------- 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index a08cae3..2cbf8d6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) @@ -140,7 +137,6 @@ func LoadRealtimeFeed() (*gtfs.Realtime, error) { } for k, v := range shared { - sh[k] = v rh[k] = v } @@ -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 } diff --git a/manager.go b/manager.go index 80b2960..704ec4b 100644 --- a/manager.go +++ b/manager.go @@ -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, diff --git a/manager_test.go b/manager_test.go index 75f908d..01cb0a7 100644 --- a/manager_test.go +++ b/manager_test.go @@ -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() @@ -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, ) @@ -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, ) @@ -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, ) @@ -798,8 +787,7 @@ 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, ) @@ -807,8 +795,7 @@ func testManagerLoadRealtime(t *testing.T, strg storage.Storage) { // Missing data is also error _, err = m.LoadRealtime( - "app1", - server.Server.URL+"/static.zip", nil, + "app1", static, server.Server.URL+"/missing.pb", nil, when, ) @@ -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, )