lichao
2021-04-28 a6f67b4249525089fb97eb9418c7014f66c2a000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * =====================================================================================
 *
 *       Filename:  simple_tests.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月30日 11时29分40秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
 
#include "util.h"
 
struct s1000 {
    char a[1000];
};
 
BOOST_AUTO_TEST_CASE(BasicTest)
{
    const std::string shm_name("basic");
    ShmRemover auto_remove(shm_name);
    SharedMemory shm(shm_name, 1024 * 1024 * 10);
    auto Avail = [&]() { return shm.get_free_memory(); };
 
    offset_ptr<const void> p;
    BOOST_CHECK(!p);
    BOOST_CHECK(p.get() == 0);
    p = 0;
    BOOST_CHECK(!p);
    BOOST_CHECK(p.get() == 0);
    const char *str = "basic";
    p = str;
    BOOST_CHECK(p);
    BOOST_CHECK(p.get() == str);
    p = 0;
    BOOST_CHECK(!p);
    BOOST_CHECK(p.get() == 0);
 
    auto init_avail = Avail();
 
    auto BasicTest = [&](int tid, int nloop) {
        auto Code = [&](int id) {
            typedef ShmObject<s1000> Int;
            std::string name = std::to_string(id);
            auto a0 = Avail();
            Int i1(shm, name);
            auto a1 = Avail();
            BOOST_CHECK_LT(a1, a0);
            printf("s1000 size: %ld\n", a0 - a1);
            i1->a[0] = 5;
            Int i2(shm, name);
            auto a2 = Avail();
            BOOST_CHECK_EQUAL(a1, a2);
            BOOST_CHECK_EQUAL(i1.data(), i2.data());
            int i = i1.Remove();
            BOOST_CHECK_EQUAL(Avail(), a0);
 
            {
                auto old = Avail();
                void *p = shm.Alloc(1024);
                shm.Dealloc(p);
                BOOST_CHECK_EQUAL(old, Avail());
            }
 
            bool r = shared_memory_object::remove(shm_name.c_str());
            BOOST_CHECK(r);
        };
        for (int i = 0; i < nloop; ++i) {
            Code(i + tid * nloop);
        }
    };
 
    // boost::timer::auto_cpu_timer timer;
    ThreadManager threads;
    int nthread = 1;
    int nloop = 1;
    for (int i = 0; i < nthread; ++i) {
        threads.Launch(BasicTest, i, nloop);
    }
    BOOST_CHECK_EQUAL(init_avail, Avail());
}
 
BOOST_AUTO_TEST_CASE(ForkTest)
{
    ProcessManager procs;
    const int nproc = 10;
 
    printf("Testing fork:\n");
 
    auto child = [&](int id) {
        std::this_thread::sleep_for(100ms * id);
        printf("child id: %3d/%d ends\r", id, nproc);
    };
 
    for (int i = 0; i < nproc; ++i) {
        procs.Launch(child, i + 1);
    }
}
 
BOOST_AUTO_TEST_CASE(TimedWaitTest)
{
    SharedMemory &shm = TestShm();
    GlobalInit(shm);
    ShmMsgQueue q(shm, 64);
    for (int i = 0; i < 2; ++i) {
        int ms = i * 100;
        printf("Timeout Test %4d: ", ms);
        boost::timer::auto_cpu_timer timer;
        MsgI msg;
        bool r = q.Recv(msg, ms);
        BOOST_CHECK(!r);
    }
}
 
BOOST_AUTO_TEST_CASE(RefCountTest)
{
    SharedMemory &shm = TestShm();
    typedef MsgI Msg;
    GlobalInit(shm);
 
    Msg m0(1000);
    BOOST_CHECK(m0.valid());
    BOOST_CHECK_EQUAL(m0.Count(), 1);
    Msg m1 = m0;
    BOOST_CHECK(m1.valid());
    BOOST_CHECK_EQUAL(m1.AddRef(), 2);
    BOOST_CHECK_EQUAL(m0.AddRef(), 3);
    BOOST_CHECK_EQUAL(m0.Release(), 2);
    BOOST_CHECK_EQUAL(m0.Release(), 1);
    BOOST_CHECK_EQUAL(m1.Release(), 0);
    BOOST_CHECK(!m1.valid());
}