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

iilyak pushed a commit to branch introduce-commit_header-2
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit e357858be1a6e40b2c58bb0e88ce127a2890effb
Author: ILYA Khlopotov <[email protected]>
AuthorDate: Tue Jun 24 13:49:50 2025 -0700

    Add tests for write_header/3 with [sync]
---
 src/couch/test/eunit/couch_file_tests.erl | 68 +++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/src/couch/test/eunit/couch_file_tests.erl 
b/src/couch/test/eunit/couch_file_tests.erl
index 5b7481636..505878f2a 100644
--- a/src/couch/test/eunit/couch_file_tests.erl
+++ b/src/couch/test/eunit/couch_file_tests.erl
@@ -946,3 +946,71 @@ legacy_stats() ->
 reset_legacy_checksum_stats() ->
     Counter = couch_stats:sample([couchdb, legacy_checksums]),
     couch_stats:decrement_counter([couchdb, legacy_checksums], Counter).
+
+write_header_sync_test_() ->
+    {
+        "Test sync options for write_header",
+        {
+            setup,
+            fun test_util:start_couch/0,
+            fun test_util:stop_couch/1,
+            {
+                foreach,
+                fun unlinked_setup/0,
+                fun teardown/1,
+                [
+                    ?TDEF_FE(should_handle_sync_option),
+                    ?TDEF_FE(should_not_sync_by_default),
+                    ?TDEF_FE(should_handle_error_of_the_fist_sync),
+                    ?TDEF_FE(should_handle_error_of_the_second_sync),
+                    ?TDEF_FE(should_handle_error_of_the_file_write)
+                ]
+            }
+        }
+    }.
+
+unlinked_setup() ->
+    Self = self(),
+    ReqId = make_ref(),
+    meck:new(file, [passthrough, unstick]),
+    spawn(fun() ->
+        {ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
+        Self ! {ReqId, Fd}
+    end),
+    receive
+        {ReqId, Result} -> Result
+    end.
+
+should_handle_sync_option(Fd) ->
+    ok = couch_file:write_header(Fd, {<<"some_data">>, 32}, [sync]),
+    ?assertMatch({ok, {<<"some_data">>, 32}}, couch_file:read_header(Fd)),
+    ?assertEqual(2, meck:num_calls(file, datasync, ['_'])),
+    ok.
+
+should_not_sync_by_default(Fd) ->
+    ok = couch_file:write_header(Fd, {<<"some_data">>, 32}),
+    ?assertMatch({ok, {<<"some_data">>, 32}}, couch_file:read_header(Fd)),
+    ?assertEqual(0, meck:num_calls(file, datasync, ['_'])),
+    ok.
+
+should_handle_error_of_the_fist_sync(Fd) ->
+    meck:expect(file, datasync, ['_'],
+        meck:val({error, terminated})
+    ),
+    ?assertEqual({error, terminated}, couch_file:write_header(Fd, 
{<<"some_data">>, 32}, [sync])),
+    ok.
+
+should_handle_error_of_the_second_sync(Fd) ->
+    meck:expect(file, datasync, ['_'], meck:seq([
+        meck:val(ok),
+        meck:val({error, terminated})
+    ])),
+    ?assertEqual({error, terminated}, couch_file:write_header(Fd, 
{<<"some_data">>, 32}, [sync])),
+    ok.
+
+should_handle_error_of_the_file_write(Fd) ->
+    meck:expect(file, write, ['_', '_'],
+        meck:val({error, terminated})
+    ),
+    ?assertEqual({error, terminated}, couch_file:write_header(Fd, 
{<<"some_data">>, 32}, [sync])),
+    ok.

Reply via email to