use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImplTest method testClearCache.
@Test
public void testClearCache() throws InterruptedException {
long serviceDate = System.currentTimeMillis();
BlockEntryImpl blockA = block("blockA");
TripEntryImpl tripA = trip("tripA", "serviceId");
stopTime(0, null, tripA, time(9, 00), 0);
BlockConfigurationEntry blockConfigA = linkBlockTrips(blockA, tripA);
BlockInstance instanceA = new BlockInstance(blockConfigA, serviceDate);
BlockEntryImpl blockB = block("blockB");
TripEntryImpl tripB = trip("tripB", "serviceId");
stopTime(0, null, tripB, time(9, 00), 0);
BlockConfigurationEntry blockConfigB = linkBlockTrips(blockB, tripB);
BlockInstance instanceB = new BlockInstance(blockConfigB, serviceDate);
VehicleLocationRecordCacheImpl cache = new VehicleLocationRecordCacheImpl();
cache.setBlockLocationRecordCacheWindowSize(20);
cache.addRecord(instanceA, record(20, "blockA", serviceDate, "vehicleA", 10.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceB, record(30, "blockB", serviceDate, "vehicleB", 20.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceA, record(40, "blockA", serviceDate, "vehicleC", 20.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceB, record(50, "blockB", serviceDate, "vehicleD", 20.0), null, null);
cache.clearStaleRecords(System.currentTimeMillis() - 150);
VehicleLocationCacheElements cacheRecord = cache.getRecordForVehicleId(aid("vehicleA"));
assertNull(cacheRecord);
cacheRecord = cache.getRecordForVehicleId(aid("vehicleB"));
assertNull(cacheRecord);
cacheRecord = cache.getRecordForVehicleId(aid("vehicleC"));
assertEquals(aid("vehicleC"), cacheRecord.getLastElement().getRecord().getVehicleId());
cacheRecord = cache.getRecordForVehicleId(aid("vehicleD"));
assertEquals(aid("vehicleD"), cacheRecord.getLastElement().getRecord().getVehicleId());
List<VehicleLocationCacheElements> records = cache.getRecordsForBlockInstance(instanceA);
assertEquals(1, records.size());
records = cache.getRecordsForBlockInstance(instanceB);
assertEquals(1, records.size());
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImplTest method testConcurrentOperations.
@Test
public void testConcurrentOperations() {
VehicleLocationRecordCacheImpl cache = new VehicleLocationRecordCacheImpl();
cache.setBlockLocationRecordCacheWindowSize(2);
long serviceDate = System.currentTimeMillis();
int vid = 0;
for (int i = 0; i < 20; i++) {
BlockEntryImpl block = block(Integer.toString(i));
TripEntryImpl trip = trip(Integer.toString(i), "serviceId");
stopTime(0, null, trip, time(9, 00), 0);
BlockConfigurationEntry blockConfig = linkBlockTrips(block, trip);
BlockInstance blockInstance = new BlockInstance(blockConfig, serviceDate);
for (int j = 0; j < 5; j++) {
AgencyAndId vehicleId = new AgencyAndId("1", Integer.toString(vid++));
RecordSource source = new RecordSource(blockInstance, vehicleId, cache);
Thread thread = new Thread(source);
thread.run();
}
}
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_FutureDay.
// Ensure that if we get an update for a future day we propagate a prediction for that day.
// (This is equivalent to timestamp on feed being early incorrectly, since currentTime in
// GtfsRealtimeTripLibrary is set via the timestamp.)
@Test
public void testCreateVehicleLocationRecordForUpdate_FutureDay() {
final long day = TimeUnit.DAYS.toMillis(1);
StopTimeUpdate.Builder stopTimeUpdate = stopTimeUpdateWithDepartureDelay("stopA", 180);
TripUpdate.Builder tripUpdate = tripUpdate("tripA", (_library.getCurrentTime() + day) / 1000, 120, stopTimeUpdate);
TripEntryImpl tripA = trip("tripA");
stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
BlockEntryImpl blockA = block("blockA");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
BlockInstance blockInstanceB = new BlockInstance(blockConfigA, day);
Mockito.when(_entitySource.getTrip("tripA")).thenReturn(tripA);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.longThat(new ArgumentMatcher<Long>() {
@Override
public boolean matches(Object argument) {
return ((Long) argument) < day;
}
}))).thenReturn(Arrays.asList(blockInstanceA));
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.longThat(new ArgumentMatcher<Long>() {
@Override
public boolean matches(Object argument) {
return ((Long) argument) >= day;
}
}))).thenReturn(Arrays.asList(blockInstanceB));
FeedMessage.Builder TU = createFeed();
TU.addEntity(feed(tripUpdate));
FeedMessage.Builder VP = createFeed();
List<CombinedTripUpdatesAndVehiclePosition> updates = _library.groupTripUpdatesAndVehiclePositions(TU.build(), VP.build());
CombinedTripUpdatesAndVehiclePosition update = updates.get(0);
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
long departure = tpr.getTimepointPredictedDepartureTime();
// 7:30 + 3 min delay + on next day
assertEquals(departure, time(7, 33) * 1000 + day);
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_NoStopTimeUpdates.
@Test
public void testCreateVehicleLocationRecordForUpdate_NoStopTimeUpdates() {
TripUpdate tripUpdate = TripUpdate.newBuilder().setTrip(TripDescriptor.newBuilder().setTripId("tripA")).setDelay(120).setTimestamp(123456789).build();
TripEntryImpl tripA = trip("tripA");
stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
BlockEntryImpl blockA = block("blockA");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
update.block = new BlockDescriptor();
update.block.setBlockInstance(blockInstanceA);
update.tripUpdates = Arrays.asList(tripUpdate);
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
assertEquals(123456789000L, record.getTimeOfRecord());
assertEquals(120, record.getScheduleDeviation(), 0.0);
}
use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibraryTest method testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates.
@Test
public void testCreateVehicleLocationRecordForUpdate_WithStopTimeUpdates() {
StopTimeUpdate.Builder stopTimeUpdate = StopTimeUpdate.newBuilder();
stopTimeUpdate.setStopId("stopA");
StopTimeEvent.Builder stopTimeEvent = StopTimeEvent.newBuilder();
stopTimeEvent.setDelay(180);
stopTimeUpdate.setDeparture(stopTimeEvent);
TripUpdate tripUpdate = TripUpdate.newBuilder().setTrip(TripDescriptor.newBuilder().setTripId("tripA")).setDelay(120).setTimestamp(123456789).addStopTimeUpdate(stopTimeUpdate).build();
TripEntryImpl tripA = trip("tripA");
stopTime(0, stop("stopA", 0, 0), tripA, time(7, 30), 0.0);
BlockEntryImpl blockA = block("blockA");
BlockConfigurationEntry blockConfigA = blockConfiguration(blockA, serviceIds("s1"), tripA);
BlockInstance blockInstanceA = new BlockInstance(blockConfigA, 0L);
Mockito.when(_blockCalendarService.getActiveBlocks(Mockito.eq(blockA.getId()), Mockito.anyLong(), Mockito.anyLong())).thenReturn(Arrays.asList(blockInstanceA));
CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
update.block = new BlockDescriptor();
update.block.setBlockInstance(blockInstanceA);
update.tripUpdates = Arrays.asList(tripUpdate);
VehicleLocationRecord record = _library.createVehicleLocationRecordForUpdate(update);
TimepointPredictionRecord tpr = record.getTimepointPredictions().get(0);
long departure = tpr.getTimepointPredictedDepartureTime();
// 7:30 plus 3 min delay, + now we are in ms.
assertEquals(departure, time(7, 33) * 1000);
}
Aggregations