Skip to content

Commit

Permalink
add test for opentripplanner#6197
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Nov 26, 2024
1 parent c2bc785 commit 06620b2
Showing 1 changed file with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<FeedScopedId, TripPattern> patternIndex;
static String feedId;
private static String feedId;
private static Route route;

@BeforeAll
public static void setUp() throws Exception {
Expand Down Expand Up @@ -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.
* <p>
* 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<Timetable> updatedTimetables,
Map<TripPattern, SortedSet<Timetable>> 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);
Expand Down

0 comments on commit 06620b2

Please sign in to comment.