Torque3D Documentation / _generateds / threadPoolTest.cpp

threadPoolTest.cpp

Engine/source/platform/threads/test/threadPoolTest.cpp

More...

Detailed Description

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