Skip to content

Commit 74421b7

Browse files
committed
include review comments, will be squashed
1 parent 77fb904 commit 74421b7

File tree

1 file changed

+98
-95
lines changed

1 file changed

+98
-95
lines changed

auto_tests/toxav_mt_test.cc

Lines changed: 98 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ using ToxAV_Ptr = std::unique_ptr<ToxAV, ToxAV_Deleter>;
3838
static void t_accept_friend_request_cb(Tox *m, const uint8_t *public_key, const uint8_t *data,
3939
size_t length, void *userdata) {
4040
if (length == 7 && std::memcmp("gentoo", data, 7) == 0) {
41-
ck_assert(tox_friend_add_norequest(m, public_key, nullptr) != (uint32_t)-1);
41+
Tox_Err_Friend_Add err;
42+
tox_friend_add_norequest(m, public_key, &err);
43+
ck_assert(err == TOX_ERR_FRIEND_ADD_OK);
44+
} else {
45+
// No other request expected
46+
ck_assert(false);
4247
}
4348
}
4449

45-
std::vector<Tox_Ptr> prepareNetwork(uint32_t count) {
50+
std::vector<Tox_Ptr> prepare_network(uint32_t count) {
4651
Tox_Err_New error;
4752

4853
// Temporary bootstrap node
49-
std::printf("Created 1 instance of Tox as boostrap node\n");
54+
std::printf("Created 1 instance of Tox as bootstrap node\n");
5055
Tox_Ptr bootstrap = Tox_Ptr(tox_new_log(nullptr, &error, nullptr));
5156
ck_assert(error == TOX_ERR_NEW_OK);
5257

@@ -81,12 +86,12 @@ std::vector<Tox_Ptr> prepareNetwork(uint32_t count) {
8186
// temporarily add bootstrap node to end of toxes, so we can iterate all
8287
toxes.push_back(std::move(bootstrap));
8388

84-
uint32_t bootstrap_iteration = 0;
8589
bool online = false;
8690

8791
auto bootstrap_start_time = Clock::now();
8892

89-
for (; bootstrap_iteration < MAX_BOOTSTRAP_ITERATIONS; ++bootstrap_iteration) {
93+
uint32_t bootstrap_iteration;
94+
for (bootstrap_iteration = 0; bootstrap_iteration < MAX_BOOTSTRAP_ITERATIONS; ++bootstrap_iteration) {
9095
for (auto &tox : toxes) {
9196
tox_iterate(tox.get(), nullptr);
9297
}
@@ -140,12 +145,12 @@ class AV_State {
140145
public:
141146
explicit AV_State(Tox_Ptr tox, std::string name, bool combined = false)
142147
: tox_(std::move(tox)),
143-
combined_(combined),
144-
stop_threads{false},
145-
incomming{false},
146-
call_state{0},
147-
video_received{false},
148-
audio_received{false},
148+
combined_av_(combined),
149+
stop_threads_{false},
150+
incomming_{false},
151+
call_state_{0},
152+
video_received_{false},
153+
audio_received_{false},
149154
name_{name} {
150155
Toxav_Err_New error;
151156
toxAV_ = ToxAV_Ptr(toxav_new(tox_.get(), &error));
@@ -156,40 +161,40 @@ class AV_State {
156161
toxav_callback_video_receive_frame(toxAV_.get(), &AV_State::toxav_receive_video_frame_cb, this);
157162
toxav_callback_audio_receive_frame(toxAV_.get(), &AV_State::toxav_receive_audio_frame_cb, this);
158163

159-
tox_thread = std::thread(&AV_State::tox_iterator, this);
164+
tox_thread_ = std::thread(&AV_State::tox_iterator, this);
160165

161166
if (combined) {
162-
av_thread = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_BOTH);
167+
av_thread_ = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_BOTH);
163168
} else {
164-
audio_thread = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_AUDIO);
165-
video_thread = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_VIDEO);
169+
audio_thread_ = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_AUDIO);
170+
video_thread_ = std::thread(&AV_State::toxav_iterator, this, Iteration_Type::TOXAV_VIDEO);
166171
}
167172
}
168173

169-
~AV_State() { stopThreads(); }
174+
~AV_State() { stop_threads(); }
170175

171-
ToxAV *getToxAV() const { return toxAV_.get(); }
172-
std::mutex &getToxLoopLock() { return tox_loop_lock; }
173-
bool inCall() const { return in_call.load(); }
174-
uint32_t getCallState() const { return call_state.load(); }
175-
void stopThreads() {
176-
if (stop_threads.exchange(true)) {
176+
ToxAV *get_ToxAV() const { return toxAV_.get(); }
177+
std::mutex &get_tox_loop_lock() { return tox_loop_lock_; }
178+
bool in_call() const { return in_call_.load(); }
179+
uint32_t get_call_state() const { return call_state_.load(); }
180+
void stop_threads() {
181+
if (stop_threads_.exchange(true)) {
177182
// already stopped
178183
return;
179184
}
180185

181-
tox_thread.join();
186+
tox_thread_.join();
182187

183-
if (combined_) {
184-
av_thread.join();
188+
if (combined_av_) {
189+
av_thread_.join();
185190
} else {
186-
audio_thread.join();
187-
video_thread.join();
191+
audio_thread_.join();
192+
video_thread_.join();
188193
}
189194
}
190195

191-
bool didReceiveAudio() const { return audio_received.load(); }
192-
bool didReceiveVideo() const { return video_received.load(); }
196+
bool did_receive_audio() const { return audio_received_.load(); }
197+
bool did_receive_video() const { return video_received_.load(); }
193198

194199
static constexpr uint32_t TEST_A_BITRATE = 48; // In kbit/s
195200
static constexpr uint32_t TEST_V_BITRATE = 4000; // In kbit/s
@@ -206,22 +211,22 @@ class AV_State {
206211
bool video_enabled, void *user_data) {
207212
AV_State *me = static_cast<AV_State *>(user_data);
208213
std::cout << "[" << me->name_ << "] Handling CALL callback" << std::endl;
209-
me->incomming.store(true);
210-
ck_assert(std::this_thread::get_id() == me->tox_thread.get_id());
214+
me->incomming_.store(true);
215+
ck_assert(std::this_thread::get_id() == me->tox_thread_.get_id());
211216
}
212217

213218
static void toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state,
214219
void *user_data) {
215220
AV_State *me = static_cast<AV_State *>(user_data);
216-
ck_assert(std::this_thread::get_id() == me->tox_thread.get_id());
221+
ck_assert(std::this_thread::get_id() == me->tox_thread_.get_id());
217222
ck_assert(state != TOXAV_FRIEND_CALL_STATE_ERROR);
218223
std::cout << "[" << me->name_ << "] State changed to: " << std::to_string(state) << std::endl;
219-
me->call_state.store(state);
220-
TimePoint tp = me->call_start.load();
224+
me->call_state_.store(state);
225+
TimePoint tp = me->call_start_.load();
221226

222227
if (state != TOXAV_FRIEND_CALL_STATE_NONE && tp == TimePoint()) {
223-
me->call_start.store(Clock::now());
224-
me->in_call.store(true);
228+
me->call_start_.store(Clock::now());
229+
me->in_call_.store(true);
225230
}
226231
}
227232

@@ -233,13 +238,13 @@ class AV_State {
233238
std::cout << "[" << me->name_ << "] Received video payload" << std::endl;
234239

235240
// toxav.h states that receive events are emitted from their respective threads
236-
if (me->combined_) {
237-
ck_assert(std::this_thread::get_id() == me->av_thread.get_id());
241+
if (me->combined_av_) {
242+
ck_assert(std::this_thread::get_id() == me->av_thread_.get_id());
238243
} else {
239-
ck_assert(std::this_thread::get_id() == me->video_thread.get_id());
244+
ck_assert(std::this_thread::get_id() == me->video_thread_.get_id());
240245
}
241246

242-
me->video_received = true;
247+
me->video_received_ = true;
243248
}
244249

245250
static void toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, int16_t const *pcm,
@@ -248,24 +253,24 @@ class AV_State {
248253
AV_State *me = static_cast<AV_State *>(user_data);
249254
std::cout << "[" << me->name_ << "] Received audio payload" << std::endl;
250255

251-
if (me->combined_) {
252-
ck_assert(std::this_thread::get_id() == me->av_thread.get_id());
256+
if (me->combined_av_) {
257+
ck_assert(std::this_thread::get_id() == me->av_thread_.get_id());
253258
} else {
254-
ck_assert(std::this_thread::get_id() == me->audio_thread.get_id());
259+
ck_assert(std::this_thread::get_id() == me->audio_thread_.get_id());
255260
}
256261

257-
me->audio_received = true;
262+
me->audio_received_ = true;
258263
}
259264

260265
void tox_iterator() {
261-
while (!stop_threads.load()) {
266+
while (!stop_threads_.load()) {
262267
// Perform this block only while loop lock is held
263268
{
264-
std::lock_guard<std::mutex> lock(tox_loop_lock);
269+
std::lock_guard<std::mutex> lock(tox_loop_lock_);
265270
tox_iterate(tox_.get(), this);
266271

267272
// handle incoming call
268-
if (incomming.exchange(false)) {
273+
if (incomming_.exchange(false)) {
269274
Toxav_Err_Answer answer_err;
270275
toxav_answer(toxAV_.get(), 0, TEST_A_BITRATE, TEST_V_BITRATE, &answer_err);
271276

@@ -276,19 +281,19 @@ class AV_State {
276281

277282
std::cout << "[" << name_ << "] Answering call" << std::endl;
278283

279-
call_start = Clock::now();
280-
in_call.store(true);
284+
call_start_ = Clock::now();
285+
in_call_.store(true);
281286
}
282287

283-
if (in_call.load()) {
284-
uint32_t state = call_state.load();
285-
TimePoint tp = call_start.load();
288+
if (in_call_.load()) {
289+
uint32_t state = call_state_.load();
290+
TimePoint tp = call_start_.load();
286291
std::chrono::duration<double> call_time = Clock::now() - tp;
287292

288293
if (state == TOXAV_FRIEND_CALL_STATE_FINISHED) {
289294
std::cout << "[" << name_ << "] Call ended by other side after: " << call_time.count()
290295
<< "s" << std::endl;
291-
in_call.store(false);
296+
in_call_.store(false);
292297
} else if (tp > TimePoint() && call_time > AV_State::AUTO_HANGUP_TIME) {
293298
std::cout << "[" << name_ << "] Ending call after: " << call_time.count() << "s"
294299
<< std::endl;
@@ -302,7 +307,7 @@ class AV_State {
302307
ck_assert(0);
303308
}
304309

305-
in_call.store(false);
310+
in_call_.store(false);
306311
}
307312
}
308313
}
@@ -312,7 +317,7 @@ class AV_State {
312317
}
313318

314319
void toxav_iterator(Iteration_Type type) {
315-
while (!stop_threads.load()) {
320+
while (!stop_threads_.load()) {
316321
switch (type) {
317322
case Iteration_Type::TOXAV_AUDIO:
318323
toxav_audio_iterate(toxAV_.get());
@@ -335,26 +340,26 @@ class AV_State {
335340
}
336341
}
337342

338-
std::thread tox_thread;
339-
std::thread audio_thread;
340-
std::thread video_thread;
341-
std::thread av_thread;
343+
std::thread tox_thread_;
344+
std::thread audio_thread_;
345+
std::thread video_thread_;
346+
std::thread av_thread_;
342347

343-
std::mutex tox_loop_lock;
348+
std::mutex tox_loop_lock_;
344349

345350
Tox_Ptr tox_;
346-
bool combined_;
351+
bool combined_av_;
347352
ToxAV_Ptr toxAV_;
348353

349-
std::atomic_bool stop_threads;
350-
std::atomic_bool incomming;
351-
std::atomic_uint32_t call_state;
354+
std::atomic_bool stop_threads_;
355+
std::atomic_bool incomming_;
356+
std::atomic_uint32_t call_state_;
352357

353-
std::atomic<TimePoint> call_start;
354-
std::atomic_bool in_call;
358+
std::atomic<TimePoint> call_start_;
359+
std::atomic_bool in_call_;
355360

356-
std::atomic_bool video_received;
357-
std::atomic_bool audio_received;
361+
std::atomic_bool video_received_;
362+
std::atomic_bool audio_received_;
358363
std::string name_;
359364
};
360365

@@ -382,33 +387,29 @@ constexpr uint8_t DUMMY_VIDEO::y[];
382387
constexpr uint8_t DUMMY_VIDEO::u[];
383388
constexpr uint8_t DUMMY_VIDEO::v[];
384389

385-
} // namespace
386-
387-
static void test_av(bool combined) {
388-
std::cout << "Testing Audio and Video in" << (combined ? "combined" : "separate") << " threads"
390+
static void test_av(bool combined_av) {
391+
std::cout << "Testing Audio and Video in" << (combined_av ? "combined" : "separate") << " threads"
389392
<< std::endl;
390-
auto toxes = prepareNetwork(2);
393+
auto toxes = prepare_network(2);
391394

392395
AV_State alice(std::move(toxes[0]), "alice", false);
393396
AV_State bob(std::move(toxes[1]), "bob", false);
394397

395398
// Let alice call bob
396399
{
397-
std::lock_guard<std::mutex>(alice.getToxLoopLock());
400+
std::lock_guard<std::mutex>(alice.get_tox_loop_lock());
398401
Toxav_Err_Call err;
399402
ck_assert(
400-
toxav_call(alice.getToxAV(), 0, AV_State::TEST_A_BITRATE, AV_State::TEST_V_BITRATE, &err));
403+
toxav_call(alice.get_ToxAV(), 0, AV_State::TEST_A_BITRATE, AV_State::TEST_V_BITRATE, &err));
401404
ck_assert(err == TOXAV_ERR_CALL_OK);
402405
}
403406

404407
std::cout << "alice started a call" << std::endl;
405408

406409
auto poll_state = [](AV_State &av, uint32_t expected, uint32_t max_tries,
407410
uint32_t delay_ms) -> bool {
408-
uint32_t i;
409-
410-
for (i = 0; i < max_tries; ++i) {
411-
uint32_t state = av.getCallState();
411+
for (uint32_t i = 0; i < max_tries; ++i) {
412+
uint32_t state = av.get_call_state();
412413

413414
if (state == expected) {
414415
return true;
@@ -431,8 +432,8 @@ static void test_av(bool combined) {
431432
// TODO: why is Bob's call state not updated?
432433
// ck_assert(poll_state(bob, full_AV_mask, 10, 100)); asserts
433434

434-
ck_assert(alice.inCall());
435-
ck_assert(bob.inCall());
435+
ck_assert(alice.in_call());
436+
ck_assert(bob.in_call());
436437

437438
std::cout << "alice and bob are in the call" << std::endl;
438439

@@ -448,23 +449,23 @@ static void test_av(bool combined) {
448449

449450
// Send frames from alice to bob
450451
{
451-
std::lock_guard<std::mutex>(alice.getToxLoopLock());
452+
std::lock_guard<std::mutex>(alice.get_tox_loop_lock());
452453
Toxav_Err_Send_Frame err;
453-
ck_assert(toxav_audio_send_frame_dummy(alice.getToxAV(), &err));
454+
ck_assert(toxav_audio_send_frame_dummy(alice.get_ToxAV(), &err));
454455
ck_assert(err == TOXAV_ERR_SEND_FRAME_OK);
455456

456-
ck_assert(toxav_video_send_frame_dummy(alice.getToxAV(), &err));
457+
ck_assert(toxav_video_send_frame_dummy(alice.get_ToxAV(), &err));
457458
ck_assert(err == TOXAV_ERR_SEND_FRAME_OK);
458459
}
459460

460461
// Send frames from bob to alice
461462
{
462-
std::lock_guard<std::mutex>(bob.getToxLoopLock());
463+
std::lock_guard<std::mutex>(bob.get_tox_loop_lock());
463464
Toxav_Err_Send_Frame err;
464-
ck_assert(toxav_audio_send_frame_dummy(bob.getToxAV(), &err));
465+
ck_assert(toxav_audio_send_frame_dummy(bob.get_ToxAV(), &err));
465466
ck_assert(err == TOXAV_ERR_SEND_FRAME_OK);
466467

467-
ck_assert(toxav_video_send_frame_dummy(bob.getToxAV(), &err));
468+
ck_assert(toxav_video_send_frame_dummy(bob.get_ToxAV(), &err));
468469
ck_assert(err == TOXAV_ERR_SEND_FRAME_OK);
469470
}
470471

@@ -474,20 +475,22 @@ static void test_av(bool combined) {
474475
// TODO: why is Bobs call state not updated?
475476
// ck_assert(poll_state(bob, TOXAV_FRIEND_CALL_STATE_FINISHED, 30, 100)); fails
476477

477-
ck_assert(!alice.inCall());
478-
ck_assert(!bob.inCall());
478+
ck_assert(!alice.in_call());
479+
ck_assert(!bob.in_call());
479480

480481
std::cout << "The call ended" << std::endl;
481482

482-
alice.stopThreads();
483-
bob.stopThreads();
483+
alice.stop_threads();
484+
bob.stop_threads();
484485

485-
ck_assert(alice.didReceiveAudio());
486-
ck_assert(alice.didReceiveVideo());
487-
ck_assert(bob.didReceiveAudio());
488-
ck_assert(bob.didReceiveVideo());
486+
ck_assert(alice.did_receive_audio());
487+
ck_assert(alice.did_receive_video());
488+
ck_assert(bob.did_receive_audio());
489+
ck_assert(bob.did_receive_video());
489490
}
490491

492+
} // namespace
493+
491494
int main(void) {
492495
setvbuf(stdout, nullptr, _IONBF, 0);
493496

0 commit comments

Comments
 (0)