Packages
thousand_island
0.4.2
1.5.0
1.4.3
1.4.2
1.4.1
1.4.0
1.3.14
1.3.13
1.3.12
1.3.11
1.3.10
1.3.9
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.0
1.1.0
1.0.0
1.0.0-pre.7
1.0.0-pre.6
1.0.0-pre.5
1.0.0-pre.4
1.0.0-pre.3
1.0.0-pre.2
1.0.0-pre.1
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.17
0.5.16
0.5.15
0.5.14
0.5.13
0.5.12
0.5.11
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
A simple & modern pure Elixir socket server
Current section
Files
Jump to
Current section
Files
test/thousand_island/server_test.exs
defmodule ThousandIsland.ServerTest do
# False due to telemetry raciness
use ExUnit.Case, async: false
defmodule Echo do
use ThousandIsland.Handler
@impl ThousandIsland.Handler
def handle_connection(socket, state) do
{:ok, data} = ThousandIsland.Socket.recv(socket, 0)
ThousandIsland.Socket.send(socket, data)
{:ok, :close, state}
end
end
describe "tests with an echo handler" do
test "should handle multiple connections as expected" do
{:ok, _, port} = start_handler(Echo)
{:ok, client} = :gen_tcp.connect(:localhost, port, active: false)
{:ok, other_client} = :gen_tcp.connect(:localhost, port, active: false)
:ok = :gen_tcp.send(client, "HELLO")
:ok = :gen_tcp.send(other_client, "BONJOUR")
# Invert the order to ensure we handle concurrently
assert :gen_tcp.recv(other_client, 0) == {:ok, 'BONJOUR'}
assert :gen_tcp.recv(client, 0) == {:ok, 'HELLO'}
:gen_tcp.close(client)
:gen_tcp.close(other_client)
end
test "it should stop accepting connections but allow existing ones to complete" do
{:ok, server_pid, port} = start_handler(Echo)
{:ok, client} = :gen_tcp.connect(:localhost, port, active: false)
# Make sure the socket has transitioned ownership to the connection process
Process.sleep(100)
task = Task.async(fn -> ThousandIsland.stop(server_pid) end)
# Make sure that the stop has had a chance to shutdown the acceptors
Process.sleep(100)
assert :gen_tcp.connect(:localhost, port, [active: false], 100) == {:error, :econnrefused}
:ok = :gen_tcp.send(client, "HELLO")
assert :gen_tcp.recv(client, 0) == {:ok, 'HELLO'}
:gen_tcp.close(client)
Task.await(task)
refute Process.alive?(server_pid)
end
test "it should emit telemetry events as expected" do
{:ok, collector_pid} = start_collector()
{:ok, server_pid, _} = start_handler(Echo)
ThousandIsland.stop(server_pid)
events = ThousandIsland.TelemetryCollector.get_events(collector_pid)
assert length(events) == 2
assert {[:listener, :start], %{}, _} = Enum.at(events, 0)
assert {[:listener, :shutdown], %{}, _} = Enum.at(events, 1)
end
end
defp start_handler(handler) do
resolved_args = [port: 0, handler_module: handler]
{:ok, server_pid} = start_supervised({ThousandIsland, resolved_args})
{:ok, port} = ThousandIsland.local_port(server_pid)
{:ok, server_pid, port}
end
defp start_collector do
start_supervised(
{ThousandIsland.TelemetryCollector, [[:listener, :start], [:listener, :shutdown]]}
)
end
end