forked from codedge-llc/pigeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpigeon_test.exs
85 lines (65 loc) · 2.43 KB
/
pigeon_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
defmodule PigeonTest do
use ExUnit.Case
@n %Pigeon.FCM.Notification{target: {:token, "test"}}
test "json_library/0 defaults to Jason" do
assert Pigeon.json_library() == Jason
end
test "default_pool_size/0 defaults to 5" do
assert Pigeon.default_pool_size() == 5
end
describe "push/3" do
test "synchronously sends a push by default" do
n = Pigeon.push(PigeonTest.Sandbox, @n)
assert n.response == :success
end
test "synchronously sends a list of notifications" do
notifs = Pigeon.push(PigeonTest.Sandbox, [@n, @n])
for n <- notifs, do: assert(n.response == :success)
end
test "response: :timeout if timed out" do
n = Pigeon.push(PigeonTest.Sandbox, @n, timeout: 0)
assert n.response == :timeout
notifs = Pigeon.push(PigeonTest.Sandbox, [@n, @n], timeout: 0)
for n <- notifs, do: assert(n.response == :timeout)
end
test "response: :not_started if pid not alive" do
pid = Process.spawn(& &1, [])
Process.exit(pid, :stop)
n = Pigeon.push(pid, @n)
assert n.response == :not_started
end
end
describe "push/3 with on_response" do
test "asynchronously sends a push" do
pid = self()
on_response = fn x -> send(pid, x) end
assert Pigeon.push(PigeonTest.Sandbox, @n, on_response: on_response) ==
:ok
assert_receive(%Pigeon.FCM.Notification{response: :success}, 5_000)
end
test "asynchronously sends a list of notifications" do
pid = self()
on_response = fn x -> send(pid, x) end
assert Pigeon.push(PigeonTest.Sandbox, [@n, @n], on_response: on_response) ==
[:ok, :ok]
assert_receive(%Pigeon.FCM.Notification{response: :success}, 5_000)
assert_receive(%Pigeon.FCM.Notification{response: :success}, 5_000)
end
test "blackholes the response if nil" do
assert Pigeon.push(PigeonTest.Sandbox, @n, on_response: nil) == :ok
end
test "accepts {m, f} tuple" do
on_response = {PigeonTest, :assert_success}
assert Pigeon.push(PigeonTest.Sandbox, @n, on_response: on_response) ==
:ok
end
test "accepts {m, f, a} tuple" do
on_response = {PigeonTest, :assert_success, [:other_data]}
assert Pigeon.push(PigeonTest.Sandbox, @n, on_response: on_response) ==
:ok
end
end
def assert_success(notification, _opts \\ []) do
assert notification.response == :success
end
end