2016-04-14 14:36:15 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
static witness_lock_error_t *witness_lock_error_orig;
|
|
|
|
static witness_owner_error_t *witness_owner_error_orig;
|
|
|
|
static witness_not_owner_error_t *witness_not_owner_error_orig;
|
|
|
|
static witness_lockless_error_t *witness_lockless_error_orig;
|
|
|
|
|
|
|
|
static bool saw_lock_error;
|
|
|
|
static bool saw_owner_error;
|
|
|
|
static bool saw_not_owner_error;
|
|
|
|
static bool saw_lockless_error;
|
|
|
|
|
|
|
|
static void
|
|
|
|
witness_lock_error_intercept(const witness_list_t *witnesses,
|
2017-01-16 08:56:30 +08:00
|
|
|
const witness_t *witness) {
|
2016-04-14 14:36:15 +08:00
|
|
|
saw_lock_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
witness_owner_error_intercept(const witness_t *witness) {
|
2016-04-14 14:36:15 +08:00
|
|
|
saw_owner_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
witness_not_owner_error_intercept(const witness_t *witness) {
|
2016-04-14 14:36:15 +08:00
|
|
|
saw_not_owner_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
witness_lockless_error_intercept(const witness_list_t *witnesses) {
|
2016-04-14 14:36:15 +08:00
|
|
|
saw_lockless_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
witness_comp(const witness_t *a, void *oa, const witness_t *b, void *ob) {
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_u_eq(a->rank, b->rank, "Witnesses should have equal rank");
|
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
assert(oa == (void *)a);
|
|
|
|
assert(ob == (void *)b);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return strcmp(a->name, b->name);
|
2016-04-14 14:36:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-01-16 08:56:30 +08:00
|
|
|
witness_comp_reverse(const witness_t *a, void *oa, const witness_t *b,
|
|
|
|
void *ob) {
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_u_eq(a->rank, b->rank, "Witnesses should have equal rank");
|
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
assert(oa == (void *)a);
|
|
|
|
assert(ob == (void *)b);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return -strcmp(a->name, b->name);
|
2016-04-14 14:36:15 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a, b;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, NULL, NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &a);
|
|
|
|
witness_lock(tsdn, &a);
|
|
|
|
witness_assert_owner(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&b, "b", 2, NULL, NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &b);
|
|
|
|
witness_lock(tsdn, &b);
|
|
|
|
witness_assert_owner(tsdn, &b);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
|
|
|
witness_unlock(tsdn, &b);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness_comp) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a, b, c, d;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, witness_comp, &a);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &a);
|
|
|
|
witness_lock(tsdn, &a);
|
|
|
|
witness_assert_owner(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&b, "b", 1, witness_comp, &b);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &b);
|
|
|
|
witness_lock(tsdn, &b);
|
|
|
|
witness_assert_owner(tsdn, &b);
|
|
|
|
witness_unlock(tsdn, &b);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_lock_error_orig = witness_lock_error;
|
|
|
|
witness_lock_error = witness_lock_error_intercept;
|
|
|
|
saw_lock_error = false;
|
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&c, "c", 1, witness_comp_reverse, &c);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &c);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_false(saw_lock_error, "Unexpected witness lock error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &c);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_lock_error, "Expected witness lock error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &c);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
saw_lock_error = false;
|
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&d, "d", 1, NULL, NULL);
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_not_owner(tsdn, &d);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_false(saw_lock_error, "Unexpected witness lock error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &d);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_lock_error, "Expected witness lock error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &d);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_lock_error = witness_lock_error_orig;
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness_reversal) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a, b;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
|
|
|
witness_lock_error_orig = witness_lock_error;
|
|
|
|
witness_lock_error = witness_lock_error_intercept;
|
|
|
|
saw_lock_error = false;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, NULL, NULL);
|
|
|
|
witness_init(&b, "b", 2, NULL, NULL);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &b);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_false(saw_lock_error, "Unexpected witness lock error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_lock_error, "Expected witness lock error");
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
|
|
|
witness_unlock(tsdn, &b);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_lock_error = witness_lock_error_orig;
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness_recursive) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
|
|
|
witness_not_owner_error_orig = witness_not_owner_error;
|
|
|
|
witness_not_owner_error = witness_not_owner_error_intercept;
|
|
|
|
saw_not_owner_error = false;
|
|
|
|
|
|
|
|
witness_lock_error_orig = witness_lock_error;
|
|
|
|
witness_lock_error = witness_lock_error_intercept;
|
|
|
|
saw_lock_error = false;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, NULL, NULL);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_false(saw_lock_error, "Unexpected witness lock error");
|
|
|
|
assert_false(saw_not_owner_error, "Unexpected witness not owner error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_lock_error, "Expected witness lock error");
|
|
|
|
assert_true(saw_not_owner_error, "Expected witness not owner error");
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_owner_error = witness_owner_error_orig;
|
|
|
|
witness_lock_error = witness_lock_error_orig;
|
|
|
|
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness_unlock_not_owned) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
|
|
|
witness_owner_error_orig = witness_owner_error;
|
|
|
|
witness_owner_error = witness_owner_error_intercept;
|
|
|
|
saw_owner_error = false;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, NULL, NULL);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
assert_false(saw_owner_error, "Unexpected owner error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_owner_error, "Expected owner error");
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_owner_error = witness_owner_error_orig;
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_witness_lockful) {
|
2016-04-14 14:36:15 +08:00
|
|
|
witness_t a;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
test_skip_if(!config_debug);
|
|
|
|
|
|
|
|
witness_lockless_error_orig = witness_lockless_error;
|
|
|
|
witness_lockless_error = witness_lockless_error_intercept;
|
|
|
|
saw_lockless_error = false;
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-04-18 03:55:10 +08:00
|
|
|
witness_init(&a, "a", 1, NULL, NULL);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
assert_false(saw_lockless_error, "Unexpected lockless error");
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_lock(tsdn, &a);
|
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
assert_true(saw_lockless_error, "Expected lockless error");
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_unlock(tsdn, &a);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
witness_assert_lockless(tsdn);
|
2016-04-14 14:36:15 +08:00
|
|
|
|
|
|
|
witness_lockless_error = witness_lockless_error_orig;
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-04-14 14:36:15 +08:00
|
|
|
test_witness,
|
|
|
|
test_witness_comp,
|
|
|
|
test_witness_reversal,
|
|
|
|
test_witness_recursive,
|
|
|
|
test_witness_unlock_not_owned,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_witness_lockful);
|
2016-04-14 14:36:15 +08:00
|
|
|
}
|