11% % -------------------------------------------------------------------
22% %
3- % % Copyright (c) 2018-2022 Workday, Inc.
3+ % % Copyright (c) 2018-2025 Workday, Inc.
44% %
55% % This file is provided to you under the Apache License,
66% % Version 2.0 (the "License"); you may not use this file
1919% % -------------------------------------------------------------------
2020-module (bitcask_lockops_tests ).
2121
22+ % % Warning: Without 'export_all', the first test will fail (that is, it
23+ % % succeeds in acquiring the lock on the second call). This happens even if
24+ % % the tests and external-vm functions are explicitly exported. Clearly
25+ % % something funky is going on in eunit.hrl.
26+ -compile ([export_all , nowarn_export_all ]).
27+
28+ % % Comment this out to see output from the spawned process.
29+ -define (NODEBUG , true ).
30+
2231-include_lib (" eunit/include/eunit.hrl" ).
2332-include (" bitcask.hrl" ).
2433
25- -compile ([export_all , nowarn_export_all ]).
26-
2734lock_cannot_be_obtained_on_already_locked_file_within_same_os_process_test () ->
28- Dir = bitcask : create_test_dir (),
35+ Dir = create_test_dir (? FUNCTION_NAME ),
2936 Filename = bitcask_lockops :lock_filename (write ,Dir ),
3037 ok = file_delete (Filename ),
3138 ok = file :write_file (Filename , " " ),
3239 {ok , _Lock } = bitcask_lockops :acquire (write , Dir ),
3340 ? assertMatch ({error , locked }, bitcask_lockops :acquire (write , Dir )).
3441
3542lock_can_be_obtained_on_already_locked_file_is_unlocked_test () ->
36- Dir = bitcask : create_test_dir (),
43+ Dir = create_test_dir (? FUNCTION_NAME ),
3744 Filename = bitcask_lockops :lock_filename (write ,Dir ),
3845 ok = file_delete (Filename ),
3946 ok = file :write_file (Filename , " " ),
@@ -42,17 +49,18 @@ lock_can_be_obtained_on_already_locked_file_is_unlocked_test() ->
4249 ? assertMatch ({ok , _ }, bitcask_lockops :acquire (write , Dir )).
4350
4451lock_cannot_be_obtained_on_already_locked_file_across_os_process_test () ->
45- DbDir = filename :absname (bitcask : create_test_dir ()),
52+ DbDir = filename :absname (create_test_dir (? FUNCTION_NAME )),
4653 % % start another erlang vm that will open the DB and obtain a write
4754 % % lock, then, when we try to obtain a write lock on the current vm
4855 % % it should fail.
56+ % % ToDo: Post OTP 24 use the 'peer' module, too much hassle before then.
4957 {? MODULE , _ , TBeam } = code :get_object_code (? MODULE ),
5058 {bitcask , _ , BBeam } = code :get_object_code (bitcask ),
5159 Tbin = filename :dirname (TBeam ),
5260 Bbin = filename :dirname (BBeam ),
5361 Erl = filename :join ([code :root_dir (), " bin" , " erl" ]),
5462 Cmd = lists :flatten ([
55- Erl , " -pa " , Bbin , " -pa " , Tbin , " -noinput -noshell -eval"
63+ " ' " , Erl , " ' -pa ' " , Bbin , " ' -pa ' " , Tbin , " ' -noinput -noshell -eval"
5664 " 'bitcask_lockops_tests:bitcask_locker_vm_main(\" " , DbDir , " \" ).'"
5765 ]),
5866 ? debugFmt (" ~n = COMMAND: ~ts " , [Cmd ]),
@@ -62,13 +70,13 @@ lock_cannot_be_obtained_on_already_locked_file_across_os_process_test() ->
6270 DB = bitcask :open (DbDir , [read_write ]),
6371 ? assertMatch ({error , {error , locked }}, bitcask :put (DB , <<" k" >>, <<" v" >>)),
6472 receive
65- {'DOWN' , MonRef , _Type , _Object , _Info } = Msg ->
66- ? debugFmt (" ~n = RECEIVE: ~0tp " , [Msg ])
73+ {'DOWN' , MonRef , _Type , _Object , _Info } = _Msg ->
74+ ? debugFmt (" ~n = RECEIVE: ~p~n " , [_Msg ])
6775 end .
6876
6977run_vm_process (Command ) ->
70- Output = ? cmd (Command ),
71- ? debugFmt (" ~n = OUTPUT ~0tp : ~ts " , [erlang : self (), Output ]).
78+ _Output = ? cmd (Command ),
79+ ? debugFmt (" ~n = CMD OUTPUT : ~s~n " , [_Output ]).
7280
7381% % entry function for another vm to lock a bitcask DB
7482bitcask_locker_vm_main (DbDir ) ->
@@ -91,3 +99,7 @@ file_delete(Filename) ->
9199 ok
92100 end .
93101
102+ -spec create_test_dir (Label :: atom () | string ()) -> string ().
103+ % % Creates a new, empty, directory for testing.
104+ create_test_dir (Label ) ->
105+ bitcask :setup_testfolder (io_lib :format (" bc.lockops.~s " , [Label ])).
0 commit comments