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

Fix/parameterize tcp timeout #11

Merged
merged 2 commits into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. This change
### Added
- Added CHANGELOG.md

### Updated
- Parameterize `timeout` for mqtt TCP connection, so it's not hardcoded

## [0.0.3] - 2016-06-16
### Added
- Add Docker support for development environment.
Expand Down
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