Skip to content

Commit 41f279b

Browse files
committed
Merge pull request #214 from hexpm/emj-travis-errors
Fix travis errors
2 parents 71908e7 + 0235638 commit 41f279b

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ before_install:
1515
- wget http://s3.hex.pm/builds/elixir/${ELIXIR}.zip
1616
- unzip -d elixir ${ELIXIR}.zip
1717
- export PATH=$(pwd)/elixir/bin:${PATH}
18+
19+
- mkdir -p ${HEXWEB_MIX_HOME}
20+
- PATH=$(pwd)/${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/${HEXWEB_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/${HEXWEB_MIX_HOME} mix local.hex --force
1821
- mix local.hex --force
1922
before_script:
2023
- git clone https://github.com/hexpm/hex_web.git
21-
- cd hex_web; PATH=$(pwd)/../${HEXWEB_ELIXIR_PATH}/bin:$(pwd)/../${HEXWEB_OTP_PATH}/bin:${PATH} ../${HEXWEB_ELIXIR_PATH}/bin/mix deps.get; cd ..
22-
- 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 ..
24+
- 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 ..
25+
- 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 ..
2326
- mix deps.get
2427
- MIX_ENV=test mix deps.compile
2528
script:
@@ -32,6 +35,7 @@ env:
3235
- HEXWEB_PATH=hex_web
3336
- HEXWEB_ELIXIR_PATH=hexweb_elixir
3437
- HEXWEB_OTP_PATH=hexweb_otp
38+
- HEXWEB_MIX_HOME=hexweb_mix
3539
matrix:
3640
- ELIXIR=v1.0.5
3741
- ELIXIR=v1.1.1

lib/hex/api.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule Hex.API do
2424
http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url)
2525
opts = [body_format: :binary]
2626
url = String.to_char_list(url)
27+
profile = Hex.State.fetch!(:httpc_profile)
2728

2829
request =
2930
cond do
@@ -37,7 +38,7 @@ defmodule Hex.API do
3738
{url, Map.to_list(headers)}
3839
end
3940

40-
case :httpc.request(method, request, http_opts, opts, :hex) do
41+
case :httpc.request(method, request, http_opts, opts, profile) do
4142
{:ok, response} ->
4243
handle_response(response)
4344
{:error, reason} ->
@@ -101,6 +102,7 @@ defmodule Hex.API do
101102
http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url)
102103
opts = [body_format: :binary]
103104
url = String.to_char_list(url)
105+
profile = Hex.State.fetch!(:httpc_profile)
104106

105107
body = fn
106108
size when size < byte_size(body) ->
@@ -114,7 +116,7 @@ defmodule Hex.API do
114116

115117
request = {url, Map.to_list(headers), 'application/octet-stream', {body, 0}}
116118

117-
case :httpc.request(method, request, http_opts, opts, :hex) do
119+
case :httpc.request(method, request, http_opts, opts, profile) do
118120
{:ok, response} ->
119121
handle_response(response)
120122
{:error, reason} ->

lib/hex/state.ex

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,35 @@ defmodule Hex.State do
4444
check_cert?: load_config(config, ["HEX_UNSAFE_HTTPS"], :unsafe_https) |> to_boolean |> default(true),
4545
check_registry?: load_config(config, ["HEX_UNSAFE_REGISTRY"], :unsafe_registry) |> to_boolean |> default(true),
4646
hexpm_pk: @hexpm_pk,
47-
registry_updated: false}
47+
registry_updated: false,
48+
httpc_profile: :hex}
49+
end
50+
51+
# Work around for :socket_closed_remotely errors in httpc
52+
# Use a unique profile for each request to avoid races
53+
if Mix.env == :test do
54+
def fetch(:httpc_profile) do
55+
profile = make_ref() |> :erlang.ref_to_list |> List.to_atom
56+
{:ok, _pid} = :httpc_manager.start_link(profile, :only_session_cookies, :stand_alone)
57+
end
4858
end
4959

5060
def fetch(key) do
5161
Agent.get(@name, Map, :fetch, [key])
5262
end
5363

5464
def fetch!(key) do
55-
Agent.get(@name, Map, :fetch!, [key])
65+
case fetch(key) do
66+
{:ok, value} -> value
67+
:error -> raise KeyError, key: key, term: Hex.State
68+
end
5669
end
5770

5871
def get(key, default \\ nil) do
59-
Agent.get(@name, Map, :get, [key, default])
72+
case fetch(key) do
73+
{:ok, value} -> value
74+
:error -> default
75+
end
6076
end
6177

6278
def put(key, value) do

test/support/hex_web.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ defmodule HexTest.HexWeb do
2828

2929
def start do
3030
path = String.to_char_list(path())
31+
hexweb_mix_home = String.to_char_list(hexweb_mix_home())
3132
key = Path.join(__DIR__, "../fixtures/test_priv.pem")
3233
|> File.read!
3334
|> String.to_char_list
3435

3536
env = [
3637
{'MIX_ENV', 'hex'},
38+
{'MIX_HOME', hexweb_mix_home},
3739
{'PATH', path},
3840
{'HEX_SIGNING_KEY', key}
3941
]
@@ -101,11 +103,22 @@ defmodule HexTest.HexWeb do
101103
end
102104
end
103105

106+
defp hexweb_mix_home do
107+
(System.get_env("HEXWEB_MIX_HOME") || Mix.Utils.mix_home)
108+
|> Path.expand
109+
end
110+
104111
defp cmd(command, args) do
112+
env = [
113+
{"MIX_ENV", "hex"},
114+
{"PATH", path()},
115+
{"MIX_HOME", hexweb_mix_home()}
116+
]
117+
105118
opts = [
106119
stderr_to_stdout: true,
107120
into: IO.stream(:stdio, :line),
108-
env: [{"MIX_ENV", "hex"}, {"PATH", path()}],
121+
env: env,
109122
cd: hexweb_dir()]
110123

111124
0 = System.cmd(command, args, opts) |> elem(1)

0 commit comments

Comments
 (0)