From fedc8275105cfa88d8702ec6910ce8d938ba5620 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 14:24:29 -0500 Subject: [PATCH 01/11] fix(ex/api/stream_test): replace `Bypass` with `GenState.from_enumerable` --- test/api/stream_test.exs | 96 ++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 64 deletions(-) diff --git a/test/api/stream_test.exs b/test/api/stream_test.exs index c9bf09259..538728594 100644 --- a/test/api/stream_test.exs +++ b/test/api/stream_test.exs @@ -2,6 +2,7 @@ defmodule Api.StreamTest do use ExUnit.Case, async: false import Test.Support.Helpers alias Plug.Conn + alias ServerSentEventStage, as: SSES describe "build_options" do setup do @@ -17,38 +18,22 @@ defmodule Api.StreamTest do end describe "start_link" do - setup do - bypass = Bypass.open() - - {:ok, %{bypass: bypass}} - end - - test "starts a genserver that sends events", %{bypass: bypass} do - Bypass.expect(bypass, fn conn -> - conn = Conn.send_chunked(conn, 200) + @data %{ + "attributes" => [], + "type" => "vehicle", + "id" => "vehicle" + } - data = %{ - "attributes" => [], - "type" => "vehicle", - "id" => "vehicle" - } - - Conn.chunk(conn, "event: reset\ndata: #{Jason.encode!([data])}\n\n") - conn - end) + test "starts a genserver that sends events" do + data = @data assert {:ok, sses} = - [ - name: :start_link_test, - base_url: "http://localhost:#{bypass.port}", - path: "/vehicles", - params: [ - "fields[vehicle]": "direction_id,current_status,longitude,latitude,bearing", - include: "stop,trip" - ] - ] - |> Api.Stream.build_options() - |> ServerSentEventStage.start_link() + GenStage.from_enumerable([ + %SSES.Event{ + event: "reset", + data: Jason.encode!(data) + } + ]) assert {:ok, pid} = Api.Stream.start_link(name: __MODULE__, subscribe_to: sses) @@ -58,46 +43,29 @@ defmodule Api.StreamTest do |> Enum.take(1) end - # We're seeing occasional failures in this test due to an underlying issue - # with the `Bypass` library. There is an - # [open issue on Bypass's github](https://github.com/PSPDFKit-labs/bypass/issues/120). - test "handles api events", %{bypass: bypass} do - Bypass.expect(bypass, fn conn -> - conn = Conn.send_chunked(conn, 200) - - data = %{ - "attributes" => [], - "type" => "vehicle", - "id" => "vehicle" - } - - {:ok, conn} = Conn.chunk(conn, "event: ignores unexpected events\n\n") - {:ok, conn} = Conn.chunk(conn, "ignored garbled data\n\n") - {:ok, conn} = Conn.chunk(conn, "event: reset\ndata: #{Jason.encode!([data])}\n\n") - {:ok, conn} = Conn.chunk(conn, "event: add\ndata: #{Jason.encode!(data)}\n\n") - {:ok, conn} = Conn.chunk(conn, "event: update\ndata: #{Jason.encode!(data)}\n\n") - {:ok, conn} = Conn.chunk(conn, "event: remove\ndata: #{Jason.encode!(data)}\n\n") - conn - end) + test "handles api events" do + data = @data assert {:ok, sses} = - [ - base_url: "http://localhost:#{bypass.port}", - path: "/vehicles" - ] - |> Api.Stream.build_options() - |> ServerSentEventStage.start_link() + GenStage.from_enumerable([ + %SSES.Event{event: "ignores unexpected events"}, + # garbled data contains no data and the `event` is `message` + %SSES.Event{event: "message"}, + %SSES.Event{event: "reset", data: Jason.encode!([data])}, + %SSES.Event{event: "add", data: Jason.encode!(data)}, + %SSES.Event{event: "update", data: Jason.encode!(data)}, + %SSES.Event{event: "remove", data: Jason.encode!(data)} + ]) assert {:ok, pid} = Api.Stream.start_link(name: __MODULE__, subscribe_to: sses) - stream = GenStage.stream([pid]) - - assert [ - %Api.Stream.Event{event: :reset}, - %Api.Stream.Event{event: :add}, - %Api.Stream.Event{event: :update}, - %Api.Stream.Event{event: :remove} - ] = Enum.take(stream, 4) + stream = + assert [ + %Api.Stream.Event{event: :reset}, + %Api.Stream.Event{event: :add}, + %Api.Stream.Event{event: :update}, + %Api.Stream.Event{event: :remove} + ] = [pid] |> GenStage.stream() |> Enum.take(4) end end end From 5d37362bd9c6de70c081bee48b60ec4a71a6647b Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 18:11:53 -0500 Subject: [PATCH 02/11] From f4621238f196f58099b39f348872548d4b65c0bb Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 18:11:54 -0500 Subject: [PATCH 03/11] From b2ba7c1d7d07f7ac64f4f15a63ebbd1f39f0d584 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 18:11:55 -0500 Subject: [PATCH 04/11] From 3c600010a9ec8a60dd5a4693b9d91384f05f7ffc Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 18:11:57 -0500 Subject: [PATCH 05/11] From afaa144fd1fcb924402dadb664382fb9c1a97cc2 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 18:11:58 -0500 Subject: [PATCH 06/11] From b3303eb3a4293d536e532ea3858c59b8a23bb0b1 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 20:26:47 -0500 Subject: [PATCH 07/11] From 85eb80f75cb1d4c0ebf777f1c5cfb982ee7d8e65 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 20:26:48 -0500 Subject: [PATCH 08/11] From 5c8d5148f1e0e920c55ae2155a3a48214fde15bd Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 20:26:49 -0500 Subject: [PATCH 09/11] From b7730d739b1a14818317ba6007eddbb662df57fd Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 20:26:50 -0500 Subject: [PATCH 10/11] From ecee66496459e57f237516655ea0c31bacd0b283 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 6 Dec 2023 20:26:51 -0500 Subject: [PATCH 11/11]