threadPoolTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "testing/unitTesting.h"
  23. #include "platform/threads/threadPool.h"
  24. #include "console/console.h"
  25. #include "core/util/tVector.h"
  26. FIXTURE(ThreadPool)
  27. {
  28. public:
  29. // Represents a single unit of work. In this test we just set an element in
  30. // a result vector.
  31. struct TestItem : public ThreadPool::WorkItem
  32. {
  33. U32 mIndex;
  34. Vector<U32>& mResults;
  35. TestItem(U32 index, Vector<U32>& results)
  36. : mIndex(index), mResults(results) {}
  37. protected:
  38. virtual void execute()
  39. {
  40. mResults[mIndex] = mIndex;
  41. }
  42. };
  43. // A worker that delays for some time. We'll use this to test the ThreadPool's
  44. // synchronous and asynchronous operations.
  45. struct DelayItem : public ThreadPool::WorkItem
  46. {
  47. U32 ms;
  48. DelayItem(U32 _ms) : ms(_ms) {}
  49. protected:
  50. virtual void execute()
  51. {
  52. Platform::sleep(ms);
  53. }
  54. };
  55. };
  56. TEST_FIX(ThreadPool, BasicAPI)
  57. {
  58. // Construct the vector of results from the work items.
  59. const U32 numItems = 100;
  60. Vector<U32> results(__FILE__, __LINE__);
  61. results.setSize(numItems);
  62. for (U32 i = 0; i < numItems; i++)
  63. results[i] = U32(-1);
  64. // Launch the work items.
  65. ThreadPool* pool = &ThreadPool::GLOBAL();
  66. for (U32 i = 0; i < numItems; i++)
  67. {
  68. ThreadSafeRef<TestItem> item(new TestItem(i, results));
  69. pool->queueWorkItem(item);
  70. }
  71. pool->waitForAllItems();
  72. // Verify.
  73. for (U32 i = 0; i < numItems; i++)
  74. EXPECT_EQ(results[i], i) << "result mismatch";
  75. results.clear();
  76. }
  77. TEST_FIX(ThreadPool, Asynchronous)
  78. {
  79. const U32 delay = 500; //ms
  80. // Launch a single delaying work item.
  81. ThreadPool* pool = &ThreadPool::GLOBAL();
  82. ThreadSafeRef<DelayItem> item(new DelayItem(delay));
  83. pool->queueWorkItem(item);
  84. // The thread should not yet be finished.
  85. EXPECT_EQ(false, item->hasExecuted());
  86. // Wait til the item should have completed.
  87. Platform::sleep(delay * 2);
  88. EXPECT_EQ(true, item->hasExecuted());
  89. }
  90. TEST_FIX(ThreadPool, Synchronous)
  91. {
  92. const U32 delay = 500; //ms
  93. // Launch a single delaying work item.
  94. ThreadPool* pool = &ThreadPool::GLOBAL();
  95. ThreadSafeRef<DelayItem> item(new DelayItem(delay));
  96. pool->queueWorkItem(item);
  97. // Wait for the item to complete.
  98. pool->waitForAllItems();
  99. EXPECT_EQ(true, item->hasExecuted());
  100. }