fix: trips-for-route logging, test coverage, and performance follow-ups#753
fix: trips-for-route logging, test coverage, and performance follow-ups#753ARCoder181105 wants to merge 4 commits intoOneBusAway:mainfrom
Conversation
This commit resolves the outstanding follow-up items from PR OneBusAway#679, consolidating several improvements to the trips-for-route endpoint: * Logging: Added warnings for base trip reference resolution failures, stripped DUPLICATED IDs, batch query failures, and shape point errors. Corrected stale occupancy comments. * Testing: Added deterministic test coverage for DUPLICATED vehicle injections, past-midnight service bounds, and fallback edge cases. * Performance: Removed redundant GetTripsInBlock query by reusing existing time-bounded queries. * Spec Compliance: Removed omitempty from LastKnownLocation to ensure OpenAPI alignment.
45509e1 to
c590882
Compare
Added a comment to clarify cache clearing before the request.
aaronbrethorst
left a comment
There was a problem hiding this comment.
Hey Aditya, solid follow-up work here — the redundant query removal is a nice optimization, the clock-based time handling is a real improvement for test determinism, and the omitempty removal on LastKnownLocation is correct per the OpenAPI spec (nullable: true means always present). A few things to address before merging.
Critical Issues (0 found)
(none)
Important Issues (2 found)
1. Extra GetTrip call on stripped ID is a wasted query
internal/restapi/trips_for_route_handler.go:375-381
After stripping the suffix and setting baseTripID = stripped (line 373), the code makes a GetTrip call purely for logging:
baseTripID = stripped
if _, err2 := api.GtfsManager.GtfsDB.Queries.GetTrip(ctx, baseTripID); err2 != nil {
// ... log warnings ...
}The result is discarded — baseTripID stays as stripped regardless. The downstream code (buildScheduleForTrip, BuildTripStatus) will discover and handle a missing trip on its own. This is a redundant DB query on every DUPLICATED vehicle with a suffix.
Remove this GetTrip call. The existing downstream error handling is sufficient, and the debug log on line 371 already records the fallback.
2. Commented-out code should be removed
internal/restapi/trips_for_route_handler_test.go:400-401
// Temporarily override the API clock
// api.Clock = mockClock{Time: pastMidnightTime}Dead commented-out code shouldn't be committed. Since the test uses the time query parameter instead, just remove these two lines.
Suggestions (1 found)
1. time.Sleep(500ms) is a flaky test pattern
internal/restapi/trips_for_route_handler_test.go:355
time.Sleep(500 * time.Millisecond)This waits for the real-time poller to complete its first fetch, but the duration is arbitrary — it could be too short on slow CI or too long on fast machines. I know other tests in the codebase already use this pattern, so it's not unique to this PR, but worth noting. Non-blocking for now.
Strengths
- The redundant
GetTripsInBlockquery removal is correct —GetActiveTripInBlockAtTime+ fallback toGetTripsInBlockWithTimeBoundscovers all cases, saving a query per block per service day - The clock-based time handling (
api.Clock.Now()when notimeparam) is a meaningful improvement — makes tests deterministic without mocking - Tests are now pinned to a specific time and route (
25_151at noon Pacific on a Tuesday) that reliably has service, eliminating the flakyminExpected: 0range - The
selectBestTripInBlocktest for the "trip spans now" case is a good edge case to cover SetRealTimeVehiclesForTestfollows the existing per-feed pattern correctly, using"_test"feed ID- Comment corrections (occupancy defaults to
-1, not0) are accurate
Recommended Action
- Remove the redundant
GetTripcall at line 375-381 - Remove the commented-out code at line 400-401
- Consider the
time.Sleepsuggestion for a future cleanup
Description
This PR addresses follow-up items from #679 to improve logging, increase test coverage, optimize queries, and align with the OpenAPI spec for the
trips-for-routeendpoint.Changes
Logging
ErrNoRowserrors when resolving base trip references.GetRouteIDsForStopsbatch queries.buildScheduleForTrip.-1).Test Coverage
TestSelectBestTripInBlock.Performance
GetTripsInBlockquery.GetTripsInBlockWithTimeBoundsfor both validation and selection.Spec Compliance
omitemptyfromLastKnownLocationto match OpenAPI expectations.Testing
make test).fixes #748
Made a extra changes in
internal/restapi/arrivals_and_departures_for_stop_handler_test.gothis just to fix the CI failure, made a separate PR #754