Add simple test for atomic operations

The test runs some simple tests with the atomic operations in MaxScale.
This commit is contained in:
Markus Mäkelä 2017-04-14 05:48:48 +03:00 committed by Markus Mäkelä
parent 122337569c
commit f91d415be1
2 changed files with 91 additions and 0 deletions

View File

@ -1,3 +1,4 @@
add_executable(test_atomic testatomic.c)
add_executable(test_adminusers testadminusers.c)
add_executable(test_buffer testbuffer.c)
add_executable(test_dcb testdcb.c)
@ -22,6 +23,7 @@ add_executable(testmaxscalepcre2 testmaxscalepcre2.c)
add_executable(testmodulecmd testmodulecmd.c)
add_executable(testconfig testconfig.c)
add_executable(trxboundaryparser_profile trxboundaryparser_profile.cc)
target_link_libraries(test_atomic maxscale-common)
target_link_libraries(test_adminusers maxscale-common)
target_link_libraries(test_buffer maxscale-common)
target_link_libraries(test_dcb maxscale-common)
@ -46,6 +48,7 @@ target_link_libraries(testmaxscalepcre2 maxscale-common)
target_link_libraries(testmodulecmd maxscale-common)
target_link_libraries(testconfig maxscale-common)
target_link_libraries(trxboundaryparser_profile maxscale-common)
add_test(TestAtomic test_atomic)
add_test(TestAdminUsers test_adminusers)
add_test(TestBuffer test_buffer)
add_test(TestDCB test_dcb)

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cdefs.h>
#include <stdio.h>
#include <maxscale/atomic.h>
#include <maxscale/debug.h>
#include <maxscale/thread.h>
#define NTHR 10
static int running = 0;
static int expected = 0;
void test_add(void* data)
{
int id = (size_t)data;
while (atomic_read(&running))
{
atomic_add(&expected, id);
atomic_add(&expected, -id);
ss_dassert(atomic_read(&expected) >= 0);
}
}
void test_load_store(void* data)
{
int id = (size_t)data;
while (atomic_read(&running))
{
if (atomic_read(&expected) % NTHR == id)
{
ss_dassert(atomic_add(&expected, 1) % NTHR == id + 1);
}
}
}
int run_test(void(*func)(void*))
{
THREAD threads[NTHR];
atomic_write(&expected, 0);
atomic_write(&running, 1);
for (int i = 0; i < NTHR; i++)
{
if (thread_start(&threads[i], func, NULL) == NULL)
{
ss_dassert(false);
}
}
thread_millisleep(2500);
atomic_write(&running, 0);
for (int i = 0; i < NTHR; i++)
{
thread_wait(threads[i]);
}
return atomic_read(&expected);
}
int main(int argc, char** argv)
{
int rval = 0;
run_test(test_load_store);
run_test(test_add);
return rval;
}