lichao
2021-03-30 3f8ae6cf4f03be83f16846af32d73dd89b937c40
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <string>
#include <vector>
#include <thread>
#include <atomic>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "pubsub.h"
#include "defs.h"
#include "util.h"
 
template <class A, class B> struct IsSameType { static const bool value = false; };
template <class A> struct IsSameType<A,A> { static const bool value = true; };
 
 
BOOST_AUTO_TEST_CASE(Temp)
{
    std::string topics[] = {
        "",
        ".",
        "a",
        "sp",
        "sport",
        "sport.",
        "sport.a",
        "sport.a.b.c",
        "sport.ab.c",
        "sport.basketball",
        "sport.football",
    };
    const char sep = '.';
    auto Adjust = [&](const std::string &user_topic) {
        if (user_topic.empty() || user_topic.back() == sep) {
            return user_topic;
        } else {
            return user_topic + sep;
        }
    };
 
    for (auto &t : topics) {
        const std::string &a = Adjust(t);
        printf("orig: %20s   adjusted: %20s   parts:[", ("'" + t + "'").c_str(), ('\'' + a + '\'').c_str());
 
        size_t pos = 0;
        while (true) {
            auto &topic = t;
            pos = topic.find(kTopicSep, pos);
            if (pos == topic.npos || ++pos == topic.size()) {
                // Find1(std::string()); // sub all.
                break;
            } else {
                printf("'%s',", topic.substr(0, pos).c_str());
            }
        }
        printf("]\n");
    }
}
 
BOOST_AUTO_TEST_CASE(PubSubTest)
{
    const std::string shm_name("ShmPubSub");
    ShmRemover auto_remove(shm_name); //remove twice? in case of killed?
    SharedMemory shm(shm_name, 1024*1024*50);
    auto Avail = [&]() { return shm.get_free_memory(); };
    auto init_avail = Avail();
 
    BusManager bus(shm);
    bus.Start(1);
    std::this_thread::sleep_for(100ms);
 
    std::atomic<uint64_t> count(0);
    std::atomic<ptime> last_time(Now() - seconds(1));
    std::atomic<uint64_t> last_count(0);
 
    const uint64_t nmsg = 100;
 
    const int timeout = 1000;
    auto Sub = [&](int id, const std::vector<std::string> &topics) {
        ShmMsgQueue client(shm, 8);
        client.Send(kBHBusQueueId, MakeSub(client.Id(), topics), timeout);
        for (int i = 0; i < nmsg * topics.size(); ++i) {
            BHMsg msg;
            if (client.Recv(msg, 1000)) {
                if (msg.type() != kMsgTypePublish) {
                    BOOST_CHECK(false);
                }
                DataPub pub;
                if (!pub.ParseFromString(msg.body())) {
                    BOOST_CHECK(false);
                }
                ++count;
                auto cur = Now();
                if (last_time.exchange(cur) < cur) {
                    std::cout << "time: " << cur;
                    printf("sub recv, total msg:%10ld, speed:[%8ld/s], used mem:%8ld \n",
                           count.load(), count - last_count.exchange(count), init_avail - Avail());
                }
                // printf("sub %2d recv: %s/%s\n", id, pub.topic().c_str(), pub.data().c_str());
            } else {
                printf("sub %2d recv timeout\n", id);
            }
 
        }
    };
    auto Pub = [&](const std::string &topic) {
        ShmMsgQueue provider(shm, 0);
        for (int i = 0; i < nmsg; ++i) {
            std::string data = topic + std::to_string(i) + std::string(1000, '-');
 
            MsgI msg;
            msg.MakeRC(shm, MakePub(topic, data.data(), data.size()));
            DEFER1(msg.Release(shm));
            bool r = provider.Send(kBHBusQueueId, msg, timeout);
 
            // bool r = provider.Send(kBHBusQueueId, MakePub(topic, data.data(), data.size()), timeout);
            if (!r) {
                printf("pub ret: %s\n", r ? "ok" : "fail");
            }
        }
    };
    ThreadManager threads;
    typedef std::vector<std::string> Topics;
    Topics topics;
    for (int i = 0; i < 100; ++i) {
        topics.push_back("t" + std::to_string(i));
    }
    Topics part;
    for (int i = 0; i < topics.size(); ++i) {
        part.push_back(topics[i]);
        threads.Launch(Sub, i, topics);
    }
    std::this_thread::sleep_for(100ms);
    for (auto &topic: topics) {
        threads.Launch(Pub, topic);
    }
    threads.Launch(Pub, "some_else");
 
    threads.WaitAll();
    std::cout << "end : " << Now();
    printf("sub recv, total msg:%10ld, speed:[%8ld/s], used mem:%8ld \n",
           count.load(), count - last_count.exchange(count), init_avail - Avail());
 
    bus.Stop();
}
 
inline int MyMin(int a, int b) {
    printf("MyMin\n");
    return a < b ? a : b;
}
int test_main(int argc, char *argv[])
{
    printf("test main\n");
    int a = 0;
    int b = 0;
    BOOST_CHECK_EQUAL(a, b);
    int n = MyMin(4,6);
    for (int i = 0; i < n; ++i) {
        printf("i = %d\n", i);
    }
 
    return 0;
}