]> git.seodisparate.com - UDPConnection/commitdiff
UnitTest for start/stop threaded update and fix
authorStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jan 2024 11:07:25 +0000 (20:07 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jan 2024 11:07:25 +0000 (20:07 +0900)
src/UDPConnection.cpp
src/test/TestUDPC.cpp

index 37dab7f590c1247c7ba34517c2cf36dfc48f633c..617778fb115cc69a1bf01b3f29218986c32ca706 100644 (file)
@@ -2326,7 +2326,9 @@ void UDPC_destroy(UDPC_HContext ctx) {
         // After lock has been dropped, wait in case there are enable/disable
         // threaded update functions waiting on the lock. Do this via a
         // atomic-int-based spin-lock.
-        while(UDPC_ctx->enableDisableFuncRunningCount.load() != 0) {}
+        while(UDPC_ctx->enableDisableFuncRunningCount.load() != 0) {
+            std::this_thread::sleep_for(std::chrono::milliseconds(10));
+        }
 
 #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
         WSACleanup();
index 1cdd83ea6e22fe4aa7954981a230834680af3e60..7d75416957fa060b7445132db4742f0421ec23b5 100644 (file)
@@ -434,3 +434,29 @@ TEST(UDPC, free_packet_ptr) {
     UDPC_free_PacketInfo_ptr(&pinfo);
     UDPC_free_PacketInfo_ptr(nullptr);
 }
+
+TEST(UDPC, enableDisableThreadedUpdate_StressTest) {
+    UDPC_ConnectionId id = UDPC_create_id_anyaddr(0);
+    UDPC_HContext ctx = UDPC_init(id, 0, 0);
+
+    std::array<std::thread, 100> thread_array;
+    for (int i = 0; i < 100; ++i) {
+        if (i % 2 == 0) {
+            thread_array[i] = std::thread([] (UDPC_HContext ctx) {
+                UDPC_enable_threaded_update(ctx);
+            }, ctx);
+        } else {
+            thread_array[i] = std::thread([] (UDPC_HContext ctx) {
+                UDPC_disable_threaded_update(ctx);
+            }, ctx);
+        }
+    }
+
+    thread_array[0].join();
+
+    UDPC_destroy(ctx);
+
+    for (int i = 1; i < 100; ++i) {
+        thread_array[i].join();
+    }
+}