MCPcopy Create free account
hub / github.com/apache/brpc / TEST

Function TEST

test/bthread_cond_unittest.cpp:66–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66TEST(CondTest, sanity) {
67 Arg a;
68 ASSERT_EQ(0, bthread_mutex_init(&a.m, NULL));
69 ASSERT_EQ(0, bthread_cond_init(&a.c, NULL));
70 // has no effect
71 ASSERT_EQ(0, bthread_cond_signal(&a.c));
72
73 stop = false;
74 wake_tid.resize(1024);
75 wake_tid.clear();
76 wake_time.resize(1024);
77 wake_time.clear();
78
79 bthread_t wth[8];
80 const size_t NW = ARRAY_SIZE(wth);
81 for (size_t i = 0; i < NW; ++i) {
82 ASSERT_EQ(0, bthread_start_urgent(&wth[i], NULL, waiter, &a));
83 }
84
85 bthread_t sth;
86 ASSERT_EQ(0, bthread_start_urgent(&sth, NULL, signaler, &a));
87
88 bthread_usleep(SIGNAL_INTERVAL_US * 200);
89
90 pthread_mutex_lock(&wake_mutex);
91 const size_t nbeforestop = wake_time.size();
92 pthread_mutex_unlock(&wake_mutex);
93
94 stop = true;
95 for (size_t i = 0; i < NW; ++i) {
96 bthread_cond_signal(&a.c);
97 }
98
99 bthread_join(sth, NULL);
100 for (size_t i = 0; i < NW; ++i) {
101 bthread_join(wth[i], NULL);
102 }
103
104 printf("wake up for %lu times\n", wake_tid.size());
105
106 // Check timing
107 long square_sum = 0;
108 for (size_t i = 0; i < nbeforestop; ++i) {
109 long last_time = (i ? wake_time[i-1] : signal_start_time);
110 long delta = wake_time[i] - last_time - SIGNAL_INTERVAL_US;
111 EXPECT_GT(wake_time[i], last_time);
112 square_sum += delta * delta;
113 EXPECT_LT(labs(delta), 10000L) << "error[" << i << "]=" << delta << "="
114 << wake_time[i] << " - " << last_time;
115 }
116 printf("Average error is %fus\n", sqrt(square_sum / std::max(nbeforestop, 1UL)));
117
118 // Check fairness
119 std::map<bthread_t, int> count;
120 for (size_t i = 0; i < wake_tid.size(); ++i) {
121 ++count[wake_tid[i]];
122 }
123 EXPECT_EQ(NW, count.size());

Callers

nothing calls this directly

Calls 15

bthread_mutex_initFunction · 0.85
bthread_cond_initFunction · 0.85
bthread_cond_signalFunction · 0.85
bthread_start_urgentFunction · 0.85
bthread_usleepFunction · 0.85
pthread_mutex_lockFunction · 0.85
pthread_mutex_unlockFunction · 0.85
bthread_joinFunction · 0.85
bthread_cond_destroyFunction · 0.85
bthread_mutex_destroyFunction · 0.85
ProfilerStartFunction · 0.85
ProfilerStopFunction · 0.85

Tested by

no test coverage detected