From 1df8755f44dd82edd79fc542a114c4f3a6314868 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Wed, 29 Jan 2014 18:22:07 -0800 Subject: [PATCH 1/6] Start "canonicalizing" unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Factored out the first check from all_test_/0. That tests that startup parameters passed to mcd_cluster:start_link/2 are honored. Therefore: • Made the test explicit; it is now a separate tests. • Nonessential details (such as result of the start/stop calls) are not checked, since if they fail the test will fail anyways. • Factored out mcd_node/1 to hide nonessential details (how the node is specified). All we test is that the node id is visible after startup, so it's left there. --- test/mcd_cluster_tests.erl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index 75d6ece..9738c57 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -5,6 +5,17 @@ -define(NAME, mb_test). -define(setup(F), {setup, fun setup/0, fun cleanup/1, F}). +% XXX: turn off 'INFO' output + +mcd_node(NodeId) -> + {NodeId, ["localhost", 2222], 10}. + +check_using_startup_args_test() -> + NodeId = localhost, + mcd_cluster:start_link(?NAME, [mcd_node(NodeId)]), + Nodes = mcd_cluster:nodes(?NAME), + ?assertMatch([{NodeId, _}], Nodes), + mcd_cluster:stop(?NAME). all_test_() -> [{"Check MCD cluster", From d565badf9cfcbac109e32f6b5b193d02a424e546 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Mon, 10 Feb 2014 17:37:17 -0800 Subject: [PATCH 2/6] Better name for a test function check_using_startup_args_test -> using_startup_args_test --- test/mcd_cluster_tests.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index 9738c57..07b19b4 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -6,11 +6,12 @@ -define(setup(F), {setup, fun setup/0, fun cleanup/1, F}). % XXX: turn off 'INFO' output +% XXX: mock memcached mcd_node(NodeId) -> {NodeId, ["localhost", 2222], 10}. -check_using_startup_args_test() -> +using_startup_args_test() -> NodeId = localhost, mcd_cluster:start_link(?NAME, [mcd_node(NodeId)]), Nodes = mcd_cluster:nodes(?NAME), From 411377a9b8021628226a2e75c63d1a6ff05738e6 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Mon, 10 Feb 2014 23:17:07 -0800 Subject: [PATCH 3/6] Add comments --- test/mcd_cluster_tests.erl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index 07b19b4..f43bc8d 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -7,6 +7,10 @@ % XXX: turn off 'INFO' output % XXX: mock memcached +% XXX: add check that adding nodes preserves their order, e.g. if we add +% a, c and then b we will get back [a, c, b], not [a, b, c] + +%%% Test helpers %%% mcd_node(NodeId) -> {NodeId, ["localhost", 2222], 10}. @@ -20,7 +24,7 @@ using_startup_args_test() -> all_test_() -> [{"Check MCD cluster", - ?setup(fun() -> [check_node_(), + ?setup(fun() -> [check_node_(), % using_startup_args_test add_node_(), check_node_2_(), add_node_dup_(), From 8ee7b695f518003556cc4727e0141d7cebc43255 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Mon, 10 Feb 2014 23:23:33 -0800 Subject: [PATCH 4/6] Put using_startup_args into a fixture --- test/mcd_cluster_tests.erl | 41 ++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index f43bc8d..e381e6e 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -15,12 +15,41 @@ mcd_node(NodeId) -> {NodeId, ["localhost", 2222], 10}. -using_startup_args_test() -> - NodeId = localhost, - mcd_cluster:start_link(?NAME, [mcd_node(NodeId)]), - Nodes = mcd_cluster:nodes(?NAME), - ?assertMatch([{NodeId, _}], Nodes), - mcd_cluster:stop(?NAME). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +test_XXX_test_() -> + [ + { + foreach, + + % setup + fun() -> + NodeId = localhost, + {ok, _Pid} = mcd_cluster:start_link(?NAME, [mcd_node(NodeId)]), + NodeId + end, + + % cleanup + fun(_NodeId) -> + mcd_cluster:stop(?NAME) + end, + + % instantiators + [ + fun(NodeId) -> + { + % XXX: this should really be tested outside of the fixture + "using startup parameters", + fun() -> + Nodes = mcd_cluster:nodes(?NAME), + ?assertMatch([{NodeId, _}], Nodes) + end + } + end + ] + } + ]. all_test_() -> [{"Check MCD cluster", From 3f66fa47248d7080128e62155558cd548163ef46 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Mon, 10 Feb 2014 23:40:05 -0800 Subject: [PATCH 5/6] New test for mcd_cluster:add/2 --- test/mcd_cluster_tests.erl | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index e381e6e..8471012 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -15,6 +15,9 @@ mcd_node(NodeId) -> {NodeId, ["localhost", 2222], 10}. +a_node_id() -> + % XXX: return unique node ids each time + some_node_id. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -25,25 +28,37 @@ test_XXX_test_() -> % setup fun() -> - NodeId = localhost, - {ok, _Pid} = mcd_cluster:start_link(?NAME, [mcd_node(NodeId)]), - NodeId + FirstNodeId = localhost, + {ok, _Pid} = mcd_cluster:start_link(?NAME, [mcd_node(FirstNodeId)]), + FirstNodeId end, % cleanup - fun(_NodeId) -> + fun(_FirstNodeId) -> mcd_cluster:stop(?NAME) end, % instantiators [ - fun(NodeId) -> + fun(FirstNodeId) -> { % XXX: this should really be tested outside of the fixture "using startup parameters", fun() -> Nodes = mcd_cluster:nodes(?NAME), - ?assertMatch([{NodeId, _}], Nodes) + ?assertMatch([{FirstNodeId, _}], Nodes) + end + } + end, + + fun(FirstNodeId) -> + { + "Add node", + fun() -> + SecondNodeId = a_node_id(), + ok = mcd_cluster:add(?NAME, mcd_node(SecondNodeId)), + Nodes = mcd_cluster:nodes(?NAME), + ?assertMatch([{FirstNodeId, _}, {SecondNodeId, _}], Nodes) end } end @@ -55,7 +70,7 @@ all_test_() -> [{"Check MCD cluster", ?setup(fun() -> [check_node_(), % using_startup_args_test add_node_(), - check_node_2_(), + check_node_2_(), % add_node_test add_node_dup_(), check_node_2_(), del_node_(), From bc91155c9980cd25250ae3608fb64f435b067a97 Mon Sep 17 00:00:00 2001 From: Vlad Skvortsov Date: Mon, 10 Feb 2014 23:50:57 -0800 Subject: [PATCH 6/6] Test for addition of duplicate node --- test/mcd_cluster_tests.erl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/mcd_cluster_tests.erl b/test/mcd_cluster_tests.erl index 8471012..0a46c4a 100644 --- a/test/mcd_cluster_tests.erl +++ b/test/mcd_cluster_tests.erl @@ -61,6 +61,16 @@ test_XXX_test_() -> ?assertMatch([{FirstNodeId, _}, {SecondNodeId, _}], Nodes) end } + end, + + fun(FirstNodeId) -> + { + "Adding duplicate node", + fun() -> + Result = mcd_cluster:add(?NAME, mcd_node(FirstNodeId)), + ?assertEqual({error, already_there, [FirstNodeId]}, Result) + end + } end ] } @@ -71,7 +81,7 @@ all_test_() -> ?setup(fun() -> [check_node_(), % using_startup_args_test add_node_(), check_node_2_(), % add_node_test - add_node_dup_(), + add_node_dup_(), % done check_node_2_(), del_node_(), check_node_(),