1+ -module (state_SUITE ).
2+
3+ -include_lib (" common_test/include/ct.hrl" ).
4+
5+ % % Common Test
6+ -export ([
7+ all /0 ,
8+ init_per_suite /1 ,
9+ end_per_suite /1
10+ ]).
11+
12+ % % Test Cases
13+ -export ([
14+ t_create_state /1 ,
15+ t_state_ops /1 ,
16+ t_get_state_badarg_error /1
17+ ]).
18+
19+ -define (EXCLUDED_FUNS , [
20+ module_info ,
21+ all ,
22+ init_per_suite ,
23+ end_per_suite
24+ ]).
25+
26+ -include (" test_helper.hrl" ).
27+
28+ % %%===================================================================
29+ % %% Common Test
30+ % %%===================================================================
31+
32+ all () ->
33+ Exports = ? MODULE :module_info (exports ),
34+ [F || {F , _ } <- Exports , not lists :member (F , ? EXCLUDED_FUNS )].
35+
36+ init_per_suite (Config ) ->
37+ shards :start (),
38+ [{scope , l } | Config ].
39+
40+ end_per_suite (Config ) ->
41+ shards :stop (),
42+ Config .
43+
44+ % %%===================================================================
45+ % %% Tests Cases
46+ % %%===================================================================
47+
48+ t_create_state (_Config ) ->
49+ % create state with all default attr values
50+ State0 = shards_state :new (),
51+ shards_local = shards_state :module (State0 ),
52+ true = ? N_SHARDS == shards_state :n_shards (State0 ),
53+ true = fun shards_local :pick /3 == shards_state :pick_shard_fun (State0 ),
54+ true = fun shards_local :pick /3 == shards_state :pick_node_fun (State0 ),
55+
56+ % create state using shards_state:new/1
57+ State1 = shards_state :new (4 ),
58+ shards_local = shards_state :module (State1 ),
59+ true = 4 == shards_state :n_shards (State1 ),
60+ true = fun shards_local :pick /3 == shards_state :pick_shard_fun (State1 ),
61+ true = fun shards_local :pick /3 == shards_state :pick_node_fun (State1 ),
62+
63+ % create state using shards_state:new/2
64+ State2 = shards_state :new (2 , shards_dist ),
65+ shards_dist = shards_state :module (State2 ),
66+ true = 2 == shards_state :n_shards (State2 ),
67+ true = fun shards_local :pick /3 == shards_state :pick_shard_fun (State2 ),
68+ true = fun shards_local :pick /3 == shards_state :pick_node_fun (State2 ),
69+
70+ % create state using shards_state:new/3
71+ Fun = fun (X , Y , Z ) -> (X + Y + Z ) rem Y end ,
72+ State3 = shards_state :new (4 , shards_dist , Fun ),
73+ shards_dist = shards_state :module (State3 ),
74+ true = 4 == shards_state :n_shards (State3 ),
75+ true = Fun == shards_state :pick_shard_fun (State3 ),
76+ true = fun shards_local :pick /3 == shards_state :pick_node_fun (State3 ),
77+
78+ % create state using shards_state:new/4
79+ State4 = shards_state :new (4 , shards_dist , Fun , Fun ),
80+ shards_dist = shards_state :module (State4 ),
81+ true = 4 == shards_state :n_shards (State4 ),
82+ true = Fun == shards_state :pick_shard_fun (State4 ),
83+ true = Fun == shards_state :pick_node_fun (State4 ),
84+
85+ ok .
86+
87+ t_state_ops (_Config ) ->
88+ test_set = shards :new (test_set , [
89+ {pick_node_fun , fun test_helper :pick_node /3 }
90+ ]),
91+ StateSet = shards_state :get (test_set ),
92+ DefaultShards = ? N_SHARDS ,
93+ #{n_shards := DefaultShards } = shards_state :to_map (StateSet ),
94+
95+ Mod = shards_state :module (test_set ),
96+ true = Mod == shards_local orelse Mod == shards_dist ,
97+ DefaultShards = shards_state :n_shards (test_set ),
98+ Fun1 = fun shards_local :pick /3 ,
99+ Fun1 = shards_state :pick_shard_fun (test_set ),
100+ Fun2 = fun test_helper :pick_node /3 ,
101+ Fun2 = shards_state :pick_node_fun (test_set ),
102+
103+ State0 = shards_state :new (),
104+ State1 = shards_state :module (shards_dist , State0 ),
105+ State2 = shards_state :n_shards (100 , State1 ),
106+ Fun = fun (X , Y , Z ) -> (X + Y + Z ) rem Y end ,
107+ State3 = shards_state :pick_shard_fun (Fun , State2 ),
108+ State4 = shards_state :pick_node_fun (Fun , State3 ),
109+
110+ #{module := shards_dist ,
111+ n_shards := 100 ,
112+ pick_shard_fun := Fun ,
113+ pick_node_fun := Fun
114+ } = shards_state :to_map (State4 ),
115+
116+ ok .
117+
118+ t_get_state_badarg_error (_Config ) ->
119+ wrong_tab = ets :new (wrong_tab , [public , named_table ]),
120+ _ = try shards_state :get (wrong_tab )
121+ catch _ :{badarg , wrong_tab } -> ok
122+ end , ok .
0 commit comments