From 9c499648e3097fb8e92e74a242fc83d6e6d09776 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 29 Dec 2016 23:41:03 -0800 Subject: [PATCH] Add thread pool tests --- .travis.yml | 2 +- tests/.gitignore | 1 + tests/Makefile | 8 +++++- tests/pool.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/pool.c diff --git a/.travis.yml b/.travis.yml index 6bf99f1bf..36537cbee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ matrix: os: linux sudo: false - - env: Ubu=12.04cont Cmd="make zlibwrapper && make clean && make -C tests test-symbols && make clean && make -C tests test-zstd-nolegacy && make clean && make cmaketest && make clean && make -C contrib/pzstd googletest pzstd tests check && make -C contrib/pzstd clean" + - env: Ubu=12.04cont Cmd="make zlibwrapper && make clean && make -C tests test-pool && make -C tests test-symbols && make clean && make -C tests test-zstd-nolegacy && make clean && make cmaketest && make clean && make -C contrib/pzstd googletest pzstd tests check && make -C contrib/pzstd clean" os: linux sudo: false language: cpp diff --git a/tests/.gitignore b/tests/.gitignore index e932ad91c..5041404dd 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -15,6 +15,7 @@ paramgrill32 roundTripCrash longmatch symbols +pool invalidDictionaries # Tmp test directory diff --git a/tests/Makefile b/tests/Makefile index c080fe34a..739944de8 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -158,6 +158,9 @@ else $(CC) $(FLAGS) $^ -o $@$(EXT) -Wl,-rpath=$(ZSTDDIR) $(ZSTDDIR)/libzstd.so endif +pool : pool.c $(ZSTDDIR)/common/pool.c + $(CC) $(FLAGS) -pthread -DZSTD_PTHREAD $^ -o $@$(EXT) + namespaceTest: if $(CC) namespaceTest.c ../lib/common/xxhash.c -o $@ ; then echo compilation should fail; exit 1 ; fi $(RM) $@ @@ -176,7 +179,7 @@ clean: fuzzer-dll$(EXT) zstreamtest-dll$(EXT) zbufftest-dll$(EXT)\ zstreamtest$(EXT) zstreamtest32$(EXT) \ datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) longmatch$(EXT) \ - symbols$(EXT) invalidDictionaries$(EXT) + symbols$(EXT) invalidDictionaries$(EXT) pool$(EXT) @echo Cleaning completed @@ -288,4 +291,7 @@ test-invalidDictionaries: invalidDictionaries test-symbols: symbols $(QEMU_SYS) ./symbols +test-pool: pool + $(QEMU_SYS) ./pool + endif diff --git a/tests/pool.c b/tests/pool.c new file mode 100644 index 000000000..ce38075d0 --- /dev/null +++ b/tests/pool.c @@ -0,0 +1,70 @@ +#include "pool.h" +#include +#include +#include + +#define ASSERT_TRUE(p) \ + do { \ + if (!(p)) { \ + return 1; \ + } \ + } while (0) +#define ASSERT_FALSE(p) ASSERT_TRUE(!(p)) +#define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs)) + +struct data { + pthread_mutex_t mutex; + unsigned data[1024]; + size_t i; +}; + +void fn(void *opaque) { + struct data *data = (struct data *)opaque; + pthread_mutex_lock(&data->mutex); + data->data[data->i] = data->i; + ++data->i; + pthread_mutex_unlock(&data->mutex); +} + +int testOrder(size_t numThreads, size_t queueLog) { + struct data data; + POOL_ctx *ctx = POOL_create(numThreads, queueLog); + ASSERT_TRUE(ctx); + data.i = 0; + ASSERT_FALSE(pthread_mutex_init(&data.mutex, NULL)); + { + size_t i; + for (i = 0; i < 1024; ++i) { + POOL_add(ctx, &fn, &data); + } + } + POOL_free(ctx); + ASSERT_EQ(1024, data.i); + { + size_t i; + for (i = 0; i < data.i; ++i) { + ASSERT_EQ(i, data.data[i]); + } + } + ASSERT_FALSE(pthread_mutex_destroy(&data.mutex)); + return 0; +} + +int main(int argc, const char **argv) { + size_t numThreads; + for (numThreads = 1; numThreads <= 8; ++numThreads) { + size_t queueLog; + for (queueLog = 1; queueLog <= 8; ++queueLog) { + if (testOrder(numThreads, queueLog)) { + printf("FAIL: testOrder\n"); + return 1; + } + } + } + printf("PASS: testOrder\n"); + (POOL_create(0, 1) || POOL_create(1, 0)) ? printf("FAIL: testInvalid\n") + : printf("PASS: testInvalid\n"); + (void)argc; + (void)argv; + return 0; +} -- 2.47.2