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;
@@ -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,6 +153,7 @@
    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;
@@ -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[])