Skip to content

Commit

Permalink
fix issues causing 500 errors in realtime API (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-mahoney authored Sep 10, 2019
1 parent 44212dd commit c7033a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion apps/schedules/lib/repo_condensed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ defmodule Schedules.RepoCondensed do
alias Stops.Repo, as: StopsRepo
alias V3Api.Schedules, as: SchedulesApi

# the long timeout is to address a worst-case scenario of cold schedule cache
@long_timeout 15_000

@default_params [
include: "trip",
"fields[schedule]": "departure_time,drop_off_type,pickup_type,stop_sequence,timepoint",
Expand Down Expand Up @@ -133,6 +136,6 @@ defmodule Schedules.RepoCondensed do
}
end)
end)
|> Enum.map(&Task.await/1)
|> Enum.map(&Task.await(&1, @long_timeout))
end
end
13 changes: 11 additions & 2 deletions apps/site/lib/site/realtime_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Site.RealtimeSchedule do
@predicted_schedules_per_stop 2

@default_opts [
stops_fn: &StopsRepo.get!/1,
stops_fn: &StopsRepo.get/1,
routes_fn: &RoutesRepo.by_stop_with_route_pattern/1,
predictions_fn: &PredictionsRepo.all_no_cache/1,
schedules_fn: &SchedulesRepo.by_route_ids/2,
Expand Down Expand Up @@ -84,12 +84,21 @@ defmodule Site.RealtimeSchedule do
stop_ids
|> Enum.map(
&Task.async(fn ->
{&1, &1 |> stops_fn.() |> Map.take([:id, :name, :accessibility, :address])}
{&1, &1 |> stops_fn.() |> stop_fields()}
end)
)
|> Enum.into(%{}, &Task.await/1)
end

@spec stop_fields(Stop.t() | nil) :: map
defp stop_fields(nil) do
%{}
end

defp stop_fields(stop) do
Map.take(stop, [:id, :name, :accessibility, :address])
end

@spec get_routes([Stop.id_t()], fun()) :: [route_with_patterns_t]
defp get_routes(stop_ids, routes_fn) do
stop_ids
Expand Down
21 changes: 20 additions & 1 deletion apps/site/test/site/realtime_schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule Site.RealtimeScheduleTest do
}
]

test "stop_data/3" do
test "stop_data/3 returns stop" do
opts = [
stops_fn: fn _ -> @stop end,
routes_fn: fn _ -> @route_with_patterns end,
Expand Down Expand Up @@ -257,4 +257,23 @@ defmodule Site.RealtimeScheduleTest do

assert actual == expected
end

test "stop_data/3 returns nil" do
opts = [
stops_fn: fn _ -> nil end,
routes_fn: fn _ -> [] end,
predictions_fn: fn _ -> [] end,
schedules_fn: fn _, _ -> [] end,
alerts_fn: fn _, _ -> [] end
]

stops = [@stop.id]

expected = []

RealtimeSchedule.clear_cache()
actual = RealtimeSchedule.stop_data(stops, @now, opts)

assert actual == expected
end
end

0 comments on commit c7033a4

Please sign in to comment.