| | |
| | | |
| | | ///////////////////////////////////////////////////////////////////////////////////////// |
| | | |
| | | BOOST_AUTO_TEST_CASE(InitTest) |
| | | { |
| | | AtomicReqRep rr; |
| | | auto client = [&]() { |
| | | for (int i = 0; i < 20; ++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) |
| | | { |
| | | const int nthread = 100; |
| | |
| | | std::atomic<uint64_t> nwrite(0); |
| | | std::atomic<uint64_t> writedone(0); |
| | | |
| | | #if 0 |
| | | typedef AtomicQueue<4> Rcb; |
| | | #if 1 |
| | | const int kPower = 0; |
| | | typedef AtomicQueue<kPower> Rcb; |
| | | |
| | | Rcb tmp; |
| | | BOOST_CHECK(tmp.like_empty()); |
| | | // BOOST_CHECK(tmp.like_empty()); |
| | | BOOST_CHECK(tmp.push(1)); |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | if (kPower != 0) { |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | } |
| | | BOOST_CHECK(tmp.head() == 0); |
| | | int64_t d; |
| | | BOOST_CHECK(tmp.pop(d)); |
| | | BOOST_CHECK(tmp.like_empty()); |
| | | BOOST_CHECK(tmp.head() == 1); |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | if (kPower != 0) { |
| | | // BOOST_CHECK(tmp.like_empty()); |
| | | BOOST_CHECK(tmp.head() == 1); |
| | | BOOST_CHECK(tmp.tail() == 1); |
| | | } |
| | | |
| | | ShmObject<Rcb> rcb(shm, "test_rcb"); |
| | | bool try_more = true; |
| | |
| | | BOOST_AUTO_TEST_CASE(MutexTest) |
| | | { |
| | | { |
| | | int fd = open("/tmp/test_fmutex", O_CREAT | O_RDONLY, 0666); |
| | | flock(fd, LOCK_EX); |
| | | printf("lock 1"); |
| | | int sem_id = semget(100, 1, 0666 | IPC_CREAT); |
| | | auto P = [&]() { |
| | | sembuf op = {0, -1, SEM_UNDO}; |
| | | semop(sem_id, &op, 1); |
| | | }; |
| | | auto V = [&]() { |
| | | sembuf op = {0, 1, SEM_UNDO}; |
| | | semop(sem_id, &op, 1); |
| | | }; |
| | | for (int i = 0; i < 10; ++i) { |
| | | V(); |
| | | } |
| | | Sleep(10s); |
| | | flock(fd, LOCK_EX); |
| | | printf("lock 2"); |
| | | Sleep(10s); |
| | | flock(fd, LOCK_UN); |
| | | printf("un lock 2"); |
| | | Sleep(10s); |
| | | flock(fd, LOCK_UN); |
| | | printf("un lock 1"); |
| | | |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | 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); |