| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <pthread.h> |
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "mempool.h" |
| 7 |
#include "process-util.h" |
| 8 |
#include "set.h" |
| 9 |
#include "tests.h" |
| 10 |
|
| 11 |
#define NUM 100 |
| 12 |
|
| 13 |
static void* thread(void *p) { |
| 14 |
Set **s = p; |
| 15 |
|
| 16 |
assert_se(s); |
| 17 |
assert_se(*s); |
| 18 |
|
| 19 |
assert_se(!is_main_thread()); |
| 20 |
assert_se(mempool_enabled); |
| 21 |
assert_se(!mempool_enabled()); |
| 22 |
|
| 23 |
assert_se(set_size(*s) == NUM); |
| 24 |
*s = set_free(*s); |
| 25 |
|
| 26 |
return NULL; |
| 27 |
} |
| 28 |
|
| 29 |
static void test_one(const char *val) { |
| 30 |
pthread_t t; |
| 31 |
int x[NUM] = {}; |
| 32 |
unsigned i; |
| 33 |
Set *s; |
| 34 |
|
| 35 |
log_info("Testing with SYSTEMD_MEMPOOL=%s", val); |
| 36 |
assert_se(setenv("SYSTEMD_MEMPOOL", val, true) == 0); |
| 37 |
|
| 38 |
assert_se(is_main_thread()); |
| 39 |
assert_se(mempool_enabled); /* It is a weak symbol, but we expect it to be available */ |
| 40 |
assert_se(!mempool_enabled()); |
| 41 |
|
| 42 |
assert_se(s = set_new(NULL)); |
| 43 |
for (i = 0; i < NUM; i++) |
| 44 |
assert_se(set_put(s, &x[i])); |
| 45 |
|
| 46 |
assert_se(pthread_create(&t, NULL, thread, &s) == 0); |
| 47 |
assert_se(pthread_join(t, NULL) == 0); |
| 48 |
|
| 49 |
assert_se(!s); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(disable_mempool) { |
| 53 |
test_one("0"); |
| 54 |
/* The value $SYSTEMD_MEMPOOL= is cached. So the following |
| 55 |
* test should also succeed. */ |
| 56 |
test_one("1"); |
| 57 |
} |
| 58 |
|
| 59 |
DEFINE_TEST_MAIN(LOG_DEBUG); |