PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 1ms

/mordor/tests/fls.cpp

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