]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test: Avoid named semaphores on Windows
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 15 Jan 2020 08:39:35 +0000 (08:39 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Tue, 5 Jan 2021 12:56:11 +0000 (14:56 +0200)
The windows pthread library doesn't support named semaphores, for
which reason we're updating some tests to use unnamed semaphores.

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
src/test/librados/watch_notify.cc
src/test/librados/watch_notify_cxx.cc
src/test/libradosstriper/aio.cc
src/test/system/cross_process_sem.cc
src/test/test_stress_watch.cc

index 148131062f786e3a704bcfbbffcca13f5e6be3ae..c87fb40aebf79af37d6d1f9c7b14c5741086f9e0 100644 (file)
@@ -16,12 +16,12 @@ typedef RadosTestEC LibRadosWatchNotifyEC;
 int notify_sleep = 0;
 
 // notify
-static sem_t *sem;
+static sem_t sem;
 
 static void watch_notify_test_cb(uint8_t opcode, uint64_t ver, void *arg)
 {
   std::cout << __func__ << std::endl;
-  sem_post(sem);
+  sem_post(&sem);
 }
 
 class LibRadosWatchNotify : public RadosTest
@@ -85,7 +85,7 @@ class WatchNotifyTestCtx2;
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 
 TEST_F(LibRadosWatchNotify, WatchNotify) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   char buf[128];
   memset(buf, 0xcc, sizeof(buf));
   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
@@ -102,18 +102,18 @@ TEST_F(LibRadosWatchNotify, WatchNotify) {
     }
   }
   TestAlarm alarm;
-  sem_wait(sem);
+  sem_wait(&sem);
   rados_unwatch(ioctx, "foo", handle);
 
   // when dne ...
   ASSERT_EQ(-ENOENT,
       rados_watch(ioctx, "dne", 0, &handle, watch_notify_test_cb, NULL));
 
-  sem_close(sem);
+  sem_destroy(&sem);
 }
 
 TEST_F(LibRadosWatchNotifyEC, WatchNotify) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   char buf[128];
   memset(buf, 0xcc, sizeof(buf));
   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
@@ -130,9 +130,9 @@ TEST_F(LibRadosWatchNotifyEC, WatchNotify) {
     }
   }
   TestAlarm alarm;
-  sem_wait(sem);
+  sem_wait(&sem);
   rados_unwatch(ioctx, "foo", handle);
-  sem_close(sem);
+  sem_destroy(&sem);
 }
 
 #pragma GCC diagnostic pop
index e6b1027d4a4b201234cb33e3232ec63894f1e9e9..624f417dad19419a215170c6dcbf1d72aabab982 100644 (file)
@@ -68,7 +68,7 @@ public:
 };
 
 // notify
-static sem_t *sem;
+static sem_t sem;
 
 class WatchNotifyTestCtx : public WatchCtx
 {
@@ -76,12 +76,12 @@ public:
   void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
   {
     std::cout << __func__ << std::endl;
-    sem_post(sem);
+    sem_post(&sem);
   }
 };
 
 TEST_P(LibRadosWatchNotifyPP, WatchNotify) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   char buf[128];
   memset(buf, 0xcc, sizeof(buf));
   bufferlist bl1;
@@ -104,13 +104,13 @@ TEST_P(LibRadosWatchNotifyPP, WatchNotify) {
     }
   }
   TestAlarm alarm;
-  sem_wait(sem);
+  sem_wait(&sem);
   ioctx.unwatch("foo", handle);
-  sem_close(sem);
+  sem_destroy(&sem);
 }
 
 TEST_F(LibRadosWatchNotifyECPP, WatchNotify) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   char buf[128];
   memset(buf, 0xcc, sizeof(buf));
   bufferlist bl1;
@@ -133,15 +133,15 @@ TEST_F(LibRadosWatchNotifyECPP, WatchNotify) {
     }
   }
   TestAlarm alarm;
-  sem_wait(sem);
+  sem_wait(&sem);
   ioctx.unwatch("foo", handle);
-  sem_close(sem);
+  sem_destroy(&sem);
 }
 
 // --
 
 TEST_P(LibRadosWatchNotifyPP, WatchNotifyTimeout) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   ioctx.set_notify_timeout(1);
   uint64_t handle;
   WatchNotifyTestCtx ctx;
@@ -153,12 +153,12 @@ TEST_P(LibRadosWatchNotifyPP, WatchNotifyTimeout) {
   ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
 
   ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
-  sem_close(sem);
+  sem_destroy(&sem);
   ASSERT_EQ(0, ioctx.unwatch("foo", handle));
 }
 
 TEST_F(LibRadosWatchNotifyECPP, WatchNotifyTimeout) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("/test_watch_notify_sem", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   ioctx.set_notify_timeout(1);
   uint64_t handle;
   WatchNotifyTestCtx ctx;
@@ -170,7 +170,7 @@ TEST_F(LibRadosWatchNotifyECPP, WatchNotifyTimeout) {
   ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
 
   ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
-  sem_close(sem);
+  sem_destroy(&sem);
   ASSERT_EQ(0, ioctx.unwatch("foo", handle));
 }
 
index 68a45aec52dd47f39a63ee8922c1d14daff2cb42..71e6abd7a0d252eb9a1afdf4fd5cf14bbff22ea5 100644 (file)
@@ -18,23 +18,32 @@ class AioTestData
 {
 public:
   AioTestData() : m_complete(false) {
-    m_sem = sem_open("test_libradosstriper_aio_sem", O_CREAT, 0644, 0);
+    sem_init(&m_sem, 0, 0);
   }
 
   ~AioTestData() {
-    sem_unlink("test_libradosstriper_aio_sem");
-    sem_close(m_sem);
+    sem_destroy(&m_sem);
+  }
+
+  void notify() {
+    sem_post(&m_sem);
+  }
+
+  void wait() {
+    sem_wait(&m_sem);
   }
 
-  sem_t *m_sem;
   bool m_complete;
+
+private:
+  sem_t m_sem;
 };
 
 void set_completion_complete(rados_completion_t cb, void *arg)
 {
   AioTestData *test = static_cast<AioTestData*>(arg);
   test->m_complete = true;
-  sem_post(test->m_sem);
+  test->notify();
 }
 
 TEST_F(StriperTest, SimpleWrite) {
@@ -47,7 +56,7 @@ TEST_F(StriperTest, SimpleWrite) {
   memset(buf, 0xcc, sizeof(buf));
   ASSERT_EQ(0, rados_striper_aio_write(striper, "StriperTest", my_completion, buf, sizeof(buf), 0));
   TestAlarm alarm;
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
 }
 
@@ -61,7 +70,7 @@ TEST_F(StriperTestPP, SimpleWritePP) {
   bl1.append(buf, sizeof(buf));
   ASSERT_EQ(0, striper.aio_write("SimpleWritePP", my_completion, bl1, sizeof(buf), 0));
   TestAlarm alarm;
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
 }
 
@@ -76,7 +85,7 @@ TEST_F(StriperTest, WaitForSafe) {
   ASSERT_EQ(0, rados_striper_aio_write(striper, "WaitForSafe", my_completion, buf, sizeof(buf), 0));
   TestAlarm alarm;
   rados_aio_wait_for_complete(my_completion);
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
 }
 
@@ -92,7 +101,7 @@ TEST_F(StriperTestPP, WaitForSafePP) {
   ASSERT_EQ(0, striper.aio_write("WaitForSafePP", my_completion, bl1, sizeof(buf), 0));
   TestAlarm alarm;
   my_completion->wait_for_complete();
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
 }
 
@@ -107,7 +116,7 @@ TEST_F(StriperTest, RoundTrip) {
   ASSERT_EQ(0, rados_striper_aio_write(striper, "RoundTrip", my_completion, buf, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   char buf2[128];
   memset(buf2, 0, sizeof(buf2));
@@ -121,7 +130,7 @@ TEST_F(StriperTest, RoundTrip) {
     rados_aio_wait_for_complete(my_completion2);
   }
   ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
 }
@@ -137,7 +146,7 @@ TEST_F(StriperTest, RoundTrip2) {
   ASSERT_EQ(0, rados_striper_aio_write(striper, "RoundTrip2", my_completion, buf, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   char buf2[128];
   memset(buf2, 0, sizeof(buf2));
@@ -151,7 +160,7 @@ TEST_F(StriperTest, RoundTrip2) {
     rados_aio_wait_for_complete(my_completion2);
   }
   ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
 }
@@ -167,7 +176,7 @@ TEST_F(StriperTestPP, RoundTripPP) {
   ASSERT_EQ(0, striper.aio_write("RoundTripPP", my_completion, bl1, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   bufferlist bl2;
   AioCompletion *my_completion2 =
@@ -178,7 +187,7 @@ TEST_F(StriperTestPP, RoundTripPP) {
     my_completion2->wait_for_complete();
   }
   ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
 }
@@ -194,7 +203,7 @@ TEST_F(StriperTestPP, RoundTripPP2) {
   ASSERT_EQ(0, striper.aio_write("RoundTripPP2", my_completion, bl1, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   bufferlist bl2;
   AioCompletion *my_completion2 =
@@ -205,7 +214,7 @@ TEST_F(StriperTestPP, RoundTripPP2) {
     my_completion2->wait_for_complete();
   }
   ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
 }
@@ -221,7 +230,7 @@ TEST_F(StriperTest, IsComplete) {
   ASSERT_EQ(0, rados_striper_aio_write(striper, "IsComplete", my_completion, buf, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   char buf2[128];
   memset(buf2, 0, sizeof(buf2));
@@ -241,7 +250,7 @@ TEST_F(StriperTest, IsComplete) {
     }
   }
   ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
 }
@@ -257,7 +266,7 @@ TEST_F(StriperTestPP, IsCompletePP) {
   ASSERT_EQ(0, striper.aio_write("IsCompletePP", my_completion, bl1, sizeof(buf), 0));
   {
     TestAlarm alarm;
-    sem_wait(test_data.m_sem);
+    test_data.wait();
   }
   bufferlist bl2;
   AioCompletion *my_completion2 =
@@ -274,7 +283,7 @@ TEST_F(StriperTestPP, IsCompletePP) {
     }
   }
   ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
 }
@@ -310,7 +319,7 @@ TEST_F(StriperTest, IsSafe) {
     rados_aio_wait_for_complete(my_completion2);
   }
   ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
 }
@@ -351,7 +360,7 @@ TEST_F(StriperTest, RoundTripAppend) {
   ASSERT_EQ((int)(sizeof(buf) + sizeof(buf2)), rados_aio_get_return_value(my_completion3));
   ASSERT_EQ(0, memcmp(buf3, buf, sizeof(buf)));
   ASSERT_EQ(0, memcmp(buf3 + sizeof(buf), buf2, sizeof(buf2)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
   rados_aio_release(my_completion3);
@@ -392,7 +401,7 @@ TEST_F(StriperTestPP, RoundTripAppendPP) {
   ASSERT_EQ(sizeof(buf) + sizeof(buf2), (unsigned)my_completion3->get_return_value());
   ASSERT_EQ(0, memcmp(bl3.c_str(), buf, sizeof(buf)));
   ASSERT_EQ(0, memcmp(bl3.c_str() + sizeof(buf), buf2, sizeof(buf2)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
   my_completion3->release();
@@ -420,7 +429,7 @@ TEST_F(StriperTest, Flush) {
     rados_aio_wait_for_complete(my_completion2);
   }
   ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
 }
@@ -444,7 +453,7 @@ TEST_F(StriperTestPP, FlushPP) {
     my_completion2->wait_for_complete();
   }
   ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
 }
@@ -484,7 +493,7 @@ TEST_F(StriperTest, RoundTripWriteFull) {
   }
   ASSERT_EQ(sizeof(buf2), (unsigned)rados_aio_get_return_value(my_completion3));
   ASSERT_EQ(0, memcmp(buf3, buf2, sizeof(buf2)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   rados_aio_release(my_completion);
   rados_aio_release(my_completion2);
   rados_aio_release(my_completion3);
@@ -524,7 +533,7 @@ TEST_F(StriperTestPP, RoundTripWriteFullPP) {
   }
   ASSERT_EQ(sizeof(buf2), (unsigned)my_completion3->get_return_value());
   ASSERT_EQ(0, memcmp(bl3.c_str(), buf2, sizeof(buf2)));
-  sem_wait(test_data.m_sem);
+  test_data.wait();
   my_completion->release();
   my_completion2->release();
   my_completion3->release();
index 7438b882781563db8d0b6f0de17d45fc7880c85b..d73259d256dcca78a38ea71013c6d77988f6fccd 100644 (file)
@@ -46,12 +46,13 @@ create(int initial_val, CrossProcessSem** res)
     int err = errno;
     return err;
   }
+  int ret = sem_init(&data->sem, 1, initial_val);
   #else
   // We can't use multiple processes on Windows for the time being.
   struct cross_process_sem_data_t *data = (cross_process_sem_data_t*)malloc(
     sizeof(cross_process_sem_data_t));
+  int ret = sem_init(&data->sem, 0, initial_val);
   #endif /* _WIN32 */
-  int ret = sem_init(&data->sem, 1, initial_val);
   if (ret) {
     return ret;
   }
index 5da2b58b154a8163e163133f2935c24239612202..2058bbf1a05d4d73db4fca3b4cd263699cf964ae 100644 (file)
@@ -22,7 +22,7 @@ using std::map;
 using std::ostringstream;
 using std::string;
 
-static sem_t *sem;
+static sem_t sem;
 static std::atomic<bool> stop_flag = { false };
 
 class WatchNotifyTestCtx : public WatchCtx
@@ -30,7 +30,7 @@ class WatchNotifyTestCtx : public WatchCtx
 public:
     void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
     {
-      sem_post(sem);
+      sem_post(&sem);
     }
 };
 
@@ -66,7 +66,7 @@ INSTANTIATE_TEST_SUITE_P(WatchStressTests, WatchStress,
                        ::testing::Values("", "cache"));
 
 TEST_P(WatchStress, Stress1) {
-  ASSERT_NE(SEM_FAILED, (sem = sem_open("test_stress_watch", O_CREAT, 0644, 0)));
+  ASSERT_EQ(0, sem_init(&sem, 0, 0));
   Rados ncluster;
   std::string pool_name = get_temp_pool_name();
   ASSERT_EQ("", create_one_pool_pp(pool_name, ncluster));
@@ -102,7 +102,7 @@ TEST_P(WatchStress, Stress1) {
       sleep(1); // Give a change to see an incorrect notify
     } else {
       TestAlarm alarm;
-      sem_wait(sem);
+      sem_wait(&sem);
     }
 
     if (do_blocklist) {
@@ -116,7 +116,7 @@ TEST_P(WatchStress, Stress1) {
   thr->join();
   nioctx.close();
   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, ncluster));
-  sem_close(sem);
+  sem_destroy(&sem);
 }
 
 #pragma GCC diagnostic pop