-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
866 lines (744 loc) · 27.2 KB
/
manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
package gtfs_test
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
proto "google.golang.org/protobuf/proto"
"tidbyt.dev/gtfs"
"tidbyt.dev/gtfs/downloader"
p "tidbyt.dev/gtfs/proto"
"tidbyt.dev/gtfs/storage"
"tidbyt.dev/gtfs/testutil"
)
type MockGTFSServer struct {
Feeds map[string][]byte
RequiredHeaders map[string]map[string]string
Requests []string
Server *httptest.Server
}
func (m *MockGTFSServer) handler(w http.ResponseWriter, r *http.Request) {
m.Requests = append(m.Requests, r.URL.Path)
feed, found := m.Feeds[r.URL.Path]
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
required := m.RequiredHeaders[r.URL.Path]
if len(required) == 0 {
w.Write(feed)
return
}
for k, v := range required {
if r.Header.Get(k) != v {
w.WriteHeader(http.StatusUnauthorized)
return
}
}
w.Write(feed)
}
func managerFixture() *MockGTFSServer {
m := &MockGTFSServer{
Feeds: map[string][]byte{},
Requests: []string{},
}
m.Server = httptest.NewServer(http.HandlerFunc(m.handler))
return m
}
func validFeed() map[string][]string {
return map[string][]string{
"agency.txt": []string{
"agency_timezone,agency_name,agency_url",
"America/Los_Angeles,Fake Agency,http://agency/index.html",
},
"routes.txt": []string{
"route_id,route_short_name,route_type",
"r,R,3",
},
"calendar.txt": []string{
"service_id,monday,start_date,end_date",
"mondays,1,20190101,20190301",
},
"calendar_dates.txt": []string{
"service_id,date,exception_type",
"mondays,20190302,1",
},
"trips.txt": []string{
"route_id,service_id,trip_id",
"r,mondays,t",
},
"stops.txt": []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s,S,12,34",
},
"stop_times.txt": []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s,1",
},
}
}
func validRealtimeFeed(t *testing.T, timestamp time.Time) []byte {
incrementality := p.FeedHeader_FULL_DATASET
tripScheduleRelationship := p.TripDescriptor_SCHEDULED
data, err := proto.Marshal(&p.FeedMessage{
Header: &p.FeedHeader{
GtfsRealtimeVersion: proto.String("2.0"),
Incrementality: &incrementality,
Timestamp: proto.Uint64(uint64(timestamp.Unix())),
},
Entity: []*p.FeedEntity{
{
Id: proto.String("t"),
TripUpdate: &p.TripUpdate{
Trip: &p.TripDescriptor{
TripId: proto.String("t"),
ScheduleRelationship: &tripScheduleRelationship,
},
StopTimeUpdate: []*p.TripUpdate_StopTimeUpdate{
{
StopId: proto.String("s"),
Arrival: &p.TripUpdate_StopTimeEvent{
Delay: proto.Int32(12),
},
},
},
},
},
},
})
require.NoError(t, err)
return data
}
func testManagerLoadSingleFeed(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
server.Feeds["/static.zip"] = testutil.BuildZip(t, validFeed())
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
// First Load will fail, coz feed is new.
s, err := m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// Refresh will load the feed
require.NoError(t, m.Refresh(context.Background()))
// So next Load will succeed
s, err = m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
// Static is now loaded and serves data
stops, err := s.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S", stops[0].Name)
}
func testManagerLoadMultipleURLs(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
// Two different static feeds, served on separate URLs.
files := validFeed()
server.Feeds["/static1.zip"] = testutil.BuildZip(t, validFeed())
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s2,S2,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s2,1",
}
server.Feeds["/static2.zip"] = testutil.BuildZip(t, files)
// First request for each will fail, but create requests.
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
s1, err := m.LoadStaticAsync("a", server.Server.URL+"/static1.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, s1)
s2, err := m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, s2)
// Refresh will load both feeds
require.NoError(t, m.Refresh(context.Background()))
// Both can now be loaded
s1, err = m.LoadStaticAsync("a", server.Server.URL+"/static1.zip", nil, when)
require.NoError(t, err)
s2, err = m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", nil, when)
require.NoError(t, err)
// And can be read
stops, err := s1.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S", stops[0].Name)
stops, err = s2.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S2", stops[0].Name)
}
func testManagerLoadWithHeaders(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
// Three feeds, on separate URLs.
files := validFeed()
server.Feeds["/static1.zip"] = testutil.BuildZip(t, validFeed())
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s2,S2,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s2,1",
}
server.Feeds["/static2.zip"] = testutil.BuildZip(t, files)
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s3,S3,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s3,1",
}
server.Feeds["/static3.zip"] = testutil.BuildZip(t, files)
// First and second requires different headers. Third requires
// no headers.
server.RequiredHeaders = map[string]map[string]string{
"/static1.zip": {
"X-Header": "1",
},
"/static2.zip": {
"X-Header": "2",
},
}
// Attempt to load without headers.
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
_, err := m.LoadStaticAsync("a", server.Server.URL+"/static1.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static3.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// Refresh will attempt to download all three, as only the
// third will succeed there'll be an error.
require.Error(t, m.Refresh(context.Background()))
// First two fails to load, but third is ok.
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static1.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
s3, err := m.LoadStaticAsync("a", server.Server.URL+"/static3.zip", nil, when)
require.NoError(t, err)
stops, err := s3.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S3", stops[0].Name)
// Re-request the first two with correct headers.
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static1.zip", map[string]string{
"X-Header": "1",
}, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
_, err = m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", map[string]string{
"X-Header": "2",
}, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// And refresh should now be able to download them
require.NoError(t, m.Refresh(context.Background()))
assert.Len(t, server.Requests, 5)
// And can be read
s2, err := m.LoadStaticAsync("a", server.Server.URL+"/static2.zip", nil, when)
require.NoError(t, err)
stops, err = s2.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S2", stops[0].Name)
s3, err = m.LoadStaticAsync("a", server.Server.URL+"/static3.zip", nil, when)
require.NoError(t, err)
stops, err = s3.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S3", stops[0].Name)
}
func testManagerMultipleConsumers(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
// Three feeds, on separate URLs.
files := validFeed()
server.Feeds["/static1.zip"] = testutil.BuildZip(t, validFeed())
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s2,S2,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s2,1",
}
server.Feeds["/static2.zip"] = testutil.BuildZip(t, files)
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s3,S3,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s3,1",
}
server.Feeds["/static3.zip"] = testutil.BuildZip(t, files)
// First and second requires different headers. Third requires
// no headers.
server.RequiredHeaders = map[string]map[string]string{
"/static1.zip": {
"X-Header": "1",
},
"/static2.zip": {
"X-Header": "2",
},
}
// We'll use three "consumers": A, B and C. Each of these can
// request any feed, and they can do so using their own
// headers.
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
// A requests static1.zip with correct headers.
_, err := m.LoadStaticAsync("A", server.Server.URL+"/static1.zip", map[string]string{
"X-Header": "1",
}, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// B requests static2.zip with _incorrect_ header
_, err = m.LoadStaticAsync("B", server.Server.URL+"/static2.zip", map[string]string{
"X-Header": "bad header!",
}, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// C requests static3.zip
_, err = m.LoadStaticAsync("C", server.Server.URL+"/static3.zip", nil, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// No requests to server yet, but refresh will request all
// three feeds. Since the request for static2.zip fails (bad
// headers), Refresh will return an error.
assert.Equal(t, 0, len(server.Requests))
assert.Error(t, m.Refresh(context.Background()))
assert.Len(t, server.Requests, 3)
// A and C should now be able to read their feeds.
a, err := m.LoadStaticAsync("A", server.Server.URL+"/static1.zip", map[string]string{
"X-Header": "1",
}, when)
require.NoError(t, err)
stops, err := a.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S", stops[0].Name)
c, err := m.LoadStaticAsync("C", server.Server.URL+"/static3.zip", nil, when)
require.NoError(t, err)
stops, err = c.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S3", stops[0].Name)
// B makes a request with correct headers for static2.zip.
_, err = m.LoadStaticAsync("B", server.Server.URL+"/static2.zip", map[string]string{
"X-Header": "2",
}, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// Refresh should now succeed, and static2.zip should be
// loadable.
assert.NoError(t, m.Refresh(context.Background()))
assert.Len(t, server.Requests, 4)
b, err := m.LoadStaticAsync("B", server.Server.URL+"/static2.zip", map[string]string{
"X-Header": "2",
}, when)
require.NoError(t, err)
stops, err = b.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S2", stops[0].Name)
// With all feeds in storage, Load will succeed for all, even
// with incorrect headers.
a, err = m.LoadStaticAsync("A", server.Server.URL+"/static1.zip", map[string]string{
"X-Header": "bad header!",
}, when)
require.NoError(t, err)
stops, err = a.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S", stops[0].Name)
a, err = m.LoadStaticAsync("A", server.Server.URL+"/static2.zip", map[string]string{
"X-Header": "bad header!",
}, when)
require.NoError(t, err)
stops, err = a.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S2", stops[0].Name)
a, err = m.LoadStaticAsync("A", server.Server.URL+"/static3.zip", nil, when)
require.NoError(t, err)
stops, err = a.NearbyStops(1.0, -2.0, 0, nil)
require.NoError(t, err)
assert.Len(t, stops, 1)
assert.Equal(t, "S3", stops[0].Name)
// At this point, we should have 3 requests recorded in
// storage, with A as consumer for all, and B/C for 1 each.
requests, err := strg.ListFeedRequests("")
require.NoError(t, err)
sort.Slice(requests, func(i, j int) bool {
return requests[i].URL < requests[j].URL
})
assert.Equal(t, 3, len(requests))
assert.Equal(t, server.Server.URL+"/static1.zip", requests[0].URL)
assert.Equal(t, server.Server.URL+"/static2.zip", requests[1].URL)
assert.Equal(t, server.Server.URL+"/static3.zip", requests[2].URL)
assert.Equal(t, 1, len(requests[0].Consumers))
assert.Equal(t, 2, len(requests[1].Consumers))
assert.Equal(t, 2, len(requests[2].Consumers))
assert.Equal(t, "A", requests[0].Consumers[0].Name)
con2 := []string{requests[1].Consumers[0].Name, requests[1].Consumers[1].Name}
con3 := []string{requests[2].Consumers[0].Name, requests[2].Consumers[1].Name}
sort.Strings(con2)
sort.Strings(con3)
assert.Equal(t, []string{"A", "B"}, con2)
assert.Equal(t, []string{"A", "C"}, con3)
}
func testManagerLoadWithRefresh(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
// Three versions of a feed, each differing in stops.txt.
files := validFeed()
feed1Zip := testutil.BuildZip(t, files)
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s2,S,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s2,1",
}
feed2Zip := testutil.BuildZip(t, files)
files["stops.txt"] = []string{
"stop_id,stop_name,stop_lat,stop_lon",
"s3,S,12,34",
}
files["stop_times.txt"] = []string{
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"t,12:00:00,12:00:00,s3,1",
}
feed3Zip := testutil.BuildZip(t, files)
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
// Attempting to load will fail, but adds a request for it to
// be downloaded by a later Refresh()
server.Feeds["/static.zip"] = feed1Zip
s1, err := m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, s1)
// Call Refresh and it'll be retrieved
assert.NoError(t, m.Refresh(context.Background()))
// It got added to storage
feeds, err := strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 1, len(feeds))
// It can be loaded and serves the correct data
s1, err = m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err := s1.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
assert.Equal(t, "s", stops[0].ID)
// Replace the feed data served. Refresh the manager. We'll
// still see the first feed's data served, as too little time
// has passed for the refresh to actually go out and retrieve
// the new data.
server.Feeds["/static.zip"] = feed2Zip
assert.NoError(t, m.Refresh(context.Background()))
s2, err := m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err = s2.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
assert.Equal(t, "s", stops[0].ID)
assert.Equal(t, []string{"/static.zip"}, server.Requests)
// No new feed added to storage either
feeds, err = strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 1, len(feeds))
// Set a very low refresh interval, and manager will consider
// existing data stale. Refresh, and we'll see the feed 2
// data served.
m.StaticRefreshInterval = time.Duration(0)
require.NoError(t, m.Refresh(context.Background()))
s2, err = m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err = s2.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
assert.Equal(t, "s2", stops[0].ID) // s2 instead of s
assert.Equal(t, []string{"/static.zip", "/static.zip"}, server.Requests)
// Second feed added to storage
feeds, err = strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 2, len(feeds))
// Serve a third feed, and refresh.
server.Feeds["/static.zip"] = feed3Zip
assert.NoError(t, m.Refresh(context.Background()))
// The refesh will haved downloaded the third feed to storage
feeds, err = strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 3, len(feeds))
assert.Equal(t, []string{"/static.zip", "/static.zip", "/static.zip"}, server.Requests)
// This time, load with a time for which no feed is
// active. It'll error out.
when = time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
s3, err := m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, s3)
// Set a high refresh interval, and refresh. Server should not
// be hit.
m.StaticRefreshInterval = time.Hour
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, []string{"/static.zip", "/static.zip", "/static.zip"}, server.Requests)
// Load with a time for which feed 3 is active.
when = time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
s3, err = m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err = s3.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
assert.Equal(t, "s3", stops[0].ID)
// No new feed added to storage
feeds, err = strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 3, len(feeds))
// No new request to server
assert.Equal(t, []string{"/static.zip", "/static.zip", "/static.zip"}, server.Requests)
}
// In the case where a feed is completely broken, manager should
// not continue refreshing it until refresh interval has passed.
func testManagerBrokenData(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
goodZip := testutil.BuildZip(t, validFeed())
badZip := testutil.BuildZip(t, map[string][]string{"parse": []string{"fail"}})
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
// First attempt to load creates request for feed
_, err := m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
// Refresh will attempt to load the feed, but fail, as server
// will give 404
assert.Error(t, m.Refresh(context.Background()))
assert.Equal(t, []string{"/static.zip"}, server.Requests)
// Attempting again will fail again
assert.Error(t, m.Refresh(context.Background()))
assert.Equal(t, []string{"/static.zip", "/static.zip"}, server.Requests)
// Now make the broken zip available
server.Feeds["/static.zip"] = badZip
// Refresh will try and fail again.
assert.Error(t, m.Refresh(context.Background()))
assert.Equal(t, []string{"/static.zip", "/static.zip", "/static.zip"}, server.Requests)
// Since the last refresh failed due to a parse error (bad
// data), the manager will wait for the refresh interval
// before new requests are made.
assert.NoError(t, m.Refresh(context.Background()))
assert.NoError(t, m.Refresh(context.Background()))
assert.NoError(t, m.Refresh(context.Background()))
assert.NoError(t, m.Refresh(context.Background()))
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 3, len(server.Requests))
// Lower StaticRefreshInterval and make the good zip
// available. Refresh will download the new data.
server.Feeds["/static.zip"] = goodZip
m.StaticRefreshInterval = time.Duration(0)
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 4, len(server.Requests))
// Data can be loaded
s, err := m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err := s.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
// With StaticRefreshInterval at 0, refresh will do a request each
// time.
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 5, len(server.Requests))
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 6, len(server.Requests))
assert.NoError(t, m.Refresh(context.Background()))
assert.Equal(t, 7, len(server.Requests))
// But there's still only 1 feed in storage, as the data
// didn't change between requests.
feeds, err := strg.ListFeeds(storage.ListFeedsFilter{})
require.NoError(t, err)
assert.Equal(t, 1, len(feeds))
// Serve bad data again. Refresh will fail.
server.Feeds["/static.zip"] = badZip
require.Error(t, m.Refresh(context.Background()))
assert.Equal(t, 8, len(server.Requests))
// But we can still load it, as the old feed is still there.
s, err = m.LoadStaticAsync("a", server.Server.URL+"/static.zip", nil, when)
require.NoError(t, err)
stops, err = s.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
}
// Requesting a new URL with LoadStaticAsync() should return
// ErrNoActiveFeed. It should also place a record in storage
// signalling that the feed has been requested.
func testManagerAsyncLoad(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
server.Feeds["/static.zip"] = testutil.BuildZip(t, validFeed())
when := time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)
// Async request a feed for the first time
static, err := m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
assert.ErrorIs(t, err, gtfs.ErrNoActiveFeed)
assert.Nil(t, static)
// Record w URL only in DB
// A FeedRequest should be in DB
reqs, err := strg.ListFeedRequests("")
require.NoError(t, err)
assert.Equal(t, 1, len(reqs))
assert.Equal(t, server.Server.URL+"/static.zip", reqs[0].URL)
assert.Equal(t, 1, len(reqs[0].Consumers))
assert.Equal(t, "app1", reqs[0].Consumers[0].Name)
assert.Equal(t, "", reqs[0].Consumers[0].Headers)
// Additional requests for the feed doesn't add new
// records. Existing record is exactly as before.
prevReq := reqs[0]
_, err = m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
assert.True(t, errors.Is(err, gtfs.ErrNoActiveFeed))
_, err = m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
assert.True(t, errors.Is(err, gtfs.ErrNoActiveFeed))
reqs, err = strg.ListFeedRequests("")
require.NoError(t, err)
assert.Equal(t, 1, len(reqs))
assert.Equal(t, prevReq, reqs[0])
// Processing async requests will retrieve the feed
err = m.Refresh(context.Background())
assert.NoError(t, err)
// Subsequent async requests will return the feed
static, err = m.LoadStaticAsync("app1", server.Server.URL+"/static.zip", nil, when)
assert.NoError(t, err)
stops, err := static.NearbyStops(1, 1, 0, nil)
require.NoError(t, err)
require.Equal(t, 1, len(stops))
assert.Equal(t, "s", stops[0].ID)
}
func testManagerLoadRealtime(t *testing.T, strg storage.Storage) {
m := gtfs.NewManager(strg)
server := managerFixture()
defer server.Server.Close()
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()
dl.TimeNow = func() time.Time {
return now
}
m.Downloader = dl
// Realtime feed can now be loaded
realtime, err := m.LoadRealtime(
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
require.NoError(t, err)
assert.Equal(t, uint64(12345), realtime.Timestamp)
// Publish a new realtime feed
server.Feeds["/realtime.pb"] = validRealtimeFeed(t, time.Unix(12346, 0))
// Old is still served from cache
realtime, err = m.LoadRealtime(
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
require.NoError(t, err)
assert.Equal(t, uint64(12345), realtime.Timestamp)
// Fast forward time to invalidate cached feed, and the new
// will be retrieved
now = now.Add(3 * time.Minute)
realtime, err = m.LoadRealtime(
"app1", static,
server.Server.URL+"/realtime.pb", nil,
when,
)
require.NoError(t, err)
assert.Equal(t, uint64(12346), realtime.Timestamp)
// Bad data results in error
server.Feeds["/bad.pb"] = []byte("this isn't protobuf")
_, err = m.LoadRealtime(
"app1", static,
server.Server.URL+"/bad.pb", nil,
when,
)
assert.Error(t, err, "umarshaling protobuf")
// Missing data is also error
_, err = m.LoadRealtime(
"app1", static,
server.Server.URL+"/missing.pb", nil,
when,
)
assert.ErrorContains(t, err, "404")
// 404 isn't cached
server.Feeds["/missing.pb"] = validRealtimeFeed(t, time.Unix(12348, 0))
realtime, err = m.LoadRealtime(
"app1", static,
server.Server.URL+"/missing.pb", nil,
when,
)
assert.NoError(t, err)
assert.Equal(t, uint64(12348), realtime.Timestamp)
}
// Verifies that Manager respects agency timezone when determining if
// a feed is active.
func testManagerRespectTimezones(t *testing.T, strg storage.Storage) {
// TODO: write me
}
// Verifies that manager can refresh a bunch of feeds according to the
// StaticRefreshInterval.
func testManagerRefreshFeeds(t *testing.T, strg storage.Storage) {
// TODO: write me
}
func TestManager(t *testing.T) {
for _, test := range []struct {
Name string
Test func(*testing.T, storage.Storage)
}{
{"LoadSingleFeed", testManagerLoadSingleFeed},
{"LoadMultipleURLs", testManagerLoadMultipleURLs},
{"LoadWithHeaders", testManagerLoadWithHeaders},
{"MultipleConsumers", testManagerMultipleConsumers},
{"LoadWithRefresh", testManagerLoadWithRefresh},
{"ManagerBrokenData", testManagerBrokenData},
{"AsyncLoad", testManagerAsyncLoad},
{"LoadRealtime", testManagerLoadRealtime},
{"RespectTimezones", testManagerRespectTimezones},
{"RefreshFeeds", testManagerRefreshFeeds},
} {
t.Run(fmt.Sprintf("%s_SQLiteMemory", test.Name), func(t *testing.T) {
s, err := storage.NewSQLiteStorage(storage.SQLiteConfig{OnDisk: false})
require.NoError(t, err)
test.Test(t, s)
})
t.Run(fmt.Sprintf("%s_SQLiteFile", test.Name), func(t *testing.T) {
dir, err := ioutil.TempDir("", "gtfs_storage_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
s, err := storage.NewSQLiteStorage(storage.SQLiteConfig{OnDisk: true, Directory: dir})
require.NoError(t, err)
test.Test(t, s)
})
if testutil.PostgresConnStr != "" {
t.Run(fmt.Sprintf("%s_Postgres", test.Name), func(t *testing.T) {
s, err := storage.NewPSQLStorage(testutil.PostgresConnStr, true)
require.NoError(t, err)
test.Test(t, s)
})
}
}
}