threadpool_unittest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "sandbox/win/src/threadpool.h"
  5. #include <windows.h>
  6. #include <stdint.h>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. void __stdcall EmptyCallBack(void*, unsigned char) {}
  9. void __stdcall TestCallBack(void* context, unsigned char) {
  10. HANDLE event = reinterpret_cast<HANDLE>(context);
  11. ::SetEvent(event);
  12. }
  13. namespace sandbox {
  14. // Test that register and unregister work, part 1.
  15. TEST(IPCTest, ThreadPoolRegisterTest1) {
  16. ThreadPool thread_pool;
  17. EXPECT_EQ(0u, thread_pool.OutstandingWaits());
  18. HANDLE event1 = ::CreateEventW(nullptr, false, false, nullptr);
  19. HANDLE event2 = ::CreateEventW(nullptr, false, false, nullptr);
  20. uint32_t context = 0;
  21. EXPECT_FALSE(thread_pool.RegisterWait(0, event1, EmptyCallBack, &context));
  22. EXPECT_EQ(0u, thread_pool.OutstandingWaits());
  23. EXPECT_TRUE(thread_pool.RegisterWait(this, event1, EmptyCallBack, &context));
  24. EXPECT_EQ(1u, thread_pool.OutstandingWaits());
  25. EXPECT_TRUE(thread_pool.RegisterWait(this, event2, EmptyCallBack, &context));
  26. EXPECT_EQ(2u, thread_pool.OutstandingWaits());
  27. EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
  28. EXPECT_EQ(0u, thread_pool.OutstandingWaits());
  29. EXPECT_TRUE(::CloseHandle(event1));
  30. EXPECT_TRUE(::CloseHandle(event2));
  31. }
  32. // Test that register and unregister work, part 2.
  33. TEST(IPCTest, ThreadPoolRegisterTest2) {
  34. ThreadPool thread_pool;
  35. HANDLE event1 = ::CreateEventW(nullptr, false, false, nullptr);
  36. HANDLE event2 = ::CreateEventW(nullptr, false, false, nullptr);
  37. uint32_t context = 0;
  38. uint32_t c1 = 0;
  39. uint32_t c2 = 0;
  40. EXPECT_TRUE(thread_pool.RegisterWait(&c1, event1, EmptyCallBack, &context));
  41. EXPECT_EQ(1u, thread_pool.OutstandingWaits());
  42. EXPECT_TRUE(thread_pool.RegisterWait(&c2, event2, EmptyCallBack, &context));
  43. EXPECT_EQ(2u, thread_pool.OutstandingWaits());
  44. EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
  45. EXPECT_EQ(1u, thread_pool.OutstandingWaits());
  46. EXPECT_TRUE(thread_pool.UnRegisterWaits(&c2));
  47. EXPECT_EQ(1u, thread_pool.OutstandingWaits());
  48. EXPECT_TRUE(thread_pool.UnRegisterWaits(&c1));
  49. EXPECT_EQ(0u, thread_pool.OutstandingWaits());
  50. EXPECT_TRUE(::CloseHandle(event1));
  51. EXPECT_TRUE(::CloseHandle(event2));
  52. }
  53. // Test that the thread pool has at least a thread that services an event.
  54. // Test that when the event is un-registered is no longer serviced.
  55. TEST(IPCTest, ThreadPoolSignalAndWaitTest) {
  56. ThreadPool thread_pool;
  57. // The events are auto reset and start not signaled.
  58. HANDLE event1 = ::CreateEventW(nullptr, false, false, nullptr);
  59. HANDLE event2 = ::CreateEventW(nullptr, false, false, nullptr);
  60. EXPECT_TRUE(thread_pool.RegisterWait(this, event1, TestCallBack, event2));
  61. EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, false));
  62. EXPECT_EQ(WAIT_OBJECT_0, ::SignalObjectAndWait(event1, event2, 5000, false));
  63. EXPECT_TRUE(thread_pool.UnRegisterWaits(this));
  64. EXPECT_EQ(0u, thread_pool.OutstandingWaits());
  65. EXPECT_EQ(static_cast<DWORD>(WAIT_TIMEOUT),
  66. ::SignalObjectAndWait(event1, event2, 1000, false));
  67. EXPECT_TRUE(::CloseHandle(event1));
  68. EXPECT_TRUE(::CloseHandle(event2));
  69. }
  70. } // namespace sandbox