From 80864734046d596879f9107f07e97bde40808ae5 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Wed, 10 Aug 2016 11:15:18 +0800 Subject: [PATCH] Account for yet another X11 race condition in unit tests. --- src/clipboard_x11.c | 12 +++++++----- test/libclipboard-test-private.h | 2 +- test/test_basics.cpp | 9 +++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/clipboard_x11.c b/src/clipboard_x11.c index a582c5d..11fc3dd 100644 --- a/src/clipboard_x11.c +++ b/src/clipboard_x11.c @@ -185,11 +185,13 @@ static void x11_clear_selection(clipboard_c *cb, xcb_selection_clear_event_t *e) } for (int i = 0; i < LCB_MODE_END; i++) { - if (cb->selections[i].xmode == e->selection && (pthread_mutex_lock(&cb->mu) == 0)) { - xcb_atom_t xmode = cb->selections[i].xmode; - cb->free(cb->selections[i].data); - memset(&cb->selections[i], 0, sizeof(selection_c)); - cb->selections[i].xmode = xmode; + selection_c *sel = &cb->selections[i]; + if (sel->xmode == e->selection && (pthread_mutex_lock(&cb->mu) == 0)) { + cb->free(sel->data); + sel->data = NULL; + sel->length = 0; + sel->has_ownership = false; + sel->target = XCB_NONE; pthread_mutex_unlock(&cb->mu); break; } diff --git a/test/libclipboard-test-private.h b/test/libclipboard-test-private.h index 0d745f7..ff7e90b 100644 --- a/test/libclipboard-test-private.h +++ b/test/libclipboard-test-private.h @@ -26,7 +26,7 @@ # define TRY_RUN(fn, ev, ret, oper) do { \ ret = fn; \ - for (int i = 0; i < 10 && oper(ret, ev); i++) { \ + for (int i = 0; i < 5 && oper(ret, ev); i++) { \ std::cout << "Warning(at line:" TO_STRING(__LINE__) "): " TO_STRING(fn) " returned '" << \ ret << "' trying again!" << std::endl; \ std::this_thread::sleep_for(std::chrono::milliseconds(50)); \ diff --git a/test/test_basics.cpp b/test/test_basics.cpp index 52ba358..cc6faee 100644 --- a/test/test_basics.cpp +++ b/test/test_basics.cpp @@ -77,7 +77,16 @@ TEST_F(BasicsTest, TestOwnership) { ASSERT_TRUE(clipboard_set_text_ex(cb1, "test", -1, LCB_CLIPBOARD)); ASSERT_TRUE(clipboard_has_ownership(cb1, LCB_CLIPBOARD)); + char *ret; ASSERT_FALSE(clipboard_has_ownership(cb2, LCB_CLIPBOARD)); + /* + The line below is present only for synchronisation purposes. + On X11, it may happen that cb2's set text call happens *before* + cb1's, meaning that the ownership would still belong to cb1. + */ + TRY_RUN_EQ(clipboard_text_ex(cb2, NULL, LCB_CLIPBOARD), NULL, ret); + ASSERT_TRUE(ret != NULL); + free(ret); ASSERT_TRUE(clipboard_set_text_ex(cb2, "test2", -1, LCB_CLIPBOARD)); bool has_ownership;