From d302c05700ab3474facded345d3d59d7cbc38184 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Tue, 29 Dec 2020 17:36:45 -0800 Subject: Deflake semaphore_test_native PiperOrigin-RevId: 349490873 --- test/syscalls/linux/semaphore.cc | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'test/syscalls/linux/semaphore.cc') diff --git a/test/syscalls/linux/semaphore.cc b/test/syscalls/linux/semaphore.cc index d485ad15a..0530fce44 100644 --- a/test/syscalls/linux/semaphore.cc +++ b/test/syscalls/linux/semaphore.cc @@ -32,8 +32,6 @@ #include "test/util/test_util.h" #include "test/util/thread_util.h" -using ::testing::Contains; - namespace gvisor { namespace testing { namespace { @@ -793,7 +791,6 @@ TEST(SemaphoreTest, IpcInfo) { struct seminfo info; // Drop CAP_IPC_OWNER which allows us to bypass semaphore permissions. ASSERT_NO_ERRNO(SetCapability(CAP_IPC_OWNER, false)); - ASSERT_THAT(semctl(0, 0, IPC_INFO, &info), SyscallSucceedsWithValue(0)); for (int i = 0; i < kLoops; i++) { AutoSem sem(semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT)); ASSERT_THAT(sem.get(), SyscallSucceeds()); @@ -805,13 +802,12 @@ TEST(SemaphoreTest, IpcInfo) { EXPECT_THAT(max_used_index = semctl(0, 0, IPC_INFO, &info), SyscallSucceeds()); - int index_count = 0; + std::set sem_ids_before_max_index; for (int i = 0; i <= max_used_index; i++) { struct semid_ds ds = {}; int sem_id = semctl(i, 0, SEM_STAT, &ds); // Only if index i is used within the registry. - if (sem_id != -1) { - ASSERT_THAT(sem_ids, Contains(sem_id)); + if (sem_ids.find(sem_id) != sem_ids.end()) { struct semid_ds ipc_stat_ds; ASSERT_THAT(semctl(sem_id, 0, IPC_STAT, &ipc_stat_ds), SyscallSucceeds()); EXPECT_EQ(ds.sem_perm.__key, ipc_stat_ds.sem_perm.__key); @@ -833,17 +829,15 @@ TEST(SemaphoreTest, IpcInfo) { ASSERT_THAT(semctl(sem_id, 0, IPC_SET, &ipc_set_ds), SyscallSucceeds()); ASSERT_THAT(semctl(i, 0, SEM_STAT, &ds), SyscallFailsWithErrno(EACCES)); - index_count += 1; + sem_ids_before_max_index.insert(sem_id); } } - EXPECT_EQ(index_count, kLoops); - ASSERT_THAT(semctl(0, 0, IPC_INFO, &info), - SyscallSucceedsWithValue(max_used_index)); + EXPECT_EQ(sem_ids_before_max_index.size(), kLoops); for (const int sem_id : sem_ids) { ASSERT_THAT(semctl(sem_id, 0, IPC_RMID), SyscallSucceeds()); } - ASSERT_THAT(semctl(0, 0, IPC_INFO, &info), SyscallSucceedsWithValue(0)); + ASSERT_THAT(semctl(0, 0, IPC_INFO, &info), SyscallSucceeds()); EXPECT_EQ(info.semmap, kSemMap); EXPECT_EQ(info.semmni, kSemMni); EXPECT_EQ(info.semmns, kSemMns); @@ -863,7 +857,6 @@ TEST(SemaphoreTest, SemInfo) { struct seminfo info; // Drop CAP_IPC_OWNER which allows us to bypass semaphore permissions. ASSERT_NO_ERRNO(SetCapability(CAP_IPC_OWNER, false)); - ASSERT_THAT(semctl(0, 0, IPC_INFO, &info), SyscallSucceedsWithValue(0)); for (int i = 0; i < kLoops; i++) { AutoSem sem(semget(IPC_PRIVATE, kSemSetSize, 0600 | IPC_CREAT)); ASSERT_THAT(sem.get(), SyscallSucceeds()); @@ -880,17 +873,19 @@ TEST(SemaphoreTest, SemInfo) { EXPECT_EQ(info.semmsl, kSemMsl); EXPECT_EQ(info.semopm, kSemOpm); EXPECT_EQ(info.semume, kSemUme); - EXPECT_EQ(info.semusz, sem_ids.size()); + // There could be semaphores existing in the system during the test, which + // prevents the test from getting a exact number, but the test could expect at + // least the number of sempahroes it creates in the begining of the test. + EXPECT_GE(info.semusz, sem_ids.size()); EXPECT_EQ(info.semvmx, kSemVmx); - EXPECT_EQ(info.semaem, sem_ids.size() * kSemSetSize); + EXPECT_GE(info.semaem, sem_ids.size() * kSemSetSize); - int index_count = 0; + std::set sem_ids_before_max_index; for (int i = 0; i <= max_used_index; i++) { struct semid_ds ds = {}; int sem_id = semctl(i, 0, SEM_STAT, &ds); // Only if index i is used within the registry. - if (sem_id != -1) { - ASSERT_THAT(sem_ids, Contains(sem_id)); + if (sem_ids.find(sem_id) != sem_ids.end()) { struct semid_ds ipc_stat_ds; ASSERT_THAT(semctl(sem_id, 0, IPC_STAT, &ipc_stat_ds), SyscallSucceeds()); EXPECT_EQ(ds.sem_perm.__key, ipc_stat_ds.sem_perm.__key); @@ -912,17 +907,15 @@ TEST(SemaphoreTest, SemInfo) { ASSERT_THAT(semctl(sem_id, 0, IPC_SET, &ipc_set_ds), SyscallSucceeds()); ASSERT_THAT(semctl(i, 0, SEM_STAT, &ds), SyscallFailsWithErrno(EACCES)); - index_count += 1; + sem_ids_before_max_index.insert(sem_id); } } - EXPECT_EQ(index_count, kLoops); - ASSERT_THAT(semctl(0, 0, SEM_INFO, &info), - SyscallSucceedsWithValue(max_used_index)); + EXPECT_EQ(sem_ids_before_max_index.size(), kLoops); for (const int sem_id : sem_ids) { ASSERT_THAT(semctl(sem_id, 0, IPC_RMID), SyscallSucceeds()); } - ASSERT_THAT(semctl(0, 0, SEM_INFO, &info), SyscallSucceedsWithValue(0)); + ASSERT_THAT(semctl(0, 0, SEM_INFO, &info), SyscallSucceeds()); EXPECT_EQ(info.semmap, kSemMap); EXPECT_EQ(info.semmni, kSemMni); EXPECT_EQ(info.semmns, kSemMns); @@ -930,9 +923,11 @@ TEST(SemaphoreTest, SemInfo) { EXPECT_EQ(info.semmsl, kSemMsl); EXPECT_EQ(info.semopm, kSemOpm); EXPECT_EQ(info.semume, kSemUme); - EXPECT_EQ(info.semusz, 0); + // Apart from semapahores that are not created by the test, we can't determine + // the exact number of semaphore sets and semaphores, as a result, semusz and + // semaem range from 0 to a random number. Since the numbers are always + // non-negative, the test will not check the reslts of semusz and semaem. EXPECT_EQ(info.semvmx, kSemVmx); - EXPECT_EQ(info.semaem, 0); } } // namespace -- cgit v1.2.3