From: Yann Collet Date: Thu, 21 Jun 2018 19:24:36 +0000 (-0700) Subject: added a test for POOL (multithreading) X-Git-Tag: v1.3.5~3^2~8^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d80ada5caa31c1d964e960f86670238473041a5;p=thirdparty%2Fzstd.git added a test for POOL (multithreading) ensuring all jobs in queue are nonetheless completed when POOL is instructed to end abruptly (POOL_free()) --- diff --git a/tests/poolTests.c b/tests/poolTests.c index 23061b568..d7ff70ac9 100644 --- a/tests/poolTests.c +++ b/tests/poolTests.c @@ -90,10 +90,10 @@ typedef struct { int val; int max; ZSTD_pthread_cond_t cond; -} test_t; +} poolTest_t; void waitLongFn(void *opaque) { - test_t* test = (test_t*) opaque; + poolTest_t* test = (poolTest_t*) opaque; UTIL_sleepMilli(10); ZSTD_pthread_mutex_lock(&test->mut); test->val = test->val + 1; @@ -102,7 +102,7 @@ void waitLongFn(void *opaque) { ZSTD_pthread_mutex_unlock(&test->mut); } -static int testThreadReduction_internal(POOL_ctx* ctx, test_t test) +static int testThreadReduction_internal(POOL_ctx* ctx, poolTest_t test) { int const nbWaits = 16; UTIL_time_t startTime, time4threads, time2threads; @@ -117,7 +117,7 @@ static int testThreadReduction_internal(POOL_ctx* ctx, test_t test) } ZSTD_pthread_mutex_lock(&test.mut); ZSTD_pthread_cond_wait(&test.cond, &test.mut); - ASSERT_TRUE(test.val == nbWaits); + ASSERT_EQ(test.val, nbWaits); ZSTD_pthread_mutex_unlock(&test.mut); time4threads = UTIL_clockSpanNano(startTime); @@ -131,7 +131,7 @@ static int testThreadReduction_internal(POOL_ctx* ctx, test_t test) } ZSTD_pthread_mutex_lock(&test.mut); ZSTD_pthread_cond_wait(&test.cond, &test.mut); - ASSERT_TRUE(test.val == nbWaits); + ASSERT_EQ(test.val, nbWaits); ZSTD_pthread_mutex_unlock(&test.mut); time2threads = UTIL_clockSpanNano(startTime); @@ -141,7 +141,7 @@ static int testThreadReduction_internal(POOL_ctx* ctx, test_t test) static int testThreadReduction(void) { int result; - test_t test; + poolTest_t test; POOL_ctx* ctx = POOL_create(4 /*nbThreads*/, 2 /*queueSize*/); ASSERT_TRUE(ctx); @@ -155,10 +155,59 @@ static int testThreadReduction(void) { ZSTD_pthread_mutex_destroy(&test.mut); ZSTD_pthread_cond_destroy(&test.cond); POOL_free(ctx); + return result; } +/* --- test abrupt ending --- */ + +typedef struct { + ZSTD_pthread_mutex_t mut; + int val; +} abruptEndCanary_t; + +void waitIncFn(void *opaque) { + abruptEndCanary_t* test = (abruptEndCanary_t*) opaque; + UTIL_sleepMilli(1); + ZSTD_pthread_mutex_lock(&test->mut); + test->val = test->val + 1; + ZSTD_pthread_mutex_unlock(&test->mut); +} + +static int testAbruptEnding_internal(abruptEndCanary_t test) +{ + int const nbWaits = 16; + + POOL_ctx* const ctx = POOL_create(2 /*numThreads*/, nbWaits /*queueSize*/); + ASSERT_TRUE(ctx); + test.val = 0; + + { int i; + for (i=0; i