|
| 1 | +defmodule Livebook.Teams.ConnectionTest do |
| 2 | + use ExUnit.Case, async: false |
| 3 | + |
| 4 | + alias Livebook.Teams.Connection |
| 5 | + |
| 6 | + @moduletag :capture_log |
| 7 | + |
| 8 | + setup do |
| 9 | + original_url = Application.get_env(:livebook, :teams_url) |
| 10 | + |
| 11 | + on_exit(fn -> |
| 12 | + Application.put_env(:livebook, :teams_url, original_url) |
| 13 | + end) |
| 14 | + |
| 15 | + :ok |
| 16 | + end |
| 17 | + |
| 18 | + describe "server error handling" do |
| 19 | + test "handles invalid protobuf without crashing" do |
| 20 | + html_error = """ |
| 21 | + <!DOCTYPE html> |
| 22 | + <html><body><h1>500 Internal Server Error</h1></body></html> |
| 23 | + """ |
| 24 | + |
| 25 | + start_error_server(500, html_error, "text/html") |
| 26 | + |
| 27 | + {:ok, conn_pid} = Connection.start_link(self(), [{"x-test", "true"}]) |
| 28 | + |
| 29 | + assert_receive {:server_error, error_message}, 5000 |
| 30 | + assert error_message =~ "Server error (unexpected response format)" |
| 31 | + assert Process.alive?(conn_pid) |
| 32 | + end |
| 33 | + |
| 34 | + test "sends :service_unavailable on 503 and retries with backoff" do |
| 35 | + error = %LivebookProto.Error{details: "Service temporarily unavailable"} |
| 36 | + encoded = LivebookProto.Error.encode(error) |
| 37 | + |
| 38 | + start_error_server(503, encoded, "application/octet-stream") |
| 39 | + |
| 40 | + {:ok, conn_pid} = Connection.start_link(self(), [{"x-test", "true"}]) |
| 41 | + |
| 42 | + # First attempt |
| 43 | + assert_receive {:service_unavailable, "Service temporarily unavailable"}, 1000 |
| 44 | + assert Process.alive?(conn_pid) |
| 45 | + |
| 46 | + # Retry after backoff |
| 47 | + assert_receive {:service_unavailable, _}, 1000 |
| 48 | + assert Process.alive?(conn_pid) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + defp start_error_server(status_code, body, content_type) do |
| 53 | + port = get_free_port() |
| 54 | + Application.put_env(:livebook, :teams_url, "http://localhost:#{port}") |
| 55 | + |
| 56 | + plug = |
| 57 | + {__MODULE__.ErrorPlug, status_code: status_code, body: body, content_type: content_type} |
| 58 | + |
| 59 | + start_supervised!({Bandit, plug: plug, port: port, startup_log: false}) |
| 60 | + end |
| 61 | + |
| 62 | + defp get_free_port do |
| 63 | + {:ok, socket} = :gen_tcp.listen(0, []) |
| 64 | + {:ok, port} = :inet.port(socket) |
| 65 | + :gen_tcp.close(socket) |
| 66 | + port |
| 67 | + end |
| 68 | + |
| 69 | + defmodule ErrorPlug do |
| 70 | + @behaviour Plug |
| 71 | + |
| 72 | + @impl true |
| 73 | + def init(opts), do: opts |
| 74 | + |
| 75 | + @impl true |
| 76 | + def call(conn, opts) do |
| 77 | + status_code = Keyword.fetch!(opts, :status_code) |
| 78 | + body = Keyword.fetch!(opts, :body) |
| 79 | + content_type = Keyword.fetch!(opts, :content_type) |
| 80 | + |
| 81 | + conn |
| 82 | + |> Plug.Conn.put_resp_content_type(content_type) |
| 83 | + |> Plug.Conn.send_resp(status_code, body) |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments