| | |
| | | #include "robust.h" |
| | | #include "util.h" |
| | | #include <boost/circular_buffer.hpp> |
| | | |
| | | using namespace robust; |
| | | |
| | |
| | | } |
| | | |
| | | ///////////////////////////////////////////////////////////////////////////////////////// |
| | | |
| | | BOOST_AUTO_TEST_CASE(InitTest) |
| | | { |
| | | AtomicReqRep rr; |
| | | auto client = [&]() { |
| | | for (int i = 0; i < 5; ++i) { |
| | | int64_t reply = 0; |
| | | bool r = rr.ClientRequest(i, reply); |
| | | printf("init request %d, %s, reply %d\n", i, (r ? "ok" : "failed"), reply); |
| | | } |
| | | }; |
| | | |
| | | bool run = true; |
| | | auto server = [&]() { |
| | | auto onReq = [](int64_t req) { return req + 100; }; |
| | | while (run) { |
| | | rr.ServerProcess(onReq); |
| | | } |
| | | }; |
| | | |
| | | ThreadManager clients, servers; |
| | | servers.Launch(server); |
| | | for (int i = 0; i < 2; ++i) { |
| | | clients.Launch(client); |
| | | } |
| | | clients.WaitAll(); |
| | | run = false; |
| | | servers.WaitAll(); |
| | | } |
| | | |
| | | BOOST_AUTO_TEST_CASE(QueueTest) |
| | | { |
| | |
| | | BOOST_CHECK_EQUAL((u64 & 255), i); |
| | | } |
| | | |
| | | #if 1 |
| | | typedef AtomicQueue<3> Rcb; |
| | | |
| | | Rcb tmp; |
| | | BOOST_CHECK(tmp.like_empty()); |
| | | BOOST_CHECK(tmp.push_back(1)); |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | BOOST_CHECK(tmp.head() == 0); |
| | | int64_t d; |
| | | BOOST_CHECK(tmp.pop_front(d)); |
| | | BOOST_CHECK(tmp.like_empty()); |
| | | BOOST_CHECK(tmp.head() == 1); |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | |
| | | ShmObject<Rcb> rcb(shm, "test_rcb"); |
| | | #else |
| | | typedef Circular<int64_t> Rcb; |
| | | ShmObject<Rcb> rcb(shm, "test_rcb", 64, shm.get_segment_manager()); |
| | | #endif |
| | | |
| | | const int nsize = sizeof(Rcb); |
| | | |
| | | bool try_more = false; |
| | | uint64_t correct_total = nmsg * (nmsg - 1) / 2; |
| | | std::atomic<uint64_t> total(0); |
| | | std::atomic<uint64_t> nwrite(0); |
| | | std::atomic<uint64_t> writedone(0); |
| | | |
| | | typedef AtomicQ63 Rcb; |
| | | |
| | | Rcb tmp; |
| | | BOOST_CHECK(tmp.push(1)); |
| | | int64_t d; |
| | | BOOST_CHECK(tmp.pop(d)); |
| | | |
| | | NamedShmObject<Rcb> rcb(shm, "test_rcb", eOpenOrCreate); |
| | | bool try_more = true; |
| | | |
| | | auto Writer = [&]() { |
| | | uint64_t n = 0; |
| | | while ((n = nwrite++) < nmsg) { |
| | | while (!rcb->push_back(n, try_more)) { |
| | | while (!rcb->push(n, try_more)) { |
| | | // MySleep(); |
| | | } |
| | | ++writedone; |
| | |
| | | auto Reader = [&]() { |
| | | while (nread.load() < nmsg) { |
| | | int64_t d; |
| | | if (rcb->pop_front(d, try_more)) { |
| | | if (rcb->pop(d, try_more)) { |
| | | ++nread; |
| | | total += d; |
| | | } else { |
| | |
| | | { |
| | | ThreadManager threads; |
| | | boost::timer::auto_cpu_timer timer; |
| | | printf("Testing Robust Buffer, msgs %ld, queue size: %d, threads: %d \n", nmsg, Rcb::capacity, nthread); |
| | | // printf("Testing Robust Buffer, msgs %ld, queue size: %d, threads: %d \n", nmsg, Rcb::capacity, nthread); |
| | | printf("Testing Robust Buffer, msgs %ld, queue size: %d, threads: %d \n", nmsg, 16, nthread); |
| | | for (int i = 0; i < nthread; ++i) { |
| | | threads.Launch(Reader); |
| | | threads.Launch(Writer); |
| | |
| | | printf("total: %ld, expected: %ld\n", total.load(), correct_total); |
| | | BOOST_CHECK_EQUAL(total.load(), correct_total); |
| | | } |
| | | |
| | | BOOST_AUTO_TEST_CASE(MutexTest) |
| | | { |
| | | typedef robust::Mutex RobustMutex; |
| | | |
| | | for (int i = 0; i < 20; ++i) { |
| | | int size = i; |
| | | int left = size & 7; |
| | | int rsize = size + ((8 - left) & 7); |
| | | printf("size: %3d, rsize: %3d\n", size, rsize); |
| | | } |
| | | SharedMemory &shm = TestShm(); |
| | | // shm.Remove(); |
| | | // return; |
| | | GlobalInit(shm); |
| | | |
| | | const std::string mtx_name("test_mutex"); |
| | | const std::string int_name("test_int"); |
| | | auto mtx = shm.FindOrCreate<RobustMutex>(mtx_name); |
| | | auto pi = shm.FindOrCreate<int>(int_name, 100); |
| | | |
| | | std::mutex m; |
| | | typedef std::chrono::steady_clock Clock; |
| | | auto Now = []() { return Clock::now().time_since_epoch(); }; |
| | | if (pi) { |
| | | auto old = *pi; |
| | | printf("int : %d, add1: %d\n", old, ++*pi); |
| | | } |
| | | |
| | | { |
| | | const int ntimes = 1000 * 1000; |
| | | RobustMutex mutex; |
| | | auto Lock = [&]() { |
| | | for (int i = 0; i < ntimes; ++i) { |
| | | mutex.lock(); |
| | | mutex.unlock(); |
| | | } |
| | | }; |
| | | |
| | | { |
| | | boost::timer::auto_cpu_timer timer; |
| | | printf("test lock/unlock %d times: ", ntimes); |
| | | Lock(); |
| | | } |
| | | { |
| | | boost::timer::auto_cpu_timer timer; |
| | | printf("test lock/unlock %d times, 2 thread: ", ntimes); |
| | | std::thread t1(Lock), t2(Lock); |
| | | t1.join(); |
| | | t2.join(); |
| | | } |
| | | } |
| | | |
| | | auto TryLock = [&]() { |
| | | if (mtx->try_lock()) { |
| | | printf("try_lock ok\n"); |
| | | return true; |
| | | } else { |
| | | printf("try_lock failed\n"); |
| | | return false; |
| | | } |
| | | }; |
| | | auto Unlock = [&]() { |
| | | mtx->unlock(); |
| | | printf("unlocked\n"); |
| | | }; |
| | | |
| | | if (mtx) { |
| | | printf("mtx exists\n"); |
| | | if (TryLock()) { |
| | | auto op = [&]() { |
| | | if (TryLock()) { |
| | | Unlock(); |
| | | } |
| | | }; |
| | | op(); |
| | | std::thread t(op); |
| | | t.join(); |
| | | // Unlock(); |
| | | } else { |
| | | // mtx->unlock(); |
| | | } |
| | | } else { |
| | | printf("mtx not exists\n"); |
| | | } |
| | | } |