You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2025/08/28 21:54:09 UTC

(couchdb) 02/02: Fix and improve couch_btree testing

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch improve-btree-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 100c30795581af0fdac4c69f0ca2792c956d2682
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Thu Aug 28 17:44:55 2025 -0400

    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.
---
 src/couch/test/eunit/couch_btree_tests.erl | 69 ++++++++++++++++++++++++++----
 1 file changed, 61 insertions(+), 8 deletions(-)

diff --git a/src/couch/test/eunit/couch_btree_tests.erl b/src/couch/test/eunit/couch_btree_tests.erl
index 6a5f9a791..f365a37cf 100644
--- a/src/couch/test/eunit/couch_btree_tests.erl
+++ b/src/couch/test/eunit/couch_btree_tests.erl
@@ -38,6 +38,26 @@ setup_kvs_with_cache(_) ->
     ]),
     {Fd, Btree}.
 
+setup_kvs_with_small_chunk_size(_) ->
+    % Less than a 1/4 than current default 1279
+    config:set("couchdb", "btree_chunk_size", "300", false),
+    {ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
+    {ok, Btree} = couch_btree:open(nil, Fd, [
+        {compression, none},
+        {reduce, fun reduce_fun/2}
+    ]),
+    {Fd, Btree}.
+
+setup_kvs_with_large_chunk_size(_) ->
+    % About 4x than current default 1279
+    config:set("couchdb", "btree_chunk_size", "5000", false),
+    {ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
+    {ok, Btree} = couch_btree:open(nil, Fd, [
+        {compression, none},
+        {reduce, fun reduce_fun/2}
+    ]),
+    {Fd, Btree}.
+
 setup_red() ->
     {_, EvenOddKVs} = lists:foldl(
         fun(Idx, {Key, Acc}) ->
@@ -114,7 +134,7 @@ sorted_kvs_test_() ->
 rsorted_kvs_test_() ->
     Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
     Funs = kvs_test_funs(),
-    Reversed = Sorted,
+    Reversed = lists:reverse(Sorted),
     {
         "BTree with backward sorted keys",
         {
@@ -167,6 +187,42 @@ sorted_kvs_with_cache_test_() ->
         }
     }.
 
+sorted_kvs_small_chunk_size_test_() ->
+    Funs = kvs_test_funs(),
+    Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
+    {
+        "BTree with a small chunk size and sorted keys",
+        {
+            setup,
+            fun() -> test_util:start_couch() end,
+            fun test_util:stop/1,
+            {
+                foreachx,
+                fun setup_kvs_with_small_chunk_size/1,
+                fun teardown/2,
+                [{Sorted, Fun} || Fun <- Funs]
+            }
+        }
+    }.
+
+sorted_kvs_large_chunk_size_test_() ->
+    Funs = kvs_test_funs(),
+    Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
+    {
+        "BTree with a large chunk size and sorted keys",
+        {
+            setup,
+            fun() -> test_util:start_couch() end,
+            fun test_util:stop/1,
+            {
+                foreachx,
+                fun setup_kvs_with_large_chunk_size/1,
+                fun teardown/2,
+                [{Sorted, Fun} || Fun <- Funs]
+            }
+        }
+    }.
+
 reductions_test_() ->
     {
         "BTree reductions",
@@ -642,18 +698,15 @@ test_key_access(Btree, List) ->
     FoldFun = fun(Element, {[HAcc | TAcc], Count}) ->
         case Element == HAcc of
             true -> {ok, {TAcc, Count + 1}};
-            _ -> {ok, {TAcc, Count + 1}}
+            _ -> {ok, {TAcc, Count}}
         end
     end,
     Length = length(List),
     Sorted = lists:sort(List),
     {ok, _, {[], Length}} = couch_btree:foldl(Btree, FoldFun, {Sorted, 0}),
-    {ok, _, {[], Length}} = couch_btree:fold(
-        Btree,
-        FoldFun,
-        {Sorted, 0},
-        [{dir, rev}]
-    ),
+    Reversed = lists:reverse(Sorted),
+    RevOpts = [{dir, rev}],
+    {ok, _, {[], Length}} = couch_btree:fold(Btree, FoldFun, {Reversed, 0}, RevOpts),
     ok.
 
 test_lookup_access(Btree, KeyValues) ->