+
Skip to content

Replace meck with Mimic #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ otp_release:
- 20.0
- 21.0
script:
- mix test --no-start --trace --cover
- mix test --trace --cover
- MIX_ENV=test mix credo --strict
- if [[ `elixir -v` = *"1.7"* ]]; then mix format --check-formatted; fi
- cd test/integration
Expand Down
7 changes: 2 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ defmodule Verk.Mixfile do
end

def application do
[
applications: [:logger, :confex, :jason, :redix, :poolboy],
env: [node_id: "1", redis_url: "redis://127.0.0.1:6379"]
]
[env: [node_id: "1", redis_url: "redis://127.0.0.1:6379"]]
end

defp deps do
Expand All @@ -38,7 +35,7 @@ defmodule Verk.Mixfile do
{:earmark, "~> 1.0", only: :dev},
{:ex_doc, "== 0.18.4", only: :dev},
{:coverex, "== 1.4.13", only: :test},
{:meck, "~> 0.8.13", only: :test}
{:mimic, "~> 0.2", only: :test}
]
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"jason": {:hex, :jason, "1.0.0", "0f7cfa9bdb23fed721ec05419bcee2b2c21a77e926bce0deda029b5adc716fe2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"meck": {:hex, :meck, "0.8.13", "ffedb39f99b0b99703b8601c6f17c7f76313ee12de6b646e671e3188401f7866", [:rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mimic": {:hex, :mimic, "0.2.0", "66601fa5ce58db59e88fa0bef9d35d8169f11afcc3c8f0c77f911093f4469785", [:mix], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
Expand Down
59 changes: 27 additions & 32 deletions test/dead_set_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
defmodule Verk.DeadSetTest do
use ExUnit.Case
use ExUnit.Case, async: true
alias Verk.SortedSet
import Verk.DeadSet
import :meck
import Mimic

setup :verify_on_exit!

@key key()

setup do
{:ok, redis} = Confex.get_env(:verk, :redis_url) |> Redix.start_link()
Redix.command!(redis, ~w(DEL dead))
on_exit(fn -> unload() end)
{:ok, %{redis: redis}}
end

Expand All @@ -19,7 +22,7 @@ defmodule Verk.DeadSetTest do

assert add(job, failed_at, redis) == :ok

assert Redix.command!(redis, ["ZRANGE", key(), 0, -1, "WITHSCORES"]) == [
assert Redix.command!(redis, ["ZRANGE", @key, 0, -1, "WITHSCORES"]) == [
payload,
to_string(failed_at)
]
Expand All @@ -35,7 +38,7 @@ defmodule Verk.DeadSetTest do
assert add(job1, failed_at + 1, redis) == :ok
assert add(job2, failed_at + 2, redis) == :ok

assert Redix.command!(redis, ["ZRANGE", key(), 0, 2, "WITHSCORES"]) ==
assert Redix.command!(redis, ["ZRANGE", @key, 0, 2, "WITHSCORES"]) ==
[payload1, to_string(failed_at + 1), payload2, to_string(failed_at + 2)]
end

Expand All @@ -51,7 +54,7 @@ defmodule Verk.DeadSetTest do
assert add(job1, failed_at + 1, redis) == :ok
assert add(job2, failed_at + 2, redis) == :ok

assert Redix.command!(redis, ["ZRANGE", key(), 0, 2, "WITHSCORES"]) ==
assert Redix.command!(redis, ["ZRANGE", @key, 0, 2, "WITHSCORES"]) ==
[payload2, to_string(failed_at + 2)]

Application.put_env(:verk, :max_dead_jobs, 100)
Expand All @@ -66,7 +69,7 @@ defmodule Verk.DeadSetTest do

assert add!(job, failed_at, redis) == nil

assert Redix.command!(redis, ["ZRANGE", key(), 0, -1, "WITHSCORES"]) == [
assert Redix.command!(redis, ["ZRANGE", @key, 0, -1, "WITHSCORES"]) == [
payload,
to_string(failed_at)
]
Expand All @@ -82,44 +85,40 @@ defmodule Verk.DeadSetTest do
assert add!(job1, failed_at + 1, redis) == nil
assert add!(job2, failed_at + 2, redis) == nil

assert Redix.command!(redis, ["ZRANGE", key(), 0, 2, "WITHSCORES"]) ==
assert Redix.command!(redis, ["ZRANGE", @key, 0, 2, "WITHSCORES"]) ==
[payload1, to_string(failed_at + 1), payload2, to_string(failed_at + 2)]
end
end

describe "count/0" do
test "count" do
expect(SortedSet, :count, [key(), Verk.Redis], {:ok, 1})
expect(SortedSet, :count, fn @key, Verk.Redis -> {:ok, 1} end)

assert count() == {:ok, 1}
assert validate(SortedSet)
end
end

describe "count!/0" do
test "count!" do
expect(SortedSet, :count!, [key(), Verk.Redis], 1)
expect(SortedSet, :count!, fn @key, Verk.Redis -> 1 end)

assert count!() == 1
assert validate(SortedSet)
end
end

describe "clear/0" do
test "clear" do
expect(SortedSet, :clear, [key(), Verk.Redis], :ok)
expect(SortedSet, :clear, fn @key, Verk.Redis -> :ok end)

assert clear() == :ok
assert validate(SortedSet)
end
end

describe "clear!/0" do
test "clear!" do
expect(SortedSet, :clear!, [key(), Verk.Redis], nil)
expect(SortedSet, :clear!, fn @key, Verk.Redis -> nil end)

assert clear!() == nil
assert validate(SortedSet)
end
end

Expand All @@ -128,10 +127,11 @@ defmodule Verk.DeadSetTest do
job = %Verk.Job{class: "Class", args: []}
json = Verk.Job.encode!(job)

expect(SortedSet, :range, [key(), 0, -1, Verk.Redis], {:ok, [%{job | original_json: json}]})
expect(SortedSet, :range, fn @key, 0, -1, Verk.Redis ->
{:ok, [%{job | original_json: json}]}
end)

assert range() == {:ok, [%{job | original_json: json}]}
assert validate(SortedSet)
end
end

Expand All @@ -140,10 +140,11 @@ defmodule Verk.DeadSetTest do
job = %Verk.Job{class: "Class", args: []}
json = Verk.Job.encode!(job)

expect(SortedSet, :range, [key(), 1, 2, Verk.Redis], {:ok, [%{job | original_json: json}]})
expect(SortedSet, :range, fn @key, 1, 2, Verk.Redis ->
{:ok, [%{job | original_json: json}]}
end)

assert range(1, 2) == {:ok, [%{job | original_json: json}]}
assert validate(SortedSet)
end
end

Expand All @@ -152,10 +153,9 @@ defmodule Verk.DeadSetTest do
job = %Verk.Job{class: "Class", args: []}
json = Verk.Job.encode!(job)

expect(SortedSet, :range!, [key(), 0, -1, Verk.Redis], [%{job | original_json: json}])
expect(SortedSet, :range!, fn @key, 0, -1, Verk.Redis -> [%{job | original_json: json}] end)

assert range!() == [%{job | original_json: json}]
assert validate(SortedSet)
end
end

Expand All @@ -164,10 +164,9 @@ defmodule Verk.DeadSetTest do
job = %Verk.Job{class: "Class", args: []}
json = Verk.Job.encode!(job)

expect(SortedSet, :range!, [key(), 1, 2, Verk.Redis], [%{job | original_json: json}])
expect(SortedSet, :range!, fn @key, 1, 2, Verk.Redis -> [%{job | original_json: json}] end)

assert range!(1, 2) == [%{job | original_json: json}]
assert validate(SortedSet)
end
end

Expand All @@ -177,18 +176,16 @@ defmodule Verk.DeadSetTest do
json = Verk.Job.encode!(job)
job = %{job | original_json: json}

expect(SortedSet, :delete_job, [key(), json, Verk.Redis], :ok)
expect(SortedSet, :delete_job, fn @key, ^json, Verk.Redis -> :ok end)

assert delete_job(job) == :ok
assert validate(SortedSet)
end

test "with original_json" do
json = %Verk.Job{class: "Class", args: []} |> Verk.Job.encode!()
expect(SortedSet, :delete_job, [key(), json, Verk.Redis], :ok)
expect(SortedSet, :delete_job, fn @key, ^json, Verk.Redis -> :ok end)

assert delete_job(json) == :ok
assert validate(SortedSet)
end
end

Expand All @@ -198,18 +195,16 @@ defmodule Verk.DeadSetTest do
json = Verk.Job.encode!(job)
job = %{job | original_json: json}

expect(SortedSet, :delete_job!, [key(), json, Verk.Redis], nil)
expect(SortedSet, :delete_job!, fn @key, ^json, Verk.Redis -> nil end)

assert delete_job!(job) == nil
assert validate(SortedSet)
end

test "with original_json" do
json = %Verk.Job{class: "Class", args: []} |> Verk.Job.encode!()
expect(SortedSet, :delete_job!, [key(), json, Verk.Redis], nil)
expect(SortedSet, :delete_job!, fn @key, ^json, Verk.Redis -> nil end)

assert delete_job!(json) == nil
assert validate(SortedSet)
end
end
end
2 changes: 1 addition & 1 deletion test/dsl_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Verk.DslTest do
use ExUnit.Case
use ExUnit.Case, async: true
import Verk.Dsl

def incr(x) do
Expand Down
2 changes: 1 addition & 1 deletion test/event_producer_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Verk.EventProducerTest do
use ExUnit.Case
use ExUnit.Case, async: true
import Verk.EventProducer

describe "init/1" do
Expand Down
13 changes: 4 additions & 9 deletions test/in_progress_queue_test.exs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
defmodule Verk.InProgressQueueTest do
use ExUnit.Case
import :meck
import Mimic
alias Verk.InProgressQueue

setup do
new(Redix)
on_exit(fn -> unload() end)
:ok
end
setup :verify_on_exit!

describe "enqueue_in_progress/3" do
test "applies script to the correct keys" do
script_sha = Verk.Scripts.sha("lpop_rpush_src_dest")
key = "inprogress:queue_name:node_id"
commands = [:redis, ["EVALSHA", script_sha, 2, key, "queue:queue_name", 1000]]
commands = ["EVALSHA", script_sha, 2, key, "queue:queue_name", 1000]
result = {:ok, []}
expect(Redix, :command, commands, result)
expect(Redix, :command, fn :redis, ^commands -> result end)
assert InProgressQueue.enqueue_in_progress("queue_name", "node_id", :redis)
assert num_calls(Redix, :command, 2) == 1
end
end
end
2 changes: 1 addition & 1 deletion test/job_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Verk.JobTest do
use ExUnit.Case
use ExUnit.Case, async: true
alias Verk.Job

describe "encode!/1" do
Expand Down
2 changes: 1 addition & 1 deletion test/log_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Verk.LogTest do
use ExUnit.Case
use ExUnit.Case, async: true
import ExUnit.CaptureLog

alias Verk.Time
Expand Down
54 changes: 15 additions & 39 deletions test/manager_supervisor_test.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
defmodule Verk.ManagerSupervisorTest do
use ExUnit.Case
import :meck
use ExUnit.Case, async: true
import Mimic
import Verk.Manager.Supervisor

setup do
new(Supervisor)

on_exit(fn -> unload() end)

:ok
end
setup :verify_on_exit!

describe "init/1" do
test "defines tree" do
Expand All @@ -29,67 +23,49 @@ defmodule Verk.ManagerSupervisorTest do
{:"test_queue.supervisor", {Verk.Queue.Supervisor, :start_link, [:test_queue, 30]},
:permanent, :infinity, :supervisor, [Verk.Queue.Supervisor]}

expect(Supervisor, :start_child, [Verk.Manager.Supervisor, child], :ok)
expect(Supervisor, :start_child, fn Verk.Manager.Supervisor, ^child -> :ok end)

assert start_child(queue, 30) == :ok

assert validate(Supervisor)
end
end

describe "stop_child/1" do
test "a queue successfully" do
queue = :test_queue

expect(
Supervisor,
:terminate_child,
[Verk.Manager.Supervisor, :"test_queue.supervisor"],
expect(Supervisor, :terminate_child, fn Verk.Manager.Supervisor, :"test_queue.supervisor" ->
:ok
)
end)

expect(Supervisor, :delete_child, [Verk.Manager.Supervisor, :"test_queue.supervisor"], :ok)
expect(Supervisor, :delete_child, fn Verk.Manager.Supervisor, :"test_queue.supervisor" ->
:ok
end)

assert stop_child(queue) == :ok

assert validate(Supervisor)
end

test "a queue unsuccessfully terminating child" do
queue = :test_queue

expect(
Supervisor,
:terminate_child,
[Verk.Manager.Supervisor, :"test_queue.supervisor"],
expect(Supervisor, :terminate_child, fn Verk.Manager.Supervisor, :"test_queue.supervisor" ->
{:error, :not_found}
)
end)

assert stop_child(queue) == {:error, :not_found}

assert validate(Supervisor)
end

test "a queue unsuccessfully deleting child" do
queue = :test_queue

expect(
Supervisor,
:terminate_child,
[Verk.Manager.Supervisor, :"test_queue.supervisor"],
expect(Supervisor, :terminate_child, fn Verk.Manager.Supervisor, :"test_queue.supervisor" ->
:ok
)
end)

expect(
Supervisor,
:delete_child,
[Verk.Manager.Supervisor, :"test_queue.supervisor"],
expect(Supervisor, :delete_child, fn Verk.Manager.Supervisor, :"test_queue.supervisor" ->
{:error, :not_found}
)
end)

assert stop_child(queue) == {:error, :not_found}

assert validate(Supervisor)
end
end
end
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载