lichao
2021-03-24 6f9521a6dca494a9f9644d1ccacdee23744dc0e5
utest/utest.cpp
@@ -1,5 +1,5 @@
#include <stdio.h>
#include "shm.h"
#include "../src/shm.h"
#include "../src/bh_util.h"
#include <string>
#include <vector>
@@ -8,9 +8,10 @@
#include <atomic>
#include <boost/noncopyable.hpp>
#include <boost/timer/timer.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/microsec_time_clock.hpp>
#include <boost/uuid/uuid_generators.hpp>
using namespace std::chrono_literals;
using namespace bhome_shm;
@@ -109,7 +110,7 @@
    const std::string shm_name("shm_wait");
    ShmRemover auto_remove(shm_name);
    SharedMemory shm(shm_name, 1024*1024);
    ShmMsgQueue q(10000, shm, 64);
    ShmMsgQueue q(shm, 64);
    for (int i = 0; i < 5; ++i) {
        int ms = i * 100;
        printf("Timeout Test %d: ", ms);
@@ -122,6 +123,29 @@
    }
}
BOOST_AUTO_TEST_CASE(MsgHeader)
{
    MsgMetaV1 head;
    BOOST_CHECK_EQUAL(head.self_size_, sizeof(head));
    BOOST_CHECK_EQUAL(head.type_, kMsgTypeNormal);
    BOOST_CHECK_EQUAL(head.tag_, kMsgMetaTag);
    BOOST_CHECK_EQUAL(head.data_size_, 0);
    BOOST_CHECK_EQUAL(head.src_id_[5], 0);
    head.data_size_ = 100;
    auto rand_id = boost::uuids::random_generator()();
    memcpy(head.src_id_, &rand_id, sizeof(rand_id));
    head.type_ = 123;
    BOOST_CHECK_EQUAL(sizeof(head.src_id_), sizeof(rand_id));
    char buf[100] = {0};
    head.Pack(buf);
    MsgMetaV1 result;
    result.Parse(buf);
    BOOST_CHECK_EQUAL(memcmp(&head, &result, sizeof(head)), 0);
}
BOOST_AUTO_TEST_CASE(RequestReply)
{
    const std::string shm_name("ShmReqRep");
@@ -129,11 +153,12 @@
    SharedMemory shm(shm_name, 1024*1024*50);
    auto Avail = [&]() { return shm.get_free_memory(); };
    auto init_avail = Avail();
    // DEFER1(BOOST_CHECK_EQUAL(init_avail, Avail()); printf("Request Reply Test shm No Leak.\n"););
    auto f0 = init_avail;
    const int qlen = 64;
    ShmMsgQueue srv(1, shm, qlen);
    ShmMsgQueue cli(2, shm, qlen);
    ShmMsgQueue srv(shm, qlen);
    ShmMsgQueue cli(shm, qlen);
    auto f1= shm.get_free_memory();
    const size_t msg_length = 1000;
@@ -148,11 +173,11 @@
    auto Client = [&](int tid, int nmsg){
        for (int i = 0; i < nmsg; ++i) {
            if (!cli.Send(srv.id(), msg_content.data(), msg_content.size(), 1000)) {
            if (!cli.Send(srv.Id(), msg_content.data(), msg_content.size(), 1000)) {
                printf("********** client send error.\n");
                continue;
            }
            MQId id = 0;
            MQId id;
            void *data = 0;
            size_t size = 0;
            if (!cli.Recv(id, data, size, 1000)) {
@@ -177,7 +202,7 @@
    auto Server = [&](){
        void *data = 0;
        size_t size = 0;
        MQId src_id = 0;
        MQId src_id;
        while (!stop) {
            if (srv.Recv(src_id, data, size, 100)) {
                DEFER1(free(data));
@@ -203,11 +228,6 @@
    printf("request ok: %ld\n", count.load());
    stop = true;
    servers.WaitAll();
    srv.Remove();
    cli.Remove();
    BOOST_CHECK_EQUAL(init_avail, Avail());
    printf("Request Reply Test shm No Leak.\n");
}
int test_main(int argc, char *argv[])