From 06620b230c19a1d377643f1cddf83aaafc779a31 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Tue, 26 Nov 2024 17:35:47 +0000 Subject: [PATCH] add test for #6197 --- .../model/TimetableSnapshotTest.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/opentripplanner/model/TimetableSnapshotTest.java b/application/src/test/java/org/opentripplanner/model/TimetableSnapshotTest.java index 4f3e12c1368..c0753a7fbf5 100644 --- a/application/src/test/java/org/opentripplanner/model/TimetableSnapshotTest.java +++ b/application/src/test/java/org/opentripplanner/model/TimetableSnapshotTest.java @@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import com.google.transit.realtime.GtfsRealtime.TripDescriptor; import com.google.transit.realtime.GtfsRealtime.TripDescriptor.ScheduleRelationship; @@ -24,14 +25,21 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.opentripplanner.ConstantsForTests; import org.opentripplanner.TestOtpModel; import org.opentripplanner._support.time.ZoneIds; import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.TransitLayerUpdater; +import org.opentripplanner.transit.model.basic.TransitMode; import org.opentripplanner.transit.model.framework.Deduplicator; import org.opentripplanner.transit.model.framework.FeedScopedId; import org.opentripplanner.transit.model.framework.Result; +import org.opentripplanner.transit.model.network.Route; +import org.opentripplanner.transit.model.network.StopPattern; import org.opentripplanner.transit.model.network.TripPattern; +import org.opentripplanner.transit.model.organization.Agency; +import org.opentripplanner.transit.model.site.RegularStop; +import org.opentripplanner.transit.model.site.StopLocation; import org.opentripplanner.transit.model.timetable.Trip; import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate; import org.opentripplanner.transit.model.timetable.TripOnServiceDate; @@ -46,7 +54,8 @@ public class TimetableSnapshotTest { private static final ZoneId timeZone = ZoneIds.GMT; public static final LocalDate SERVICE_DATE = LocalDate.of(2024, 1, 1); private static Map patternIndex; - static String feedId; + private static String feedId; + private static Route route; @BeforeAll public static void setUp() throws Exception { @@ -412,6 +421,75 @@ void testClear() { assertNotNull(snapshot.getRealtimeAddedRoute(pattern.getRoute().getId())); } + /** + * This test checks that an empty timetable is given to TransitLayerUpdater for previously + * added patterns after the buffer is cleared. + *

+ * Refer to bug #6197 for details. + */ + @Test + void testTransitLayerUpdateAfterAddPatternAndClear() { + var resolver = new TimetableSnapshot(); + var today = LocalDate.now(timeZone); + + var departureStopTime = new StopTime(); + var arrivalStopTime = new StopTime(); + + departureStopTime.setDepartureTime(0); + departureStopTime.setArrivalTime(0); + departureStopTime.setStop(RegularStop.of(new FeedScopedId(feedId, "XX"), () -> 0).build()); + arrivalStopTime.setDepartureTime(300); + arrivalStopTime.setArrivalTime(300); + arrivalStopTime.setStop(RegularStop.of(new FeedScopedId(feedId, "YY"), () -> 1).build()); + + var stopTimes = List.of(departureStopTime, arrivalStopTime); + + var stopPattern = new StopPattern(stopTimes); + + var route = patternIndex.values().stream().findFirst().orElseThrow().getRoute(); + var pattern = TripPattern + .of(new FeedScopedId(feedId, "1.1")) + .withRoute(route) + .withStopPattern(stopPattern) + .withCreatedByRealtimeUpdater(true) + .build(); + + var tripTimes = TripTimesFactory.tripTimes( + Trip.of(new FeedScopedId(feedId, "addedTrip")).withRoute(route).build(), + stopTimes, + new Deduplicator() + ); + var realTimeTripUpdate = new RealTimeTripUpdate(pattern, tripTimes, today, null, true, false); + var transitLayerUpdater = new TransitLayerUpdater(null) { + int count = 0; + + @Override + public void update( + Collection updatedTimetables, + Map> timetables + ) { + var timetable = updatedTimetables.stream().findFirst().orElseThrow(); + assertEquals(today, timetable.getServiceDate()); + assertEquals(pattern, timetable.getPattern()); + + if (count == 1) { + // commited after clear + assertThat(timetable.getTripTimes()).isEmpty(); + } else { + // commited with an update + assertThat(timetable.getTripTimes()).hasSize(1); + } + ++count; + } + }; + + resolver.update(realTimeTripUpdate); + resolver.commit(transitLayerUpdater, true); + + resolver.clear(feedId); + resolver.commit(transitLayerUpdater, true); + } + private static TimetableSnapshot createCommittedSnapshot() { TimetableSnapshot timetableSnapshot = new TimetableSnapshot(); return timetableSnapshot.commit(null, true);