Skip to content

Commit

Permalink
test: reformat test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Jun 11, 2024
1 parent 1f5a442 commit efc3721
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/logflare/backends/adaptor/tcp_adaptor.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Logflare.Backends.Adaptor.TCPAdaptor do
use TypedStruct

import Ecto.Changeset

alias Logflare.Backends.Adaptor.TCPAdaptor.Pool
alias Logflare.Backends.Adaptor.TCPAdaptor.Syslog

Expand All @@ -20,13 +22,11 @@ defmodule Logflare.Backends.Adaptor.TCPAdaptor do
@impl true
def cast_config(params) do
{%{}, %{tls: :bool, host: :string, port: :integer}}
|> Ecto.Changeset.cast(params, [:tls, :host, :port])
|> cast(params, [:tls, :host, :port])
end

@impl true
def validate_config(changeset) do
import Ecto.Changeset

changeset
# Port is at most max(u16)
|> validate_inclusion(:port, 0..0xFFFF)
Expand Down
40 changes: 29 additions & 11 deletions test/logflare/backends/adaptor/tcp_adaptor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,34 @@ defmodule Logflare.Backends.Adaptor.TCPAdaptorTest do
{:ok, source: source, backend: backend, port: port, socket: socket}
end

test "ingest/3", %{source: source, backend: backend} do
le = build(:log_event, source: source)
describe "ingest/3" do
test "simple message", %{source: source, backend: backend} do
le = build(:log_event, source: source)

{:ok, pid} = @subject.start_link({source, backend})
{:ok, pid} = @subject.start_link({source, backend})

_ = @subject.ingest(pid, [le], [])
_ = @subject.ingest(pid, [le], [])

assert_receive {:tcp, _msg}, 5000
assert_receive {:tcp, _msg}, 5000
end

test "message contains source ID", %{source: source, backend: backend} do
le = build(:log_event, source: source)

{:ok, pid} = @subject.start_link({source, backend})

_ = @subject.ingest(pid, [le], [])

assert_receive {:tcp, msg}, 5000

assert msg =~ ~r/id=#{source.id}/
end
end

# Simple TCP server
defp listen do
this = self()

spawn_link(fn ->
{:ok, sock} =
:gen_tcp.listen(0,
Expand All @@ -57,11 +72,12 @@ defmodule Logflare.Backends.Adaptor.TCPAdaptorTest do
{:ok, lsock} = :gen_tcp.accept(socket)
ref = make_ref()

pid = spawn_link(fn ->
receive do
^ref -> server(lsock, parent)
end
end)
pid =
spawn_link(fn ->
receive do
^ref -> server(lsock, parent)
end
end)

:gen_tcp.controlling_process(lsock, pid)
send(pid, ref)
Expand All @@ -71,7 +87,9 @@ defmodule Logflare.Backends.Adaptor.TCPAdaptorTest do

defp server(sock, pid) do
receive do
{:tcp_close, ^sock} -> :ok
{:tcp_close, ^sock} ->
:ok

{:tcp, ^sock, msg} ->
send(pid, {:tcp, msg})
server(sock, pid)
Expand Down

0 comments on commit efc3721

Please sign in to comment.