Skip to content

Commit

Permalink
fix(PredictionsChannel): filter out past predictions (#2229)
Browse files Browse the repository at this point in the history
* fix(PredictionsChannel): filter out past predictions

* fixup for the linter
  • Loading branch information
thecristen authored Nov 22, 2024
1 parent 4bf3b89 commit 038db3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/dotcom_web/channels/predictions_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule DotcomWeb.PredictionsChannel do
no_departure_time?(prediction) ||
skipped_or_cancelled?(prediction)
end)
|> Enum.filter(&in_future_seconds?/1)
end

defp no_trip?(prediction), do: is_nil(prediction.trip)
Expand All @@ -61,4 +62,8 @@ defmodule DotcomWeb.PredictionsChannel do
Route.subway?(prediction.route.type, prediction.route.id) &&
prediction.schedule_relationship in [:cancelled, :skipped]
end

defp in_future_seconds?(prediction) do
Timex.compare(prediction.departure_time, Util.now(), :seconds) >= 0
end
end
25 changes: 25 additions & 0 deletions test/dotcom_web/channels/predictions_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ defmodule DotcomWeb.PredictionsChannelTest do
# Verify
assert_push("data", %{predictions: ^predictions})
end

test "filters out past predictions", context do
# Setup
now = Timex.now() |> Timex.shift(seconds: 1)
past = Timex.shift(now, seconds: -15)
future = Timex.shift(now, seconds: 15)

predictions =
[past, now, future]
|> Enum.map(&Prediction.build(:canonical_prediction, %{departure_time: &1}))

expect(@predictions_pub_sub, :subscribe, fn _ ->
predictions
end)

{:ok, _, socket} = subscribe_and_join(context.socket, PredictionsChannel, context.channel)

# Exercise
PredictionsChannel.handle_info({:new_predictions, predictions}, socket)

[_past_prediction | expected_predictions] = predictions

# Verify
assert_push("data", %{predictions: ^expected_predictions})
end
end

describe "terminate/2" do
Expand Down

0 comments on commit 038db3d

Please sign in to comment.