summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-04-25 21:57:19 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-04-25 21:57:19 -0400
commitd186b7d7d799f4cb7e201077760904821298247f (patch)
treed700a48c027cc002f3890434a9bf825e985484a1
parentmore unit tests (diff)
test message streaming
-rw-r--r--test/polyjuice/client/room_test.exs52
-rw-r--r--test/support/dummy_client.ex48
2 files changed, 99 insertions, 1 deletions
diff --git a/test/polyjuice/client/room_test.exs b/test/polyjuice/client/room_test.exs
index afc2052..f5470bc 100644
--- a/test/polyjuice/client/room_test.exs
+++ b/test/polyjuice/client/room_test.exs
@@ -237,4 +237,56 @@ defmodule Polyjuice.Client.RoomTest do
)
end
end
+
+ test "stream messages" do
+ with client =
+ DummyClient.MultiReq.create([
+ {
+ %Polyjuice.Client.Endpoint.GetRoomsMessages{
+ room: "!room",
+ from: "token1",
+ dir: :backward
+ },
+ {:ok,
+ %{
+ "start" => "token1",
+ "end" => "token2",
+ "chunk" => [
+ "event1",
+ "event2"
+ ]
+ }}
+ },
+ {
+ %Polyjuice.Client.Endpoint.GetRoomsMessages{
+ room: "!room",
+ from: "token2",
+ dir: :backward
+ },
+ {:ok,
+ %{
+ "start" => "token2",
+ "end" => "token2",
+ "chunk" => []
+ }}
+ }
+ ]) do
+ events =
+ Polyjuice.Client.Room.stream_messages(client, "!room", "token1", :backward)
+ |> Enum.to_list()
+
+ assert events == [
+ %{
+ "start" => "token1",
+ "end" => "token2",
+ "chunk" => [
+ "event1",
+ "event2"
+ ]
+ }
+ ]
+
+ DummyClient.MultiReq.destroy(client)
+ end
+ end
end
diff --git a/test/support/dummy_client.ex b/test/support/dummy_client.ex
index 1506431..0a6ad21 100644
--- a/test/support/dummy_client.ex
+++ b/test/support/dummy_client.ex
@@ -1,4 +1,4 @@
-# Copyright 2019 Hubert Chathi <hubert@uhoreg.ca>
+# Copyright 2019-2020 Hubert Chathi <hubert@uhoreg.ca>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -32,4 +32,50 @@ defmodule DummyClient do
%{}
end
end
+
+ defmodule MultiReq do
+ require ExUnit.Assertions
+ import ExUnit.Assertions
+ defstruct [:pid]
+
+ def create(responses) when is_list(responses) do
+ {:ok, pid} = Agent.start(fn -> responses end)
+
+ %MultiReq{
+ pid: pid
+ }
+ end
+
+ def destroy(%{pid: pid}) do
+ # make sure we weren't expecting any more requests
+ remaining = Agent.get(pid, & &1)
+ assert remaining == []
+
+ Process.exit(pid, :kill)
+ end
+
+ defimpl Polyjuice.Client.API do
+ def call(%{pid: pid}, endpoint) do
+ {request, result} =
+ Agent.get_and_update(
+ pid,
+ fn
+ [head | rest] -> {head, rest}
+ [] -> {nil, nil}
+ end
+ )
+
+ assert request != nil
+ assert endpoint == request
+
+ result
+ end
+
+ def transaction_id(_), do: "txn_id"
+
+ def sync_child_spec(_client_api, _listener, _opts \\ []) do
+ %{}
+ end
+ end
+ end
end