22-compile (export_all ).
33-include_lib (" eunit/include/eunit.hrl" ).
44
5+ -define (SETUP_ENV , application :set_env (riak_repl , rtq_max_bytes , 10 * 1024 * 1024 )).
6+ -define (CLEAN_ENV , application :unset_env (riak_repl , rtq_max_bytes )).
7+
58rtq_trim_test () ->
69 % % make sure the queue is 10mb
7- application : set_env ( riak_repl , rtq_max_bytes , 10 * 1024 * 1024 ) ,
10+ ? SETUP_ENV ,
811 {ok , Pid } = riak_repl2_rtq :start_test (),
912 try
1013 gen_server :call (Pid , {register , rtq_test }),
@@ -19,7 +22,7 @@ rtq_trim_test() ->
1922 % % the queue is now empty
2023 ? assert (gen_server :call (Pid , {is_empty , rtq_test }))
2124 after
22- application : unset_env ( riak_repl , rtq_max_bytes ) ,
25+ ? CLEAN_ENV ,
2326 exit (Pid , kill )
2427 end .
2528
@@ -45,12 +48,12 @@ accumulate(Pid, Acc, C) ->
4548
4649status_test_ () ->
4750 {setup , fun () ->
48- application : set_env ( riak_repl , rtq_max_bytes , 10 * 1024 * 1024 ) ,
51+ ? SETUP_ENV ,
4952 {ok , QPid } = riak_repl2_rtq :start_link (),
5053 QPid
5154 end ,
5255 fun (QPid ) ->
53- application : unset_env ( riak_repl , rtq_max_bytes ) ,
56+ ? CLEAN_ENV ,
5457 riak_repl_test_util :kill_and_wait (QPid )
5558 end ,
5659 fun (_QPid ) -> [
@@ -68,6 +71,64 @@ status_test_() ->
6871
6972 ] end }.
7073
74+ summarize_test_ () ->
75+ {setup ,
76+ fun start_rtq /0 ,
77+ fun kill_rtq /1 ,
78+ fun (_QPid ) -> [
79+ {" includes sequence number, object ID, and size" ,
80+ fun () ->
81+ Objects = push_objects (<<" BucketsOfRain" >>, [<<" obj1" >>, <<" obj2" >>]),
82+ Summarized = riak_repl2_rtq :summarize (),
83+ Zipped = lists :zip (Objects , Summarized ),
84+ lists :foreach (
85+ fun ({Obj , Summary }) ->
86+ {Seq , _ , _ } = Summary ,
87+ ExpectedSummary = {Seq , riak_object :key (Obj ), get_approximate_size (Obj )},
88+ ? assertMatch (ExpectedSummary , Summary )
89+ end ,
90+ Zipped )
91+ end
92+ }
93+ ]
94+ end
95+ }.
96+
97+ evict_test_ () ->
98+ {foreach ,
99+ fun start_rtq /0 ,
100+ fun kill_rtq /1 ,
101+ [
102+ fun (_QPid ) ->
103+ {" evicts object by sequence if present" ,
104+ fun () ->
105+ Objects = push_objects (<<" TwoPeasInABucket" >>, [<<" obj1" >>, <<" obj2" >>]),
106+ [KeyToEvict , RemainingKey ] = [riak_object :key (O ) || O <- Objects ],
107+ [{SeqToEvict , KeyToEvict , _ }, {RemainingSeq , RemainingKey , _ }] = riak_repl2_rtq :summarize (),
108+ ok = riak_repl2_rtq :evict (SeqToEvict ),
109+ ? assertMatch ([{RemainingSeq , RemainingKey , _ }], riak_repl2_rtq :summarize ()),
110+ ok = riak_repl2_rtq :evict (RemainingSeq + 1 ),
111+ ? assertMatch ([{RemainingSeq , RemainingKey , _ }], riak_repl2_rtq :summarize ())
112+ end
113+ }
114+ end ,
115+ fun (_QPid ) ->
116+ {" evicts object by sequence if present and key matches" ,
117+ fun () ->
118+ Objects = push_objects (<<" TwoPeasInABucket" >>, [<<" obj1" >>, <<" obj2" >>]),
119+ [KeyToEvict , RemainingKey ] = [riak_object :key (O ) || O <- Objects ],
120+ [{SeqToEvict , KeyToEvict , _ }, {RemainingSeq , RemainingKey , _ }] = riak_repl2_rtq :summarize (),
121+ ? assertMatch ({wrong_key , _ , _ }, riak_repl2_rtq :evict (SeqToEvict , RemainingKey )),
122+ ? assertMatch ({not_found , _ }, riak_repl2_rtq :evict (RemainingSeq + 1 , RemainingKey )),
123+ ? assertEqual (2 , length (riak_repl2_rtq :summarize ())),
124+ ok = riak_repl2_rtq :evict (SeqToEvict , KeyToEvict ),
125+ ? assertMatch ([{RemainingSeq , RemainingKey , _ }], riak_repl2_rtq :summarize ())
126+ end
127+ }
128+ end
129+ ]
130+ }.
131+
71132overload_protection_start_test_ () ->
72133 [
73134 {" able to start after a crash without ets errors" , fun () ->
@@ -213,6 +274,28 @@ overload_test_() ->
213274
214275 ]}.
215276
277+ start_rtq () ->
278+ ? SETUP_ENV ,
279+ {ok , Pid } = riak_repl2_rtq :start_link (),
280+ gen_server :call (Pid , {register , rtq_test }),
281+ Pid .
282+
283+ kill_rtq (QPid ) ->
284+ ? CLEAN_ENV ,
285+ riak_repl_test_util :kill_and_wait (QPid ).
286+
287+ object_format () -> riak_core_capability :get ({riak_kv , object_format }, v0 ).
288+
289+ get_approximate_size (O ) -> riak_object :approximate_size (object_format (), O ).
290+
291+ push_objects (Bucket , Keys ) -> [push_object (Bucket , O ) || O <- Keys ].
292+
293+ push_object (Bucket , Key ) ->
294+ RandomData = crypto :rand_bytes (1024 * 1024 ),
295+ Obj = riak_object :new (Bucket , Key , RandomData ),
296+ riak_repl2_rtq :push (1 , Obj ),
297+ Obj .
298+
216299pull (N ) ->
217300 lists :foldl (fun (_Nth , _LastSeq ) ->
218301 pull ()
0 commit comments