@@ -946,3 +946,86 @@ legacy_stats() ->
946946reset_legacy_checksum_stats () ->
947947 Counter = couch_stats :sample ([couchdb , legacy_checksums ]),
948948 couch_stats :decrement_counter ([couchdb , legacy_checksums ], Counter ).
949+
950+ write_header_sync_test_ () ->
951+ {
952+ " Test sync options for write_header" ,
953+ {
954+ setup ,
955+ fun test_util :start_couch /0 ,
956+ fun test_util :stop_couch /1 ,
957+ {
958+ foreach ,
959+ fun unlinked_setup /0 ,
960+ fun teardown /1 ,
961+ [
962+ ? TDEF_FE (should_handle_sync_option ),
963+ ? TDEF_FE (should_not_sync_by_default ),
964+ ? TDEF_FE (should_handle_error_of_the_first_sync ),
965+ ? TDEF_FE (should_handle_error_of_the_second_sync ),
966+ ? TDEF_FE (should_handle_error_of_the_file_write )
967+ ]
968+ }
969+ }
970+ }.
971+
972+ unlinked_setup () ->
973+ Self = self (),
974+ ReqId = make_ref (),
975+ meck :new (file , [passthrough , unstick ]),
976+ spawn (fun () ->
977+ {ok , Fd } = couch_file :open (? tempfile (), [create , overwrite ]),
978+ Self ! {ReqId , Fd }
979+ end ),
980+ receive
981+ {ReqId , Result } -> Result
982+ end .
983+
984+ should_handle_sync_option (Fd ) ->
985+ ok = couch_file :write_header (Fd , {<<" some_data" >>, 32 }, [sync ]),
986+ ? assertMatch ({ok , {<<" some_data" >>, 32 }}, couch_file :read_header (Fd )),
987+ ? assertEqual (2 , meck :num_calls (file , datasync , ['_' ])),
988+ ok .
989+
990+ should_not_sync_by_default (Fd ) ->
991+ ok = couch_file :write_header (Fd , {<<" some_data" >>, 32 }),
992+ ? assertMatch ({ok , {<<" some_data" >>, 32 }}, couch_file :read_header (Fd )),
993+ ? assertEqual (0 , meck :num_calls (file , datasync , ['_' ])),
994+ ok .
995+
996+ should_handle_error_of_the_first_sync (Fd ) ->
997+ meck :expect (
998+ file ,
999+ datasync ,
1000+ ['_' ],
1001+ meck :val ({error , terminated })
1002+ ),
1003+ ? assertEqual ({error , terminated }, couch_file :write_header (Fd , {<<" some_data" >>, 32 }, [sync ])),
1004+ ? assertEqual (1 , meck :num_calls (file , datasync , ['_' ])),
1005+ ok .
1006+
1007+ should_handle_error_of_the_second_sync (Fd ) ->
1008+ meck :expect (
1009+ file ,
1010+ datasync ,
1011+ ['_' ],
1012+ meck :seq ([
1013+ meck :val (ok ),
1014+ meck :val ({error , terminated })
1015+ ])
1016+ ),
1017+ ? assertEqual ({error , terminated }, couch_file :write_header (Fd , {<<" some_data" >>, 32 }, [sync ])),
1018+ ? assertEqual (2 , meck :num_calls (file , datasync , ['_' ])),
1019+ ok .
1020+
1021+ should_handle_error_of_the_file_write (Fd ) ->
1022+ meck :expect (
1023+ file ,
1024+ write ,
1025+ ['_' , '_' ],
1026+ meck :val ({error , terminated })
1027+ ),
1028+ ? assertEqual ({error , terminated }, couch_file :write_header (Fd , {<<" some_data" >>, 32 }, [sync ])),
1029+ ? assertEqual (1 , meck :num_calls (file , datasync , ['_' ])),
1030+ ? assertEqual (1 , meck :num_calls (file , write , ['_' , '_' ])),
1031+ ok .
0 commit comments