@@ -1106,31 +1106,37 @@ BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
1106
1106
BOOST_CHECK (!ParseFixedPoint (" 31.999999999999999999999" , 3 , &amount));
1107
1107
}
1108
1108
1109
- static void TestOtherThread (fs::path dirname, fs::path lockname, bool *result)
1110
- {
1111
- *result = LockDirectory (dirname, lockname);
1112
- }
1113
-
1114
1109
#ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork()
1115
1110
static constexpr char LockCommand = ' L' ;
1116
1111
static constexpr char UnlockCommand = ' U' ;
1117
1112
static 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
+ };
1118
1118
1119
1119
[[noreturn]] static void TestOtherProcess (fs::path dirname, fs::path lockname, int fd)
1120
1120
{
1121
1121
char ch;
1122
1122
while (true ) {
1123
1123
int rv = read (fd, &ch, 1 ); // Wait for command
1124
1124
assert (rv == 1 );
1125
- switch (ch) {
1125
+ switch (ch) {
1126
1126
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
+ }();
1128
1134
rv = write (fd, &ch, 1 );
1129
1135
assert (rv == 1 );
1130
1136
break ;
1131
1137
case UnlockCommand:
1132
1138
ReleaseDirectoryLocks ();
1133
- ch = true ; // Always succeeds
1139
+ ch = ResUnlockSuccess ; // Always succeeds
1134
1140
rv = write (fd, &ch, 1 );
1135
1141
assert (rv == 1 );
1136
1142
break ;
@@ -1167,51 +1173,51 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
1167
1173
BOOST_CHECK_EQUAL (close (fd[0 ]), 0 ); // Parent: close child end
1168
1174
#endif
1169
1175
// 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 );
1171
1177
1172
1178
fs::create_directories (dirname);
1173
1179
1174
1180
// 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 );
1176
1182
1177
1183
// 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 );
1179
1185
1180
1186
// 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 );
1182
1188
1183
1189
// 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); } );
1186
1192
thr.join ();
1187
- BOOST_CHECK_EQUAL (threadresult, true );
1193
+ BOOST_CHECK_EQUAL (threadresult, util::LockResult::Success );
1188
1194
#ifndef WIN32
1189
1195
// Try to acquire lock in child process while we're holding it, this should fail.
1190
1196
char ch;
1191
1197
BOOST_CHECK_EQUAL (write (fd[1 ], &LockCommand, 1 ), 1 );
1192
1198
BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1193
- BOOST_CHECK_EQUAL (( bool ) ch, false );
1199
+ BOOST_CHECK_EQUAL (ch, ResErrorLock );
1194
1200
1195
1201
// Give up our lock
1196
1202
ReleaseDirectoryLocks ();
1197
1203
// 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 );
1199
1205
1200
1206
// Try to acquire the lock in the child process, this should be successful.
1201
1207
BOOST_CHECK_EQUAL (write (fd[1 ], &LockCommand, 1 ), 1 );
1202
1208
BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1203
- BOOST_CHECK_EQUAL (( bool ) ch, true );
1209
+ BOOST_CHECK_EQUAL (ch, ResSuccess );
1204
1210
1205
1211
// 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 );
1207
1213
1208
1214
// Unlock the lock in the child process
1209
1215
BOOST_CHECK_EQUAL (write (fd[1 ], &UnlockCommand, 1 ), 1 );
1210
1216
BOOST_CHECK_EQUAL (read (fd[1 ], &ch, 1 ), 1 );
1211
- BOOST_CHECK_EQUAL (( bool ) ch, true );
1217
+ BOOST_CHECK_EQUAL (ch, ResUnlockSuccess );
1212
1218
1213
1219
// 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 );
1215
1221
1216
1222
// Re-lock the lock in the child process, then wait for it to exit, check
1217
1223
// successful return. After that, we check that exiting the process
@@ -1221,7 +1227,7 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
1221
1227
BOOST_CHECK_EQUAL (write (fd[1 ], &ExitCommand, 1 ), 1 );
1222
1228
BOOST_CHECK_EQUAL (waitpid (pid, &processstatus, 0 ), pid);
1223
1229
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 );
1225
1231
1226
1232
// Restore SIGCHLD
1227
1233
signal (SIGCHLD, old_handler);
0 commit comments