PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/tests/thread.cpp

http://github.com/mozy/mordor
C++ | 79 lines | 57 code | 11 blank | 11 comment | 0 complexity | 87058e795d129f899390b8ae60c8cb84 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2010 - Mozy, Inc.
  2. #include <boost/bind.hpp>
  3. #include "mordor/test/test.h"
  4. #include "mordor/thread.h"
  5. #include "mordor/workerpool.h"
  6. using namespace Mordor;
  7. static void threadFunc(tid_t &myTid)
  8. {
  9. myTid = gettid();
  10. }
  11. // Tests to make sure the tid as reported by Thread object and inside the
  12. // thread itself are the same (and have a valid value)
  13. MORDOR_UNITTEST(Thread, correctTID)
  14. {
  15. tid_t tid = emptytid();
  16. Thread t(boost::bind(&threadFunc, boost::ref(tid)), "my thread");
  17. t.join();
  18. MORDOR_TEST_ASSERT_NOT_EQUAL(tid, emptytid());
  19. MORDOR_TEST_ASSERT_EQUAL(tid, t.tid());
  20. }
  21. MORDOR_UNITTEST(Thread, bookMark)
  22. {
  23. WorkerPool poolA(1, true), poolB(1, false);
  24. tid_t mainTid = gettid();
  25. Thread::Bookmark mark;
  26. MORDOR_TEST_ASSERT_EQUAL(mark.tid(), mainTid);
  27. poolB.switchTo();
  28. tid_t tidB = gettid();
  29. MORDOR_TEST_ASSERT_NOT_EQUAL(tidB, mainTid);
  30. mark.switchTo();
  31. tid_t current = gettid();
  32. MORDOR_TEST_ASSERT_EQUAL(current, mainTid);
  33. }
  34. namespace {
  35. struct DummyException : public virtual Mordor::Exception {};
  36. static void doSomething(WorkerPool &pool)
  37. {
  38. pool.switchTo();
  39. }
  40. static void rethrowException(WorkerPool &poolA, WorkerPool &poolB)
  41. {
  42. tid_t mainTid = 0;
  43. try {
  44. // execute in poolA
  45. poolA.switchTo();
  46. mainTid = gettid();
  47. MORDOR_THROW_EXCEPTION(DummyException());
  48. } catch (...) {
  49. // still in poolA
  50. MORDOR_TEST_ASSERT_EQUAL(gettid(), mainTid);
  51. // do something that might switch or not switch thread
  52. // in this test case, it does switch to a different thread
  53. doSomething(poolB);
  54. MORDOR_TEST_ASSERT_NOT_EQUAL(mainTid, gettid());
  55. // switch back
  56. // rethrow the exception without any issue
  57. // if we do not handle exception stack, this `throw' can't be caught any
  58. // more, and C++ runtime terminates the program.
  59. throw;
  60. }
  61. }
  62. }
  63. MORDOR_UNITTEST(Thread, exceptionRethrow)
  64. {
  65. WorkerPool poolA(1, true), poolB(1, false);
  66. MORDOR_TEST_ASSERT_EXCEPTION(rethrowException(poolA, poolB), DummyException);
  67. poolA.switchTo();
  68. }