mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-15 00:24:40 +00:00
Fix ThreadPool tests to account for asynchronicity.
This commit is contained in:
parent
0995520d6f
commit
b491d7bbc0
1 changed files with 48 additions and 2 deletions
|
|
@ -44,6 +44,20 @@ public:
|
||||||
mResults[mIndex] = mIndex;
|
mResults[mIndex] = mIndex;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// A worker that delays for some time. We'll use this to test the ThreadPool's
|
||||||
|
// synchronous and asynchronous operations.
|
||||||
|
struct DelayItem : public ThreadPool::WorkItem
|
||||||
|
{
|
||||||
|
U32 ms;
|
||||||
|
DelayItem(U32 _ms) : ms(_ms) {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void execute()
|
||||||
|
{
|
||||||
|
Platform::sleep(ms);
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_FIX(ThreadPool, BasicAPI)
|
TEST_FIX(ThreadPool, BasicAPI)
|
||||||
|
|
@ -63,8 +77,7 @@ TEST_FIX(ThreadPool, BasicAPI)
|
||||||
pool->queueWorkItem(item);
|
pool->queueWorkItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all items to complete.
|
pool->waitForAllItems();
|
||||||
pool->flushWorkItems();
|
|
||||||
|
|
||||||
// Verify.
|
// Verify.
|
||||||
for (U32 i = 0; i < numItems; i++)
|
for (U32 i = 0; i < numItems; i++)
|
||||||
|
|
@ -72,4 +85,37 @@ TEST_FIX(ThreadPool, BasicAPI)
|
||||||
results.clear();
|
results.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_FIX(ThreadPool, Asynchronous)
|
||||||
|
{
|
||||||
|
const U32 delay = 500; //ms
|
||||||
|
|
||||||
|
// Launch a single delaying work item.
|
||||||
|
ThreadPool* pool = &ThreadPool::GLOBAL();
|
||||||
|
ThreadSafeRef<DelayItem> item(new DelayItem(delay));
|
||||||
|
pool->queueWorkItem(item);
|
||||||
|
|
||||||
|
// The thread should not yet be finished.
|
||||||
|
EXPECT_EQ(false, item->hasExecuted());
|
||||||
|
|
||||||
|
// Wait til the item should have completed.
|
||||||
|
Platform::sleep(delay * 2);
|
||||||
|
|
||||||
|
EXPECT_EQ(true, item->hasExecuted());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_FIX(ThreadPool, Synchronous)
|
||||||
|
{
|
||||||
|
const U32 delay = 500; //ms
|
||||||
|
|
||||||
|
// Launch a single delaying work item.
|
||||||
|
ThreadPool* pool = &ThreadPool::GLOBAL();
|
||||||
|
ThreadSafeRef<DelayItem> item(new DelayItem(delay));
|
||||||
|
pool->queueWorkItem(item);
|
||||||
|
|
||||||
|
// Wait for the item to complete.
|
||||||
|
pool->waitForAllItems();
|
||||||
|
|
||||||
|
EXPECT_EQ(true, item->hasExecuted());
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue