From b252a7d182f47317facad1ac28ae76ada07efbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Mon, 14 Mar 2016 16:37:38 +0100 Subject: [PATCH 1/2] Fix hex version warnings on travis --- .travis.yml | 8 ++++++-- test/support/hex_web.ex | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6f6fe075..b340fed2b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,11 +15,14 @@ before_install: - wget http://s3.hex.pm/builds/elixir/${ELIXIR}.zip - unzip -d elixir ${ELIXIR}.zip - export PATH=$(pwd)/elixir/bin:${PATH} + + - mkdir -p ${HEXWEB_MIX_HOME} + - PATH=$(pwd)/${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/${HEXWEB_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/${HEXWEB_MIX_HOME} mix local.hex --force - mix local.hex --force before_script: - git clone https://github.com/hexpm/hex_web.git - - cd hex_web; PATH=$(pwd)/../${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/../${HEXWEB_OTP_PATH}/bin:${PATH} ../${HEXWEB_ELIXIR_PATH}/bin/mix deps.get; cd .. - - cd hex_web; PATH=$(pwd)/../${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/../${HEXWEB_OTP_PATH}/bin:${PATH} MIX_ENV=hex ../${HEXWEB_ELIXIR_PATH}/bin/mix compile; cd .. + - cd hex_web; PATH=$(pwd)/../${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/../${HEXWEB_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/../${HEXWEB_MIX_HOME} MIX_ENV=hex ../${HEXWEB_ELIXIR_PATH}/bin/mix deps.get; cd .. + - cd hex_web; PATH=$(pwd)/../${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/../${HEXWEB_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/../${HEXWEB_MIX_HOME} MIX_ENV=hex ../${HEXWEB_ELIXIR_PATH}/bin/mix compile; cd .. - mix deps.get - MIX_ENV=test mix deps.compile script: @@ -32,6 +35,7 @@ env: - HEXWEB_PATH=hex_web - HEXWEB_ELIXIR_PATH=hexweb_elixir - HEXWEB_OTP_PATH=hexweb_otp + - HEXWEB_MIX_HOME=hexweb_mix matrix: - ELIXIR=v1.0.5 - ELIXIR=v1.1.1 diff --git a/test/support/hex_web.ex b/test/support/hex_web.ex index d7de4465b..e783b316d 100644 --- a/test/support/hex_web.ex +++ b/test/support/hex_web.ex @@ -28,12 +28,14 @@ defmodule HexTest.HexWeb do def start do path = String.to_char_list(path()) + hexweb_mix_home = String.to_char_list(hexweb_mix_home()) key = Path.join(__DIR__, "../fixtures/test_priv.pem") |> File.read! |> String.to_char_list env = [ {'MIX_ENV', 'hex'}, + {'MIX_HOME', hexweb_mix_home}, {'PATH', path}, {'HEX_SIGNING_KEY', key} ] @@ -101,11 +103,22 @@ defmodule HexTest.HexWeb do end end + defp hexweb_mix_home do + (System.get_env("HEXWEB_MIX_HOME") || Mix.Utils.mix_home) + |> Path.expand + end + defp cmd(command, args) do + env = [ + {"MIX_ENV", "hex"}, + {"PATH", path()}, + {"MIX_HOME", hexweb_mix_home()} + ] + opts = [ stderr_to_stdout: true, into: IO.stream(:stdio, :line), - env: [{"MIX_ENV", "hex"}, {"PATH", path()}], + env: env, cd: hexweb_dir()] 0 = System.cmd(command, args, opts) |> elem(1) From 02356386fbcd9eddc9bd0fa4ead601fbc1cae179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Mon, 14 Mar 2016 16:37:57 +0100 Subject: [PATCH 2/2] Fix :socket_closed_remotely error on travis --- lib/hex/api.ex | 6 ++++-- lib/hex/state.ex | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/hex/api.ex b/lib/hex/api.ex index eb79724f4..d2b4a9fd7 100644 --- a/lib/hex/api.ex +++ b/lib/hex/api.ex @@ -24,6 +24,7 @@ defmodule Hex.API do http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url) opts = [body_format: :binary] url = String.to_char_list(url) + profile = Hex.State.fetch!(:httpc_profile) request = cond do @@ -37,7 +38,7 @@ defmodule Hex.API do {url, Map.to_list(headers)} end - case :httpc.request(method, request, http_opts, opts, :hex) do + case :httpc.request(method, request, http_opts, opts, profile) do {:ok, response} -> handle_response(response) {:error, reason} -> @@ -101,6 +102,7 @@ defmodule Hex.API do http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url) opts = [body_format: :binary] url = String.to_char_list(url) + profile = Hex.State.fetch!(:httpc_profile) body = fn size when size < byte_size(body) -> @@ -114,7 +116,7 @@ defmodule Hex.API do request = {url, Map.to_list(headers), 'application/octet-stream', {body, 0}} - case :httpc.request(method, request, http_opts, opts, :hex) do + case :httpc.request(method, request, http_opts, opts, profile) do {:ok, response} -> handle_response(response) {:error, reason} -> diff --git a/lib/hex/state.ex b/lib/hex/state.ex index ed5d38802..e0e30b84a 100644 --- a/lib/hex/state.ex +++ b/lib/hex/state.ex @@ -44,7 +44,17 @@ defmodule Hex.State do check_cert?: load_config(config, ["HEX_UNSAFE_HTTPS"], :unsafe_https) |> to_boolean |> default(true), check_registry?: load_config(config, ["HEX_UNSAFE_REGISTRY"], :unsafe_registry) |> to_boolean |> default(true), hexpm_pk: @hexpm_pk, - registry_updated: false} + registry_updated: false, + httpc_profile: :hex} + end + + # Work around for :socket_closed_remotely errors in httpc + # Use a unique profile for each request to avoid races + if Mix.env == :test do + def fetch(:httpc_profile) do + profile = make_ref() |> :erlang.ref_to_list |> List.to_atom + {:ok, _pid} = :httpc_manager.start_link(profile, :only_session_cookies, :stand_alone) + end end def fetch(key) do @@ -52,11 +62,17 @@ defmodule Hex.State do end def fetch!(key) do - Agent.get(@name, Map, :fetch!, [key]) + case fetch(key) do + {:ok, value} -> value + :error -> raise KeyError, key: key, term: Hex.State + end end def get(key, default \\ nil) do - Agent.get(@name, Map, :get, [key, default]) + case fetch(key) do + {:ok, value} -> value + :error -> default + end end def put(key, value) do