-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_test.go
2108 lines (2005 loc) · 57.5 KB
/
realtime_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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gtfs_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
proto "google.golang.org/protobuf/proto"
"tidbyt.dev/gtfs"
"tidbyt.dev/gtfs/model"
p "tidbyt.dev/gtfs/proto"
"tidbyt.dev/gtfs/testutil"
)
// Helpers for building gtfs-realtime feeds
type StopUpdate struct {
ArrivalSet bool
ArrivalDelay int32
ArrivalTime time.Time
DepartureSet bool
DepartureDelay int32
DepartureTime time.Time
StopID string
StopSequence uint32
SchedRel string
}
type TripUpdate struct {
TripID string
StopUpdates []StopUpdate
Canceled bool
}
func buildFeed(t *testing.T, tripUpdates []TripUpdate) [][]byte {
entity := make([]*p.FeedEntity, 0, len(tripUpdates))
for _, tripUpdate := range tripUpdates {
stopTimeUpdate := make([]*p.TripUpdate_StopTimeUpdate, 0, len(tripUpdate.StopUpdates))
for _, stopUpdate := range tripUpdate.StopUpdates {
var scheduleRelationship p.TripUpdate_StopTimeUpdate_ScheduleRelationship
switch stopUpdate.SchedRel {
case "SKIPPED":
scheduleRelationship = p.TripUpdate_StopTimeUpdate_SKIPPED
case "NO_DATA":
scheduleRelationship = p.TripUpdate_StopTimeUpdate_NO_DATA
case "":
fallthrough
case "SCHEDULED":
scheduleRelationship = p.TripUpdate_StopTimeUpdate_SCHEDULED
default:
t.Fatal(fmt.Sprintf("bad SchedRel: %s", stopUpdate.SchedRel))
}
stup := &p.TripUpdate_StopTimeUpdate{
ScheduleRelationship: &scheduleRelationship,
StopSequence: proto.Uint32(stopUpdate.StopSequence),
StopId: proto.String(stopUpdate.StopID),
}
if stopUpdate.DepartureSet {
departureTime := int64(0)
if !stopUpdate.DepartureTime.IsZero() {
departureTime = stopUpdate.DepartureTime.Unix()
}
stup.Departure = &p.TripUpdate_StopTimeEvent{
Delay: proto.Int32(stopUpdate.DepartureDelay),
Time: proto.Int64(departureTime),
}
}
if stopUpdate.ArrivalSet {
arrivalTime := int64(0)
if !stopUpdate.ArrivalTime.IsZero() {
arrivalTime = stopUpdate.ArrivalTime.Unix()
}
stup.Arrival = &p.TripUpdate_StopTimeEvent{
Delay: proto.Int32(stopUpdate.ArrivalDelay),
Time: proto.Int64(arrivalTime),
}
}
stopTimeUpdate = append(stopTimeUpdate, stup)
}
tripScheduleRelationship := p.TripDescriptor_SCHEDULED
if tripUpdate.Canceled {
tripScheduleRelationship = p.TripDescriptor_CANCELED
}
entity = append(entity, &p.FeedEntity{
Id: proto.String(tripUpdate.TripID),
TripUpdate: &p.TripUpdate{
Trip: &p.TripDescriptor{
TripId: proto.String(tripUpdate.TripID),
ScheduleRelationship: &tripScheduleRelationship,
},
StopTimeUpdate: stopTimeUpdate,
},
})
}
incrementality := p.FeedHeader_FULL_DATASET
timestamp := uint64(time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC).Unix())
header := &p.FeedHeader{
GtfsRealtimeVersion: proto.String("2.0"),
Incrementality: &incrementality,
Timestamp: proto.Uint64(timestamp),
}
feed := &p.FeedMessage{Header: header, Entity: entity}
data, err := proto.Marshal(feed)
require.NoError(t, err)
if err != nil {
panic(err)
}
return [][]byte{data}
}
// A simple Static fixture. Trips t1 and t2 cover the same three
// stops s1-s3. Trip t3 covers z1-z2. Full service all days of 2020.
func SimpleStaticFixture(t *testing.T) *gtfs.Static {
static := testutil.BuildStatic(t, "sqlite", map[string][]string{
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"everyday,20200101,20210101,1,1,1,1,1,1,1",
},
"routes.txt": {
"route_id,route_short_name,route_type",
"R1,R_1,1",
"R2,R_2,1",
},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"s1,S1,1,1",
"s2,S2,2,3",
"s3,S3,4,5",
"s4,S4,6,7",
"z1,Z1,8,9",
"z2,Z2,10,11",
"z3,Z3,12,13",
},
"trips.txt": {
"service_id,trip_id,route_id",
"everyday,t1,R1",
"everyday,t2,R1",
"everyday,t3,R2",
},
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"t1,s1,1,23:0:0,23:0:0",
"t1,s2,2,23:1:0,23:1:0",
"t1,s3,3,23:2:0,23:2:0",
"t1,s4,4,23:3:0,23:3:0",
"t2,s1,1,23:10:0,23:10:0",
"t2,s2,2,23:11:0,23:11:0",
"t2,s3,3,23:12:0,23:12:0",
"t2,s4,4,23:13:0,23:13:0",
"t3,z1,1,23:5:0,23:5:0",
"t3,z2,2,23:6:0,23:6:0",
"t3,z3,3,23:7:0,23:7:0",
},
})
return static
}
// Test realtime data where updates all have 0 delay.
func TestRealtimeNoChanges(t *testing.T) {
// This realtime feed has updates for stops on trip t1, but
// none of them modify the departure time from what's already
// scheduled.
feed := buildFeed(t, []TripUpdate{
{
TripID: "t1",
StopUpdates: []StopUpdate{
{
StopID: "s2", // identify by stop_id
DepartureSet: true,
DepartureDelay: 0,
},
{
StopSequence: 3, // identify by stop_sequence
DepartureSet: true,
DepartureTime: time.Date(2020, 1, 15, 23, 2, 0, 0, time.UTC),
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
assert.Equal(t, uint64(time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC).Unix()), rt.Timestamp)
// Check s1
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 10, 0, 0, time.UTC),
},
}, departures)
// Check s2
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 11, 0, 0, time.UTC),
},
}, departures)
// Check s3
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 2, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 12, 0, 0, time.UTC),
},
}, departures)
// No departures from s4 since it's the final stop
departures, err = rt.Departures("s4", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 30*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
// And z1 for good measure. This one definitely shouldn't have
// changed.
departures, err = rt.Departures("z1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R2",
TripID: "t3",
StopID: "z1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 5, 0, 0, time.UTC),
},
}, departures)
// And no departures from z3 since it's the final stop
departures, err = rt.Departures("z3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 30*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
}
// Realtime data where updates are expected to propagate along a trip
func TestRealtimeDelayWithPropagation(t *testing.T) {
// Delays on s2! For trip t1 there are no updates for s3, so
// s1 delay should propagate. For trip t2, train is expected
// to catch up fully before s3.
feed := buildFeed(t, []TripUpdate{
{
TripID: "t1",
StopUpdates: []StopUpdate{
{
StopID: "s2",
DepartureSet: true,
DepartureDelay: 30, // 30 second delay
},
},
},
{
TripID: "t2",
StopUpdates: []StopUpdate{
{
SchedRel: "SCHEDULED",
StopSequence: 2,
DepartureSet: true,
DepartureTime: time.Date(2020, 1, 15, 23, 11, 45, 0, time.UTC), // 45s delay
},
{
StopID: "s3", // no delay
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
// Check s1
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 10, 0, 0, time.UTC),
},
}, departures)
// Check s2. Expecting delays on both trips.
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 11, 45, 0, time.UTC),
Delay: 45 * time.Second,
},
}, departures)
// Check s3. Expecting delay on t1, but t2 back on schedule.
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 2, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 12, 0, 0, time.UTC),
},
}, departures)
}
// Verifies that StopTimeUpdate with NO_DATA are handled correctly.
func TestRealtimeNoData(t *testing.T) {
// This realtime feed has NO_DATA for t1-s1, which means the
// provided Time (which is really a violation of spec) is
// ignored and the static scheduled is used instead. Then at
// t1-s2, a real delay appears, which should be propagated to
// t1-s3 since no later update contradicts it.
// For trip 2 we have a delay at s1
feed := buildFeed(t, []TripUpdate{
{
// For trip t1 we've NO_DATA at s1, which
// means the provided departure time (which
// strictly speaking is a violation of spec)
// should be ignored and the static schedule
// is used instead. At s2, there is a 30s
// delay, which should be propagated to s3 as
// well.
TripID: "t1",
StopUpdates: []StopUpdate{
{
SchedRel: "NO_DATA",
StopID: "s1",
DepartureSet: true,
DepartureTime: time.Date(1212, 12, 12, 12, 12, 12, 12, time.UTC),
},
{
SchedRel: "SCHEDULED",
StopID: "s2",
DepartureSet: true,
DepartureDelay: 30,
},
},
},
{
// On t2 we have a delay at s1. The NO_DATA
// record should make s2 and s3 fall back to
// static schedule, i.e. delay propagation
// halts.
TripID: "t2",
StopUpdates: []StopUpdate{
{
StopSequence: 1,
DepartureSet: true,
DepartureTime: time.Date(2020, 1, 15, 23, 10, 45, 0, time.UTC), // 45s delay
},
{
SchedRel: "NO_DATA",
StopID: "s2", // no delay
DepartureSet: true,
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
// Check s1
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 10, 45, 0, time.UTC),
Delay: 45 * time.Second,
},
}, departures)
// Check s2
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
// delay not propagated due to NO_DATA
Time: time.Date(2020, 1, 15, 23, 11, 0, 0, time.UTC),
},
}, departures)
// Check s3
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 3,
// delay propagated from s2
Time: time.Date(2020, 1, 15, 23, 2, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s3",
StopSequence: 3,
// and t2 remains on schedule due to NO_DATA at s2
Time: time.Date(2020, 1, 15, 23, 12, 0, 0, time.UTC),
},
}, departures)
}
// Verifies that StopTimeUpdate with SKIPPED stops work.
func TestRealtimeSkippedStop(t *testing.T) {
feed := buildFeed(t, []TripUpdate{
{
// Trip t1 skips stops s1 and s3. A delay is
// included at s1, but is ignored.
TripID: "t1",
StopUpdates: []StopUpdate{
{
SchedRel: "SKIPPED",
StopID: "s1",
DepartureSet: true,
DepartureDelay: 120,
},
{
SchedRel: "SKIPPED",
StopSequence: 3,
},
},
},
{
// On t2 we have a delay at s1 and a skip at
// s2. The delay from s1 should be propagated
// to s3.
TripID: "t2",
StopUpdates: []StopUpdate{
{
StopID: "s1",
DepartureSet: true,
DepartureDelay: 30,
},
{
SchedRel: "SKIPPED",
StopID: "s2",
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
// Check s1. Expect t1 to skip past. t2 is delayed 30s.
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 10, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
// Check s2. Expect t2 to skip. t1 is on time.
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 0, 0, time.UTC),
},
}, departures)
// Check s3. Expect t1 to skip, and t2 to remain 30s delayed.
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 12, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
}
// Verifies that trip cancellations work.
func TestRealtimeCanceledTrip(t *testing.T) {
// Trip t1 is canceled, t2 runs with a delay from s2.
feed := buildFeed(t, []TripUpdate{
{
TripID: "t1",
Canceled: true,
},
{
TripID: "t2",
StopUpdates: []StopUpdate{
{
StopID: "s2",
DepartureSet: true,
DepartureDelay: 30,
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 10, 0, 0, time.UTC),
},
}, departures)
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 11, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 12, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
departures, err = rt.Departures("z1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R2",
TripID: "t3",
StopID: "z1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 5, 0, 0, time.UTC),
},
}, departures)
// All trips are canceled!
feed = buildFeed(t, []TripUpdate{
{
TripID: "t1",
Canceled: true,
},
{
TripID: "t2",
Canceled: true,
},
{
TripID: "t3",
Canceled: true,
},
})
rt, err = gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
departures, err = rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = rt.Departures("s31", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = rt.Departures("z1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = rt.Departures("z2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
}
// Check that the time parameters are respected
func TestRealtimeTimeWindowing(t *testing.T) {
// Delay some stuff, cancel some stuff.
feed := buildFeed(t, []TripUpdate{
{
TripID: "t1",
StopUpdates: []StopUpdate{
{
StopID: "s2",
DepartureSet: true,
DepartureDelay: 30,
},
},
},
{
TripID: "t2",
StopUpdates: []StopUpdate{
{
StopSequence: 1,
SchedRel: "SKIPPED",
},
{
StopSequence: 2,
DepartureSet: true,
DepartureDelay: 120,
},
},
},
})
static := SimpleStaticFixture(t)
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
// This should produce the following schedule for t1 and t2:
// s1 - t1 23:00:00
// s2 - t1 23:01:30
// s2 - t2 23:13:00
// s3 - t1 23:02:30
// s3 - t2 23:14:00
// Window exludes t2 stop
departures, err := rt.Departures(
"s2",
time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC),
12*time.Minute+59*time.Second,
-1, "", -1, nil,
)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
// Nudge it forward 1 second and t2 is included
departures, err = rt.Departures(
"s2",
time.Date(2020, 1, 15, 23, 0, 1, 0, time.UTC),
12*time.Minute+59*time.Second,
-1, "", -1, nil,
)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 13, 0, 0, time.UTC),
Delay: 120 * time.Second,
},
}, departures)
// Move the window past t1-s1 departure and t1 is exluded
departures, err = rt.Departures(
"s2",
time.Date(2020, 1, 15, 23, 1, 30, 1, time.UTC),
12*time.Minute+59*time.Second,
-1, "", -1, nil,
)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t2",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 13, 0, 0, time.UTC),
Delay: 120 * time.Second,
},
}, departures)
}
// Make sure we can deal with trips that have loops. In these cases,
// (tripID,stopID) is not a unique departure identifier, but
// (tripID,stopSequence) is.
func TestRealtimeTripWithLoop(t *testing.T) {
// This static schedule has t1 running from s1 to s2, and then
// 3 loops s3-s5, and finally end of the trip at s3.
static := testutil.BuildStatic(t, "sqlite", map[string][]string{
// A weekdays only schedule
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"everyday,20200101,20210101,1,1,1,1,1,1,1",
},
// Two routes: L and F
"routes.txt": {"route_id,route_short_name,route_type", "R1,R one,3"},
// A bunch of stops
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"s1,stop one,1,1",
"s2,stop two,1,1",
"s3,stop three,1,1",
"s4,stop four,1,1",
"s5,stop five,1,1",
},
// The L has three trips running east, two running west. F runs north then south.
"trips.txt": {
"trip_id,route_id,service_id",
"t1,R1,everyday",
},
// The L trips run 3rd ave - 14th st - 6th ave. F runs W4 - 14th - 23rd.
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"t1,s1,1,23:0:0,23:0:0",
"t1,s2,2,23:1:0,23:1:0",
"t1,s3,3,23:2:0,23:2:0",
"t1,s4,4,23:3:0,23:3:0",
"t1,s5,5,23:4:0,23:4:0",
"t1,s3,6,23:5:0,23:5:0",
"t1,s4,7,23:6:0,23:6:0",
"t1,s5,8,23:7:0,23:7:0",
"t1,s3,9,23:8:0,23:8:0",
"t1,s4,10,23:9:0,23:9:0",
"t1,s5,11,23:10:0,23:10:0",
"t1,s3,12,23:11:0,23:11:0",
},
})
// Let's drop in a delay at the 5th stop, skip stop 8 and stop
// propagating the delay at stop 11.
feed := buildFeed(t, []TripUpdate{
{
TripID: "t1",
StopUpdates: []StopUpdate{
{
StopID: "s5",
StopSequence: 5,
DepartureSet: true,
DepartureDelay: 30,
},
{
SchedRel: "SKIPPED",
StopID: "s5",
StopSequence: 8,
},
{
SchedRel: "NO_DATA",
StopSequence: 11,
DepartureSet: true,
DepartureDelay: 0,
},
},
},
})
rt, err := gtfs.NewRealtime(context.Background(), static, feed)
require.NoError(t, err)
departures, err := rt.Departures("s1", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s1",
StopSequence: 1,
Time: time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC),
},
}, departures)
departures, err = rt.Departures("s2", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s2",
StopSequence: 2,
Time: time.Date(2020, 1, 15, 23, 1, 0, 0, time.UTC),
},
}, departures)
departures, err = rt.Departures("s3", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 3,
Time: time.Date(2020, 1, 15, 23, 2, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 6,
Time: time.Date(2020, 1, 15, 23, 5, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t1",
StopID: "s3",
StopSequence: 9,
Time: time.Date(2020, 1, 15, 23, 8, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
departures, err = rt.Departures("s4", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s4",
StopSequence: 4,
Time: time.Date(2020, 1, 15, 23, 3, 0, 0, time.UTC),
},
{
RouteID: "R1",
TripID: "t1",
StopID: "s4",
StopSequence: 7,
Time: time.Date(2020, 1, 15, 23, 6, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t1",
StopID: "s4",
StopSequence: 10,
Time: time.Date(2020, 1, 15, 23, 9, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
}, departures)
departures, err = rt.Departures("s5", time.Date(2020, 1, 15, 23, 0, 0, 0, time.UTC), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{
{
RouteID: "R1",
TripID: "t1",
StopID: "s5",
StopSequence: 5,
Time: time.Date(2020, 1, 15, 23, 4, 30, 0, time.UTC),
Delay: 30 * time.Second,
},
{
RouteID: "R1",
TripID: "t1",
StopID: "s5",
StopSequence: 11,
Time: time.Date(2020, 1, 15, 23, 10, 0, 0, time.UTC),
},
}, departures)
}
func TestRealtimeDepartureFiltering(t *testing.T) {
// Two routes: bus going center to south and rail going center
// to east. Each route has two trips: one heading out, one
// heading in.
static := testutil.BuildStatic(t, "sqlite", map[string][]string{
// A weekdays only schedule
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"everyday,20200101,20210101,1,1,1,1,1,1,1",
},
// Two routes: L and F
"routes.txt": {
"route_id,route_long_name,route_type",
"BusSouth,Bus South,3",
"RailEast,Rail East,2",