Skip to content

Commit c5f6307

Browse files
committed
Fix and improve couch_btree testing
Previously, it turns out, we didn't actually test the reverse sorted keys. Instead we just renamed the `Reversed = Sorted` and then tested the `Sorted` case. But it's even worse than that because in order to "force" the test to pass we had effectively disable the check so make it count element whether they were there or not (Count + 1) in this snippet: ```diff FoldFun = fun(Element, {[HAcc | TAcc], Count}) -> case Element == HAcc of true -> {ok, {TAcc, Count + 1}}; - _ -> {ok, {TAcc, Count + 1}} + _ -> {ok, {TAcc, Count}} end end, ``` Luckily, it seems we survived with the code being correct as far as fixing the test didn't reveal any actual btree logic errors. In addition, we were missing tests trying out various chunk sizes, so added two extra cases: one with much smaller chunks, and another with much larger chunks.
1 parent 08cb7e8 commit c5f6307

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

src/couch/test/eunit/couch_btree_tests.erl

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ setup_kvs_with_cache(_) ->
3838
]),
3939
{Fd, Btree}.
4040

41+
setup_kvs_with_small_chunk_size(_) ->
42+
% Less than a 1/4 than current default 1279
43+
config:set("couchdb", "btree_chunk_size", "300", false),
44+
{ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
45+
{ok, Btree} = couch_btree:open(nil, Fd, [
46+
{compression, none},
47+
{reduce, fun reduce_fun/2}
48+
]),
49+
{Fd, Btree}.
50+
51+
setup_kvs_with_large_chunk_size(_) ->
52+
% About 4x than current default 1279
53+
config:set("couchdb", "btree_chunk_size", "5000", false),
54+
{ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
55+
{ok, Btree} = couch_btree:open(nil, Fd, [
56+
{compression, none},
57+
{reduce, fun reduce_fun/2}
58+
]),
59+
{Fd, Btree}.
60+
4161
setup_red() ->
4262
{_, EvenOddKVs} = lists:foldl(
4363
fun(Idx, {Key, Acc}) ->
@@ -114,7 +134,7 @@ sorted_kvs_test_() ->
114134
rsorted_kvs_test_() ->
115135
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
116136
Funs = kvs_test_funs(),
117-
Reversed = Sorted,
137+
Reversed = lists:reverse(Sorted),
118138
{
119139
"BTree with backward sorted keys",
120140
{
@@ -167,6 +187,42 @@ sorted_kvs_with_cache_test_() ->
167187
}
168188
}.
169189

190+
sorted_kvs_small_chunk_size_test_() ->
191+
Funs = kvs_test_funs(),
192+
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
193+
{
194+
"BTree with a small chunk size and sorted keys",
195+
{
196+
setup,
197+
fun() -> test_util:start_couch() end,
198+
fun test_util:stop/1,
199+
{
200+
foreachx,
201+
fun setup_kvs_with_small_chunk_size/1,
202+
fun teardown/2,
203+
[{Sorted, Fun} || Fun <- Funs]
204+
}
205+
}
206+
}.
207+
208+
sorted_kvs_large_chunk_size_test_() ->
209+
Funs = kvs_test_funs(),
210+
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
211+
{
212+
"BTree with a large chunk size and sorted keys",
213+
{
214+
setup,
215+
fun() -> test_util:start_couch() end,
216+
fun test_util:stop/1,
217+
{
218+
foreachx,
219+
fun setup_kvs_with_large_chunk_size/1,
220+
fun teardown/2,
221+
[{Sorted, Fun} || Fun <- Funs]
222+
}
223+
}
224+
}.
225+
170226
reductions_test_() ->
171227
{
172228
"BTree reductions",
@@ -642,18 +698,15 @@ test_key_access(Btree, List) ->
642698
FoldFun = fun(Element, {[HAcc | TAcc], Count}) ->
643699
case Element == HAcc of
644700
true -> {ok, {TAcc, Count + 1}};
645-
_ -> {ok, {TAcc, Count + 1}}
701+
_ -> {ok, {TAcc, Count}}
646702
end
647703
end,
648704
Length = length(List),
649705
Sorted = lists:sort(List),
650706
{ok, _, {[], Length}} = couch_btree:foldl(Btree, FoldFun, {Sorted, 0}),
651-
{ok, _, {[], Length}} = couch_btree:fold(
652-
Btree,
653-
FoldFun,
654-
{Sorted, 0},
655-
[{dir, rev}]
656-
),
707+
Reversed = lists:reverse(Sorted),
708+
RevOpts = [{dir, rev}],
709+
{ok, _, {[], Length}} = couch_btree:fold(Btree, FoldFun, {Reversed, 0}, RevOpts),
657710
ok.
658711

659712
test_lookup_access(Btree, KeyValues) ->

0 commit comments

Comments
 (0)