diff options
Diffstat (limited to 'server/src/connectionpool.cc')
-rw-r--r-- | server/src/connectionpool.cc | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/server/src/connectionpool.cc b/server/src/connectionpool.cc index 07e6aa2..e2b6bfc 100644 --- a/server/src/connectionpool.cc +++ b/server/src/connectionpool.cc @@ -58,6 +58,20 @@ static void* thread_run(void *data) return NULL; } +static void* thread_run_clear_test(void *data) +{ + ConnectionPool<int> *pool = (ConnectionPool<int>*)data; + pool->giveBack(1); + pool->giveBack(2); + + sleep(1); + + pool->giveBack(3); + pool->giveBack(4); + + return NULL; +} + TEST_BEGIN; ConnectionPool<int> pool; @@ -90,19 +104,21 @@ pool.giveBack(b_db2); TEST_EQUAL(pool.numFree(), 4, "Testing number of free databases."); -pthread_attr_t attr; -pthread_t tid; -pthread_attr_init(&attr); -pthread_create(&tid, &attr, thread_run, &pool); - -while(pool.numFree() > 0) { usleep(10); } - -int b_db3 = pool.borrow(); -TEST_FALSE(pool.testFree(b_db3), "Testing if returned db is free (semaphore test)."); -pool.giveBack(db3); - -pthread_join(tid, NULL); -pthread_attr_destroy(&attr); +{ + pthread_attr_t attr; + pthread_t tid; + pthread_attr_init(&attr); + pthread_create(&tid, &attr, thread_run, &pool); + + while(pool.numFree() > 0) { usleep(10); } + + int b_db3 = pool.borrow(); + TEST_FALSE(pool.testFree(b_db3), "Testing if returned db is free (semaphore test)."); + pool.giveBack(db3); + + pthread_join(tid, NULL); + pthread_attr_destroy(&attr); +} TEST_EQUAL(pool.numFree(), 4, "Testing if all database are now available again"); @@ -114,6 +130,33 @@ TEST_EQUAL(pool.numFree(), 4, "Testing if all database are now available again") } TEST_EQUAL(pool.numFree(), 4, "Testing if autoborrower has returned the db."); +// Force remove all elements. +pool.borrow(); +pool.clear(true); +TEST_EQUAL(pool.numFree(), 0, "Testing number of free databases."); + +// Add them again. +pool.add(db1); +pool.add(db2); +pool.add(db3); +pool.add(db4); +TEST_EQUAL(pool.numFree(), 4, "Testing number of free databases."); + +pool.borrow(); +pool.borrow(); +pool.borrow(); +pool.borrow(); + +{ + pthread_attr_t attr; + pthread_t tid; + pthread_attr_init(&attr); + pthread_create(&tid, &attr, thread_run_clear_test, &pool); + pool.clear(false); + pthread_join(tid, NULL); + pthread_attr_destroy(&attr); +} + TEST_END; #endif/*TEST_CONNECTIONPOOL*/ |