TestPollableEvent.cpp (1947B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "TestCommon.h" 7 #include "gtest/gtest.h" 8 9 #include "mozilla/gtest/MozAssertions.h" 10 #include "nsCOMPtr.h" 11 #include "nsISocketTransport.h" 12 #include "../../base/nsSocketTransportService2.h" 13 #include "../../base/PollableEvent.h" 14 #include "nsServiceManagerUtils.h" 15 #include "nsThreadUtils.h" 16 17 using namespace mozilla; 18 using namespace mozilla::net; 19 20 // Test for bug 1993248 - PR_CreatePipe doesn't set nonblocking flag 21 // This test verifies that PollableEvent::Clear() doesn't block when called 22 TEST(TestPollableEvent, ClearDoesNotBlock) 23 { 24 nsCOMPtr<nsISocketTransportService> service = 25 do_GetService("@mozilla.org/network/socket-transport-service;1"); 26 ASSERT_TRUE(service); 27 28 auto* sts = gSocketTransportService; 29 ASSERT_TRUE(sts); 30 31 NS_DispatchAndSpinEventLoopUntilComplete( 32 "TestPollableEvent::ClearDoesNotBlock"_ns, sts, 33 NS_NewRunnableFunction("TestPollableEvent::ClearDoesNotBlock", [&]() { 34 // Create a PollableEvent 35 PollableEvent event; 36 ASSERT_TRUE(event.Valid()); 37 38 // The constructor signals the event, so Clear() should succeed 39 ASSERT_TRUE(event.Clear()); 40 41 // Signal the event 42 ASSERT_TRUE(event.Signal()); 43 44 // Clear should succeed and not block 45 ASSERT_TRUE(event.Clear()); 46 47 // Calling Clear() again without Signal() should also not block 48 // It should return true even when there's no data available 49 ASSERT_TRUE(event.Clear()); 50 51 // Signal and clear multiple times to ensure robustness 52 for (int i = 0; i < 10; i++) { 53 ASSERT_TRUE(event.Signal()); 54 ASSERT_TRUE(event.Clear()); 55 } 56 })); 57 }