Skip to content

Commit 07361a1

Browse files
committed
Add tests for SUBSCRIBE
1 parent 3aa9242 commit 07361a1

File tree

9 files changed

+73
-11
lines changed

9 files changed

+73
-11
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUN mix local.rebar --force &&\
99

1010
# Cache dependencies
1111
COPY mix.exs mix.lock ./
12-
RUN mix deps.get
12+
RUN mix deps.get --only prod
1313
COPY config ./config
1414
RUN mix deps.compile
1515

config/test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import Config
22

33
config :injector, [
44
{:mnesia, MnesiaMock},
5+
{:pg2, Pg2Mock},
56
{Redex.Protocol, ProtocolMock}
67
]

lib/redex/command/subscribe.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
defmodule Redex.Command.SUBSCRIBE do
22
use Redex.Command
33

4+
import Injector
5+
6+
inject :pg2, as: Pg2
7+
48
def exec(channels, state) when channels != [] do
59
subscribe(channels, state)
610
end
@@ -10,16 +14,16 @@ defmodule Redex.Command.SUBSCRIBE do
1014
defp subscribe([], state), do: state
1115

1216
defp subscribe([ch | channels], state = %State{channels: subscribed}) do
13-
subscribed =
17+
state =
1418
if ch in subscribed do
15-
subscribed
19+
state
1620
else
17-
:pg2.create(ch)
18-
:pg2.join(ch, self())
19-
[ch | subscribed]
21+
Pg2.create(ch)
22+
Pg2.join(ch, self())
23+
%{state | channels: [ch | subscribed]}
2024
end
2125

22-
reply(["subscribe", ch, length(subscribed)], state)
26+
reply(["subscribe", ch, length(state.channels)], state)
2327
subscribe(channels, state)
2428
end
2529
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ defmodule Redex.MixProject do
3636
defp deps do
3737
[
3838
{:injector, "~> 0.2"},
39-
{:mox, "~> 0.5"},
4039
{:manifold, "~> 1.2"},
4140
{:ranch, "~> 1.7"},
4241
{:libcluster, "~> 3.1"},
4342
{:nimble_parsec, "~> 0.5.0"},
43+
{:mox, "~> 0.5", only: :test},
4444
{:excoveralls, "~> 0.11.1", only: :test},
4545
{:dialyxir, "~> 1.0.0-rc.6", only: :dev, runtime: false},
4646
{:stream_data, "~> 0.4", only: [:dev, :test]}

test/redex/command/quit_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Redex.Command.QuitTest do
1717
TransportMock
1818
|> expect(:close, fn :socket -> :ok end)
1919

20-
exec(args, state)
20+
assert state == exec(args, state)
2121
end
2222
end
2323
end

test/redex/command/subscribe_test.exs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule Redex.Command.SubscribeTest do
2+
use ExUnit.Case, async: true
3+
use ExUnitProperties
4+
5+
import Mox
6+
import Redex.DataGenerators
7+
import Redex.Command.SUBSCRIBE
8+
9+
setup :verify_on_exit!
10+
11+
property "SUBSCRIBE to some channels" do
12+
check all state <- state(channels: true),
13+
channels <- list_of(binary(), min_length: 1) do
14+
new_state =
15+
for ch <- channels, reduce: state do
16+
state = %{channels: channels} ->
17+
channels = if ch in channels do
18+
channels
19+
else
20+
Pg2Mock
21+
|> expect(:create, fn ^ch -> :ok end)
22+
|> expect(:join, fn ^ch, pid when pid == self() -> :ok end)
23+
24+
[ch | channels]
25+
end
26+
state = %{state | channels: channels}
27+
subscribed = length(channels)
28+
29+
ProtocolMock
30+
|> expect(:reply, fn ["subscribe", ^ch, ^subscribed], ^state -> state end)
31+
32+
state
33+
end
34+
35+
assert new_state == exec(channels, state)
36+
end
37+
end
38+
39+
test "SUBSCRIBE with wrong number of arguments" do
40+
error = {:error, "ERR wrong number of arguments for 'SUBSCRIBE' command"}
41+
42+
state = state()
43+
44+
ProtocolMock
45+
|> expect(:reply, fn ^error, ^state -> state end)
46+
47+
assert state == exec([], state)
48+
end
49+
end

test/support/data_generators.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ defmodule Redex.DataGenerators do
4848

4949
def state(opts \\ []) do
5050
quorum = if opts[:quorum], do: constant(opts[:quorum]), else: positive_integer()
51+
channels = if opts[:channels], do: uniq_list_of(binary(), min_length: 1), else: constant([])
5152

5253
gen all db <- integer(0..100),
53-
quorum <- quorum do
54+
quorum <- quorum,
55+
channels <- channels do
5456
%State{
5557
transport: TransportMock,
5658
socket: :socket,
5759
db: db,
58-
quorum: quorum
60+
quorum: quorum,
61+
channels: channels
5962
}
6063
end
6164
end

test/support/mocks.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Mox.defmock(TransportMock, for: Redex.TransportBehaviour)
22
Mox.defmock(MnesiaMock, for: Redex.MnesiaBehaviour)
3+
Mox.defmock(Pg2Mock, for: Redex.Pg2Behaviour)
34
Mox.defmock(ProtocolMock, for: Redex.Protocol)

test/support/pg2_behaviour.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule Redex.Pg2Behaviour do
2+
@callback create(any) :: :ok
3+
@callback join(any, pid) :: :ok
4+
end

0 commit comments

Comments
 (0)