Skip to content

Commit

Permalink
add test for pinpoint-apm#562
Browse files Browse the repository at this point in the history
  • Loading branch information
eeliu committed Nov 23, 2023
1 parent 9ec1bde commit 7bd93c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
4 changes: 4 additions & 0 deletions common/test/test_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ TEST(common, start_end_trace) {

id = pinpoint_end_trace(id);
EXPECT_EQ(id, 0);
mark_current_trace_status(-1024, E_TRACE_BLOCK);
mark_current_trace_status(1024, E_TRACE_BLOCK);
catch_error(-1024, "sdfasfas", "fsafdsfasd", 234);
catch_error(0, "sdfasfas", "fsafdsfasd", 234);
}

TEST(common, context_check) {
Expand Down
98 changes: 49 additions & 49 deletions common/test/test_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ std::mutex cv_m;
std::condition_variable cv;
NodeID rootId = E_ROOT_NODE;

int usedNode() { return PoolManager::getInstance().totoalNodesCount() - PoolManager::getInstance().freeNodesCount(); }
int usedNode() {
return PoolManager::getInstance().totoalNodesCount() -
PoolManager::getInstance().freeNodesCount();
}

// note: as it known, there may leak some node
void func()
{
void func() {
std::unique_lock<std::mutex> lk(cv_m);
cv.wait(lk);
pinpoint_add_clues(rootId, "xxxx", "bbbbbbss", E_LOC_CURRENT);
Expand All @@ -45,26 +47,28 @@ void func()
pinpoint_add_clue(rootId, "xxx", "bbbbbb", E_LOC_CURRENT);
}

TEST(node, multipleThread)
{
TEST(node, multipleThread) {
// no crash, works fine
NodeID root = pinpoint_start_trace(E_ROOT_NODE);

std::vector<std::thread> threads;

for (int i = 0; i < 10; i++) { threads.push_back(std::thread(func)); }
for (int i = 0; i < 10; i++) {
threads.push_back(std::thread(func));
}

sleep(2);
cv.notify_all();

for (int i = 0; i < 10; i++) { threads[i].join(); }
for (int i = 0; i < 10; i++) {
threads[i].join();
}
pinpoint_end_trace(root);
pinpoint_end_trace(root);
// EXPECT_TRUE(PoolManager::getInstance().NoNodeLeak());
}

TEST(node, wakeTrace)
{
TEST(node, wakeTrace) {
NodeID root = pinpoint_start_trace(E_ROOT_NODE);
NodeID child1 = pinpoint_start_trace(root);

Expand All @@ -82,17 +86,15 @@ TEST(node, wakeTrace)
// EXPECT_TRUE(PoolManager::getInstance().NoNodeLeak());
}

void test_opt(TraceNode &node, const char *opt, ...)
{
void test_opt(TraceNode& node, const char* opt, ...) {
va_list args;
va_start(args, opt);
node.setOpt(opt, &args);
va_end(args);
}

TEST(node, opt)
{
TraceNode &node = PoolManager::getInstance().Take();
TEST(node, opt) {
TraceNode& node = PoolManager::getInstance().Take();

test_opt(node, "TraceMinTimeMs:23", "TraceOnlyException", nullptr);

Expand All @@ -112,10 +114,9 @@ TEST(node, opt)
}

static std::string span;
void capture(const char *msg) { span = msg; }
void capture(const char* msg) { span = msg; }
//./bin/TestCommon --gtest_filter=node.pinpoint_start_traceV1
TEST(node, pinpoint_start_traceV1)
{
TEST(node, pinpoint_start_traceV1) {
auto count = usedNode();
register_span_handler(capture);
NodeID root, child1;
Expand All @@ -124,7 +125,9 @@ TEST(node, pinpoint_start_traceV1)
pinpoint_add_clue(child1, "name", "Take1sec", E_LOC_CURRENT);
sleep(1);
pinpoint_end_trace(child1);

check_tracelimit(-1);
check_tracelimit(0);
check_tracelimit(time(nullptr));
child1 = pinpoint_start_traceV1(root, "TraceOnlyException", nullptr);
pinpoint_add_clue(child1, "name", "Exception", E_LOC_CURRENT);
pinpoint_add_exception(child1, "xxxxxxxxx");
Expand Down Expand Up @@ -168,8 +171,7 @@ TEST(node, pinpoint_start_traceV1)
EXPECT_EQ(count, usedNode());
}

TEST(node, leak_node)
{
TEST(node, leak_node) {
auto count = PoolManager::getInstance().freeNodesCount();
NodeID root, child1, child2;
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -185,17 +187,17 @@ TEST(node, leak_node)
show_status();
}

TEST(node, tons_of_nodes_01)
{
auto count = usedNode(); // PoolManager::getInstance().totoalNodesCount() - PoolManager::getInstance().freeNodesCount();
TEST(node, tons_of_nodes_01) {
auto count = usedNode(); // PoolManager::getInstance().totoalNodesCount() -
// PoolManager::getInstance().freeNodesCount();
NodeID root = pinpoint_start_trace(E_ROOT_NODE);
for (int i = 0; i < 1000; i++) {
NodeID child1 = pinpoint_start_trace(root);
pinpoint_end_trace(child1);
}
pinpoint_end_trace(root);

EXPECT_EQ(count, usedNode()); //);
EXPECT_EQ(count, usedNode()); //);

count = usedNode();
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -212,23 +214,24 @@ TEST(node, tons_of_nodes_01)
EXPECT_EQ(count, usedNode());
}

TEST(node, tons_of_nodes_2k)
{
auto count = usedNode(); // PoolManager::getInstance().totoalNodesCount() - PoolManager::getInstance().freeNodesCount();
TEST(node, tons_of_nodes_2k) {
auto count = usedNode(); // PoolManager::getInstance().totoalNodesCount() -
// PoolManager::getInstance().freeNodesCount();
NodeID root = pinpoint_start_trace(E_ROOT_NODE);
NodeID next = root;
for (int i = 0; i < 2000; i++) {
NodeID child = pinpoint_start_trace(next);
pinpoint_end_trace(child);
if (i % 2 == 0) { next = child; }
if (i % 2 == 0) {
next = child;
}
}
pinpoint_end_trace(root);

EXPECT_EQ(count, usedNode());
}

TEST(node, tons_of_nodes_leak)
{
TEST(node, tons_of_nodes_leak) {
auto count = usedNode();

NodeID root, child_1, child_2;
Expand All @@ -244,8 +247,7 @@ TEST(node, tons_of_nodes_leak)
EXPECT_EQ(count, usedNode());
}

TEST(node, tons_of_nodes_free_all)
{
TEST(node, tons_of_nodes_free_all) {
auto count = usedNode();

NodeID root, child_1, child_2;
Expand All @@ -261,8 +263,7 @@ TEST(node, tons_of_nodes_free_all)
EXPECT_EQ(count, usedNode());
}
//./bin/TestCommon --gtest_filter=node.free_when_add
TEST(node, free_when_add)
{
TEST(node, free_when_add) {
auto count = usedNode();
NodeID root;
auto make_it_busy = [&]() {
Expand All @@ -287,8 +288,7 @@ TEST(node, free_when_add)
}

//./bin/TestCommon --gtest_filter=node.orphan_node
TEST(node, orphan_node)
{
TEST(node, orphan_node) {
auto count = usedNode();
NodeID root, child_1, orphan;
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -307,8 +307,7 @@ TEST(node, orphan_node)
EXPECT_EQ(count, usedNode());
}
//./bin/TestCommon --gtest_filter=node.orphan_node_01
TEST(node, orphan_node_01)
{
TEST(node, orphan_node_01) {
auto count = usedNode();
NodeID root, child_1, orphan;
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -322,8 +321,7 @@ TEST(node, orphan_node_01)
EXPECT_EQ(count, usedNode());
}
//./bin/TestCommon --gtest_filter=node.orphan_root_parent_end
TEST(node, orphan_parent_root_end)
{
TEST(node, orphan_parent_root_end) {
auto count = usedNode();
NodeID root, child_1, orphan;
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -337,8 +335,7 @@ TEST(node, orphan_parent_root_end)
EXPECT_EQ(count, usedNode());
}

TEST(node, orphan_root_parent_end)
{
TEST(node, orphan_root_parent_end) {
auto count = usedNode();
NodeID root, child_1, orphan;
root = pinpoint_start_trace(E_ROOT_NODE);
Expand All @@ -352,12 +349,13 @@ TEST(node, orphan_root_parent_end)
EXPECT_EQ(count, usedNode());
}
// ./bin/TestCommon --gtest_filter=node.end_trace_in_mt
TEST(node, end_trace_in_mt)
{
TEST(node, end_trace_in_mt) {
auto count = usedNode();
NodeID root = pinpoint_start_trace(E_ROOT_NODE);
NodeID next = root;
// limit size 100; due to https://github.com/pinpoint-apm/pinpoint-c-agent/runs/6806024797?check_suite_focus=true bus error under macos
// limit size 100; due to
// https://github.com/pinpoint-apm/pinpoint-c-agent/runs/6806024797?check_suite_focus=true bus
// error under macos
for (int i = 0; i < 100; i++) {
next = pinpoint_start_trace(next);
pinpoint_end_trace(next);
Expand All @@ -380,17 +378,19 @@ TEST(node, end_trace_in_mt)
// wait for all threads running
sleep(2);
cv.notify_all();
for (auto &thread : threads) thread.join();
for (auto& thread : threads)
thread.join();
EXPECT_EQ(count, usedNode());
}
// ./bin/TestCommon --gtest_filter=node.max_sub_nodes
TEST(node, max_sub_nodes)
{
TEST(node, max_sub_nodes) {
auto count = usedNode();
NodeID root = pinpoint_start_trace(E_ROOT_NODE);
while (true) {
NodeID next = pinpoint_start_trace(root);
if (next == E_INVALID_NODE) { break; }
if (next == E_INVALID_NODE) {
break;
}
pinpoint_end_trace(next);
}
pinpoint_end_trace(root);
Expand Down
1 change: 1 addition & 0 deletions plugins/PY/pinpointPy/Fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async def read_main(name, request: Request):
def test_request_example(self):
response = self.client.get("/cluster/abc")
assert "ut" in response.headers
self.assertEqual(response.headers['ut'], "/cluster/{name}")


if __name__ == '__main__':
Expand Down

0 comments on commit 7bd93c4

Please sign in to comment.