From 7c2a84b5832903427c4a613011e09e467a90f077 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Mon, 9 Sep 2024 09:53:54 +0200 Subject: [PATCH 1/3] Fix red CI --- .github/workflows/main.yml | 2 +- README.md | 4 ++-- test/redix/pubsub_test.exs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cbd422c..1140395 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,7 +38,7 @@ jobs: uses: actions/checkout@v3 - name: Start Docker - run: docker-compose up --detach + run: docker compose up --detach - name: Install OTP and Elixir uses: erlef/setup-beam@v1 diff --git a/README.md b/README.md index aea5876..09b3df3 100644 --- a/README.md +++ b/README.md @@ -131,10 +131,10 @@ Redix is low-level, but it's still built to handle most things thrown at it. For ## Contributing -To run the Redix test suite you will have to have Redis running locally. Redix requires a somewhat complex setup for running tests (because it needs a few instances running, for pub/sub and sentinel). For this reason, in this repository you'll find a `docker-compose.yml` file so that you can use [Docker][docker] and [docker-compose][] to spin up all the necessary Redis instances with just one command. Make sure you have Docker installed and then just run: +To run the Redix test suite you will have to have Redis running locally. Redix requires a somewhat complex setup for running tests (because it needs a few instances running, for pub/sub and sentinel). For this reason, in this repository you'll find a `docker-compose.yml` file so that you can use [Docker][docker] and [docker compose][docker-compose] to spin up all the necessary Redis instances with just one command. Make sure you have Docker installed and then just run: ```bash -docker-compose up +docker compose up ``` Now, you're ready to run tests with the `$ mix test` command. diff --git a/test/redix/pubsub_test.exs b/test/redix/pubsub_test.exs index 23eccca..e60ddce 100644 --- a/test/redix/pubsub_test.exs +++ b/test/redix/pubsub_test.exs @@ -7,7 +7,7 @@ defmodule Redix.PubSubTest do @moduletag :pubsub - # See docker-compose. + # See docker-compose.yml. @port 6380 setup do From b201e15d249b81bc3f8f3e9d8e44c574525df8de Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Mon, 9 Sep 2024 10:20:34 +0200 Subject: [PATCH 2/3] Fix --- .github/workflows/main.yml | 19 +++++++++---------- lib/redix/connector.ex | 20 +++++++++++++++++--- lib/redix/format.ex | 10 ++++++++++ test/redix/sentinel_test.exs | 1 + 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1140395..59b6d74 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,33 +9,33 @@ on: jobs: test: name: Test (Elixir ${{matrix.elixir}} | Erlang/OTP ${{matrix.otp}}) - runs-on: ubuntu-20.04 + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - otp: "27.0" - elixir: "1.17.0" - version-type: strict + elixir: "1.17" + os: ubuntu-latest dialyzer: true lint: true - otp: "25.3" elixir: "1.14.3" + os: ubuntu-20.04 coverage: true - version-type: loose - otp: "23.0" elixir: "1.12" - version-type: loose + os: ubuntu-20.04 env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} MIX_ENV: test steps: - name: Clone the repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Start Docker run: docker compose up --detach @@ -43,9 +43,8 @@ jobs: - name: Install OTP and Elixir uses: erlef/setup-beam@v1 with: - otp-version: ${{matrix.otp}} - elixir-version: ${{matrix.elixir}} - version-type: ${{matrix.version-type}} + otp-version: ${{ matrix.otp }} + elixir-version: ${{ matrix.elixir }} - name: Cache dependencies id: cache-deps diff --git a/lib/redix/connector.ex b/lib/redix/connector.ex index 93f1adb..b4c9955 100644 --- a/lib/redix/connector.ex +++ b/lib/redix/connector.ex @@ -137,15 +137,16 @@ defmodule Redix.Connector do Logger.debug(fn -> "Sentinel reported #{sentinel_opts[:role]}: #{server_host}:#{server_port}" end), - {:ok, server_socket, _address} <- + server_host = string_address_to_erlang(server_host), + {:ok, server_socket, address} <- connect_directly( - String.to_charlist(server_host), + server_host, String.to_integer(server_port), opts ), :ok <- verify_server_role(server_socket, opts, sentinel_opts) do :ok = transport.close(sent_socket) - {:ok, server_socket, "#{server_host}:#{server_port}"} + {:ok, server_socket, address} else {cause, reason} when cause in [:error, :stop] -> :telemetry.execute([:redix, :failed_connection], %{}, %{ @@ -171,6 +172,19 @@ defmodule Redix.Connector do end end + defp string_address_to_erlang(address) when is_binary(address) do + address = String.to_charlist(address) + + case :inet.parse_address(address) do + {:ok, ip} -> ip + {:error, :einval} -> address + end + end + + defp string_address_to_erlang(address) do + address + end + defp connect_to_sentinel(sentinel, sentinel_opts, transport) do host = Keyword.fetch!(sentinel, :host) port = Keyword.fetch!(sentinel, :port) diff --git a/lib/redix/format.ex b/lib/redix/format.ex index d47ea4a..88594a9 100644 --- a/lib/redix/format.ex +++ b/lib/redix/format.ex @@ -14,4 +14,14 @@ defmodule Redix.Format do def format_host_and_port(host, port) when is_list(host), do: format_host_and_port(IO.chardata_to_string(host), port) + + def format_host_and_port(host, port) when is_tuple(host) do + case :inet.ntoa(host) do + {:error, :einval} -> + raise ArgumentError, "invalid host: #{inspect(host)}" + + host -> + format_host_and_port(host, port) + end + end end diff --git a/test/redix/sentinel_test.exs b/test/redix/sentinel_test.exs index 1cf6a91..5397679 100644 --- a/test/redix/sentinel_test.exs +++ b/test/redix/sentinel_test.exs @@ -49,6 +49,7 @@ defmodule Redix.SentinelTest do assert_receive {:redix_pubsub, ^pubsub, ^ref, :message, %{channel: "foo", payload: "hello"}} end + @tag :capture_log test "when no sentinels are reachable" do Process.flag(:trap_exit, true) From 91a7350960e62047d060305bcf3de03e8f307e79 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Mon, 9 Sep 2024 10:26:43 +0200 Subject: [PATCH 3/3] Fix Dialyzer --- lib/redix/format.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/redix/format.ex b/lib/redix/format.ex index 88594a9..bec5821 100644 --- a/lib/redix/format.ex +++ b/lib/redix/format.ex @@ -4,7 +4,7 @@ defmodule Redix.Format do # Used for formatting things to print or log or anything like that. @spec format_host_and_port(host, :inet.port_number()) :: String.t() - when host: {:local, String.t()} | binary() + when host: {:local, String.t()} | charlist() | binary() | :inet.ip_address() def format_host_and_port(host, port) def format_host_and_port({:local, path}, 0) when is_binary(path), do: path