Skip to content

Commit e7167fe

Browse files
Bump MAX_FILES and gracefully handle filename exhaustion in all open rules
Increase MAX_FILES from 100 to 500 in PosixFileTests.cpp to give the filename pool more headroom. The pool caps at MAX_FILES+1 entries because get_filename picks in [0, MAX_FILES]; once FILES.size() exceeds that, only cached filenames are returned and new generation stops. Also change the uniqueness loop in OpenBaseRule, OpenFileCreateBounded, and OpenFileCreateString to gracefully skip the iteration (return) instead of failing with ASSERT_LT when a unique filename cannot be found. This prevents test explosions if the pool is ever exhausted again. Co-Authored-By: michael.d.starch <michael.d.starch@jpl.nasa.gov>
1 parent 02c0717 commit e7167fe

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

Os/Posix/test/ut/PosixFileTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace FileTest {
1717

1818
std::vector<std::shared_ptr<const std::string> > FILES;
1919

20-
static const U32 MAX_FILES = 100;
20+
static const U32 MAX_FILES = 500;
2121
static const char BASE_PATH[] = "/tmp/fprime";
2222
static const char TEST_FILE[] = "fprime-os-file-test";
2323
//! Check if we can use the file. F_OK file exists, R_OK, W_OK are read and write.

Os/test/ut/file/FileRules.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ void Os::Test::FileTest::Tester::OpenBaseRule::action(Os::Test::FileTest::Tester
290290
while (state.exists(*filename)) {
291291
filename = state.get_filename(this->m_random);
292292
attempts++;
293-
ASSERT_LT(attempts, MAX_FILENAME_ATTEMPTS)
294-
<< "Failed to generate unique filename after " << attempts << " attempts. "
295-
<< "Consider expanding the filename generation in get_filename().";
293+
// Gracefully skip this iteration if we cannot find a unique filename
294+
if (attempts >= MAX_FILENAME_ATTEMPTS) {
295+
return;
296+
}
296297
}
297298
}
298299

@@ -400,11 +401,19 @@ void Os::Test::FileTest::Tester::OpenFileCreateBounded::action(Os::Test::FileTes
400401
printf("--> Rule: %s mode %d\n", this->getName(), this->m_mode);
401402
// Initial variables used for this test
402403
std::shared_ptr<const std::string> filename = state.get_filename(this->m_random);
403-
// When randomly generating filenames, skip this iteration if the file already exists on the
404-
// real filesystem but not in the synthetic filesystem (which is reset per test). This avoids
405-
// a status mismatch between the real and synthetic open calls.
406-
if (this->m_random && !this->m_overwrite && state.exists(*filename)) {
407-
return;
404+
// When randomly generating filenames, some seeds can result in duplicate filenames
405+
// Continue generating until unique, unless this is an overwrite test
406+
constexpr U32 MAX_FILENAME_ATTEMPTS = 100000;
407+
U32 attempts = 0;
408+
if (this->m_random && !this->m_overwrite) {
409+
while (state.exists(*filename)) {
410+
filename = state.get_filename(this->m_random);
411+
attempts++;
412+
// Gracefully skip this iteration if we cannot find a unique filename
413+
if (attempts >= MAX_FILENAME_ATTEMPTS) {
414+
return;
415+
}
416+
}
408417
}
409418

410419
// Ensure initial and shadow states synchronized
@@ -443,11 +452,19 @@ void Os::Test::FileTest::Tester::OpenFileCreateString::action(Os::Test::FileTest
443452
printf("--> Rule: %s mode %d\n", this->getName(), this->m_mode);
444453
// Initial variables used for this test
445454
std::shared_ptr<const std::string> filename = state.get_filename(this->m_random);
446-
// When randomly generating filenames, skip this iteration if the file already exists on the
447-
// real filesystem but not in the synthetic filesystem (which is reset per test). This avoids
448-
// a status mismatch between the real and synthetic open calls.
449-
if (this->m_random && !this->m_overwrite && state.exists(*filename)) {
450-
return;
455+
// When randomly generating filenames, some seeds can result in duplicate filenames
456+
// Continue generating until unique, unless this is an overwrite test
457+
constexpr U32 MAX_FILENAME_ATTEMPTS = 100000;
458+
U32 attempts = 0;
459+
if (this->m_random && !this->m_overwrite) {
460+
while (state.exists(*filename)) {
461+
filename = state.get_filename(this->m_random);
462+
attempts++;
463+
// Gracefully skip this iteration if we cannot find a unique filename
464+
if (attempts >= MAX_FILENAME_ATTEMPTS) {
465+
return;
466+
}
467+
}
451468
}
452469

453470
// Ensure initial and shadow states synchronized

0 commit comments

Comments
 (0)