Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Parameterize TCP timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
suvash committed Jul 24, 2016
1 parent ac907d8 commit cd988c3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/hulaaki/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ defmodule Hulaaki.Client do
def handle_call({:connect, opts, conn_pid}, _from, state) do
host = opts |> Keyword.fetch!(:host)
port = opts |> Keyword.fetch!(:port)
timeout = opts |> Keyword.get(:timeout, 100)

client_id = opts |> Keyword.fetch!(:client_id)
username = opts |> Keyword.get(:username, "")
Expand All @@ -74,7 +75,7 @@ defmodule Hulaaki.Client do

state = Map.merge(%{connection: conn_pid}, state)

connect_opts = [host: host, port: port]
connect_opts = [host: host, port: port, timeout: timeout]
:ok = state.connection |> Connection.connect(message, connect_opts)
{:reply, :ok, state}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hulaaki/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule Hulaaki.Connection do
end

defp open_tcp_socket(opts) do
timeout = 100
timeout = opts |> Keyword.fetch!(:timeout)
host = opts |> Keyword.fetch!(:host)
host = if is_binary(host), do: String.to_char_list(host), else: host
port = opts |> Keyword.fetch!(:port)
Expand Down
2 changes: 1 addition & 1 deletion test/hulaaki/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule Hulaaki.ClientTest do
end

defp pre_connect(pid) do
options = [client_id: "some-name", host: TestConfig.mqtt_host, port: TestConfig.mqtt_port]
options = [client_id: "some-name", host: TestConfig.mqtt_host, port: TestConfig.mqtt_port, timeout: 200]
SampleClient.connect(pid, options)
end

Expand Down
2 changes: 1 addition & 1 deletion test/hulaaki/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Hulaaki.ConnectionTest do

defp pre_connect(pid) do
message = Message.connect(client_name, "", "", "", "", 0, 0, 0, 100)
Connection.connect(pid, message, [host: TestConfig.mqtt_host, port: TestConfig.mqtt_port])
Connection.connect(pid, message, [host: TestConfig.mqtt_host, port: TestConfig.mqtt_port, timeout: TestConfig.mqtt_timeout])
end

defp post_disconnect(pid) do
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ defmodule TestConfig do
{port, _} = ( System.get_env("MQTT_PORT") || "1883" ) |> Integer.parse
port
end

def mqtt_timeout do
500
end
end

0 comments on commit cd988c3

Please # to comment.