threadPoolTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/threads/threadPool.h"
  25. #include "console/console.h"
  26. #include "core/util/tVector.h"
  27. FIXTURE(ThreadPool)
  28. {
  29. public:
  30. // Represents a single unit of work. In this test we just set an element in
  31. // a result vector.
  32. struct TestItem : public ThreadPool::WorkItem
  33. {
  34. U32 mIndex;
  35. Vector<U32>& mResults;
  36. TestItem(U32 index, Vector<U32>& results)
  37. : mIndex(index), mResults(results) {}
  38. protected:
  39. virtual void execute()
  40. {
  41. mResults[mIndex] = mIndex;
  42. }
  43. };
  44. // A worker that delays for some time. We'll use this to test the ThreadPool's
  45. // synchronous and asynchronous operations.
  46. struct DelayItem : public ThreadPool::WorkItem
  47. {
  48. U32 ms;
  49. DelayItem(U32 _ms) : ms(_ms) {}
  50. protected:
  51. virtual void execute()
  52. {
  53. Platform::sleep(ms);
  54. }
  55. };
  56. };
  57. TEST_FIX(ThreadPool, BasicAPI)
  58. {
  59. // Construct the vector of results from the work items.
  60. const U32 numItems = 100;
  61. Vector<U32> results(__FILE__, __LINE__);
  62. results.setSize(numItems);
  63. for (U32 i = 0; i < numItems; i++)
  64. results[i] = U32(-1);
  65. // Launch the work items.
  66. ThreadPool* pool = &ThreadPool::GLOBAL();
  67. for (U32 i = 0; i < numItems; i++)
  68. {
  69. ThreadSafeRef<TestItem> item(new TestItem(i, results));
  70. pool->queueWorkItem(item);
  71. }
  72. pool->waitForAllItems();
  73. // Verify.
  74. for (U32 i = 0; i < numItems; i++)
  75. EXPECT_EQ(results[i], i) << "result mismatch";
  76. results.clear();
  77. }
  78. TEST_FIX(ThreadPool, Asynchronous)
  79. {
  80. const U32 delay = 500; //ms
  81. // Launch a single delaying work item.
  82. ThreadPool* pool = &ThreadPool::GLOBAL();
  83. ThreadSafeRef<DelayItem> item(new DelayItem(delay));
  84. pool->queueWorkItem(item);
  85. // The thread should not yet be finished.
  86. EXPECT_EQ(false, item->hasExecuted());
  87. // Wait til the item should have completed.
  88. Platform::sleep(delay * 2);
  89. EXPECT_EQ(true, item->hasExecuted());
  90. }
  91. TEST_FIX(ThreadPool, Synchronous)
  92. {
  93. const U32 delay = 500; //ms
  94. // Launch a single delaying work item.
  95. ThreadPool* pool = &ThreadPool::GLOBAL();
  96. ThreadSafeRef<DelayItem> item(new DelayItem(delay));
  97. pool->queueWorkItem(item);
  98. // Wait for the item to complete.
  99. pool->waitForAllItems();
  100. EXPECT_EQ(true, item->hasExecuted());
  101. }
  102. #endif