@@ -1106,31 +1106,37 @@ BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
11061106 BOOST_CHECK (!ParseFixedPoint (" 31.999999999999999999999" , 3 , &amount));
11071107}
11081108
1109- static void TestOtherThread (fs::path dirname, fs::path lockname, bool *result)
1110- {
1111- *result = LockDirectory (dirname, lockname);
1112- }
1113-
11141109#ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork()
11151110static constexpr char LockCommand = ' L' ;
11161111static constexpr char UnlockCommand = ' U' ;
11171112static constexpr char ExitCommand = ' X' ;
1113+ enum : char {
1114+ ResSuccess = 2 , // Start with 2 to avoid accidental collision with common values 0 and 1
1115+ ResErrorLock,
1116+ ResUnlockSuccess,
1117+ };
11181118
11191119[[noreturn]] static void TestOtherProcess (fs::path dirname, fs::path lockname, int fd)
11201120{
11211121 char ch;
11221122 while (true ) {
11231123 int rv = read (fd, &ch, 1 ); // Wait for command
11241124 assert (rv == 1 );
1125- switch (ch) {
1125+ switch (ch) {
11261126 case LockCommand:
1127- ch = LockDirectory (dirname, lockname);
1127+ ch = [&] {
1128+ switch (util::LockDirectory (dirname, lockname)) {
1129+ case util::LockResult::Success: return ResSuccess;
1130+ case util::LockResult::ErrorLock: return ResErrorLock;
1131+ } // no default case, so the compiler can warn about missing cases
1132+ assert (false );
1133+ }();
11281134 rv = write (fd, &ch, 1 );
11291135 assert (rv == 1 );
11301136 break ;
11311137 case UnlockCommand:
11321138 ReleaseDirectoryLocks ();
1133- ch = true ; // Always succeeds
1139+ ch = ResUnlockSuccess ; // Always succeeds
11341140 rv = write (fd, &ch, 1 );
11351141 assert (rv == 1 );
11361142 break ;
@@ -1167,51 +1173,51 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
11671173 BOOST_CHECK_EQUAL (close (fd[0 ]), 0 ); // Parent: close child end
11681174#endif
11691175 // Lock on non-existent directory should fail
1170- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname), false );
1176+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname), util::LockResult::ErrorLock );
11711177
11721178 fs::create_directories (dirname);
11731179
11741180 // Probing lock on new directory should succeed
1175- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname, true ), true );
1181+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname, true ), util::LockResult::Success );
11761182
11771183 // Persistent lock on new directory should succeed
1178- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname), true );
1184+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname), util::LockResult::Success );
11791185
11801186 // Another lock on the directory from the same thread should succeed
1181- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname), true );
1187+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname), util::LockResult::Success );
11821188
11831189 // Another lock on the directory from a different thread within the same process should succeed
1184- bool threadresult;
1185- std::thread thr (TestOtherThread, dirname, lockname, &threadresult );
1190+ util::LockResult threadresult;
1191+ std::thread thr ([&] { threadresult = util::LockDirectory ( dirname, lockname); } );
11861192 thr.join ();
1187- BOOST_CHECK_EQUAL (threadresult, true );
1193+ BOOST_CHECK_EQUAL (threadresult, util::LockResult::Success );
11881194#ifndef WIN32
11891195 // Try to acquire lock in child process while we're holding it, this should fail.
11901196 char ch;
11911197 BOOST_CHECK_EQUAL (write (fd[1 ], &LockCommand, 1 ), 1 );
11921198 BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1193- BOOST_CHECK_EQUAL (( bool ) ch, false );
1199+ BOOST_CHECK_EQUAL (ch, ResErrorLock );
11941200
11951201 // Give up our lock
11961202 ReleaseDirectoryLocks ();
11971203 // Probing lock from our side now should succeed, but not hold on to the lock.
1198- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname, true ), true );
1204+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname, true ), util::LockResult::Success );
11991205
12001206 // Try to acquire the lock in the child process, this should be successful.
12011207 BOOST_CHECK_EQUAL (write (fd[1 ], &LockCommand, 1 ), 1 );
12021208 BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1203- BOOST_CHECK_EQUAL (( bool ) ch, true );
1209+ BOOST_CHECK_EQUAL (ch, ResSuccess );
12041210
12051211 // When we try to probe the lock now, it should fail.
1206- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname, true ), false );
1212+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname, true ), util::LockResult::ErrorLock );
12071213
12081214 // Unlock the lock in the child process
12091215 BOOST_CHECK_EQUAL (write (fd[1 ], &UnlockCommand, 1 ), 1 );
12101216 BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1211- BOOST_CHECK_EQUAL (( bool ) ch, true );
1217+ BOOST_CHECK_EQUAL (ch, ResUnlockSuccess );
12121218
12131219 // When we try to probe the lock now, it should succeed.
1214- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname, true ), true );
1220+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname, true ), util::LockResult::Success );
12151221
12161222 // Re-lock the lock in the child process, then wait for it to exit, check
12171223 // successful return. After that, we check that exiting the process
@@ -1221,7 +1227,7 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
12211227 BOOST_CHECK_EQUAL (write (fd[1 ], &ExitCommand, 1 ), 1 );
12221228 BOOST_CHECK_EQUAL (waitpid (pid, &processstatus, 0 ), pid);
12231229 BOOST_CHECK_EQUAL (processstatus, 0 );
1224- BOOST_CHECK_EQUAL (LockDirectory (dirname, lockname, true ), true );
1230+ BOOST_CHECK_EQUAL (util:: LockDirectory (dirname, lockname, true ), util::LockResult::Success );
12251231
12261232 // Restore SIGCHLD
12271233 signal (SIGCHLD, old_handler);
0 commit comments