PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/tests/fls.cpp

http://github.com/mozy/mordor
C++ | 54 lines | 45 code | 8 blank | 1 comment | 0 complexity | 63a7383e9dcf4c830c1d86e1188cffe3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2009 - Mozy, Inc.
  2. #include <boost/bind.hpp>
  3. #include "mordor/fiber.h"
  4. #include "mordor/scheduler.h"
  5. #include "mordor/test/test.h"
  6. using namespace Mordor;
  7. static void basic(FiberLocalStorage<int> &fls)
  8. {
  9. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 0);
  10. fls = 2;
  11. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 2);
  12. Fiber::yield();
  13. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 2);
  14. fls = 4;
  15. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 4);
  16. Fiber::yield();
  17. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 4);
  18. fls = 6;
  19. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 6);
  20. }
  21. static void thread(FiberLocalStorage<int> &fls,
  22. Fiber::ptr fiber)
  23. {
  24. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 0);
  25. fls = 3;
  26. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 3);
  27. fiber->call();
  28. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 3);
  29. fls = 5;
  30. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 5);
  31. }
  32. MORDOR_UNITTEST(FLS, basic)
  33. {
  34. FiberLocalStorage<int> fls;
  35. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 0);
  36. fls = 1;
  37. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 1);
  38. Fiber::ptr fiber(new Fiber(boost::bind(&basic, boost::ref(fls))));
  39. fiber->call();
  40. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 1);
  41. Thread thread1(boost::bind(&thread, boost::ref(fls), fiber));
  42. thread1.join();
  43. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 1);
  44. fiber->call();
  45. MORDOR_TEST_ASSERT_EQUAL(fls.get(), 1);
  46. }