From a33fbbf643a52938063779a35167757a2426446e Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 3 Sep 2010 16:20:08 +0200 Subject: more testing --- src/result.cpp | 2 +- tests/GNUmakefile | 2 +- tests/Makefile.W32 | 2 +- tests/test9.cpp | 64 +++++++++++++++++++++++++++++++++++------------------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/result.cpp b/src/result.cpp index 0994f47..f855a0a 100644 --- a/src/result.cpp +++ b/src/result.cpp @@ -95,7 +95,7 @@ TRY_AGAIN_STEP: /* don't fail if the sqlite file is locked by another writer, try again later */ case SQLITE_BUSY: - sqlitexx_port_sleep( 1 ); +// sqlitexx_port_sleep( 1 ); goto TRY_AGAIN_STEP; default: { diff --git a/tests/GNUmakefile b/tests/GNUmakefile index a2bd9af..8701359 100644 --- a/tests/GNUmakefile +++ b/tests/GNUmakefile @@ -66,4 +66,4 @@ local_test: @./exec_test test4 "type conversion of bind parameters" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" @./exec_test test5 "PRAGMAs outside a transaction" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" @./exec_test test8 "ticket #14: to conversion fails with SQLITE_NULL" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" - @./exec_test test9 "ticket #5: support in mult-threaded enironment" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" + @./exec_test test9 "ticket #5: support in multi-threaded enironment" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" diff --git a/tests/Makefile.W32 b/tests/Makefile.W32 index bffd817..fa8d8dd 100644 --- a/tests/Makefile.W32 +++ b/tests/Makefile.W32 @@ -67,4 +67,4 @@ local_test: $(CPP_BINS) $(BINS) @exec_test.cmd test4 "type conversion of bind parameters" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" @exec_test.cmd test5 "PRAGMAs outside a transaction" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" @exec_test.cmd test8 "ticket #14: to conversion fails with SQLITE_NULL" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" - @exec_test.cmd test9 "ticket #5: support in mult-threaded enironment" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" + @exec_test.cmd test9 "ticket #5: support in multi-threaded enironment" "$(PLATFORM)" "$(LINUX_DIST)" "$(LINUX_REV)" diff --git a/tests/test9.cpp b/tests/test9.cpp index 5b57a87..62e17dc 100644 --- a/tests/test9.cpp +++ b/tests/test9.cpp @@ -26,26 +26,29 @@ /* test needs 'unlink' from 'unistd.h' */ #if !defined _WIN32 #include +#include #endif /* !defined _WIN32 */ #include "threads.h" using namespace sqlite3xx; using namespace std; -const int NOF_PRODUCERS = 2; -const int NOF_CONSUMERS = 2; -const int NOF_PRODUCER_TRANSACTIONS = 10; -const int NOF_PRODUCER_OPS = 10; -const int NOF_CONSUMER_TRANSACTIONS = 10; -const int NOF_CONSUMER_OPS = 10; +const int NOF_PRODUCERS = 10; +const int NOF_CONSUMERS = 10; +const int NOF_PRODUCER_TRANSACTIONS = 100; +const int NOF_PRODUCER_OPS = 100; +const int NOF_CONSUMER_TRANSACTIONS = 100; +const int NOF_CONSUMER_OPS = 100; #define UNUSED( x ) if( 0 && (x) ) { } -MUTEX_TYPE cout_mutex; +static MUTEX_TYPE cout_mutex; + +static bool verbose = false; static THREAD_FUNC_DECL produce( void *thread_data ) { - UNUSED( thread_data ); + uintptr_t no = (uintptr_t)thread_data; try { connection c( "test9.db" ); @@ -53,14 +56,21 @@ static THREAD_FUNC_DECL produce( void *thread_data ) for( int i = 0; i < NOF_PRODUCER_TRANSACTIONS; i++ ) { work t( c, "ins" ); - MUTEX_LOCK( cout_mutex ); - cout << "producer transaction " << i << " started" << endl; - MUTEX_UNLOCK( cout_mutex ); + if( verbose ) { + MUTEX_LOCK( cout_mutex ); + cout << "producer " << no << " transaction " << i << " started" << endl; + MUTEX_UNLOCK( cout_mutex ); + } for( int j = 0; j < NOF_PRODUCER_OPS; j++ ) { - result r = t.prepared( "ins" )( i * 1000 + j ).exec( ); + result r = t.prepared( "ins" )( no * 100000 + i * 1000 + j ).exec( ); assert( r.affected_rows( ) == 1 ); } t.commit( ); + if( verbose ) { + MUTEX_LOCK( cout_mutex ); + cout << "producer " << no << " transaction " << i << " terminated" << endl; + MUTEX_UNLOCK( cout_mutex ); + } } } catch( sql_error& e ) { cerr << "producer error, " << e.msg( ) << ": " << e.query( ) << endl; @@ -71,7 +81,7 @@ static THREAD_FUNC_DECL produce( void *thread_data ) static THREAD_FUNC_DECL consume( void *thread_data ) { - UNUSED( thread_data ); + uintptr_t no = (uintptr_t)thread_data; try { connection c( "test9.db" ); @@ -79,20 +89,30 @@ static THREAD_FUNC_DECL consume( void *thread_data ) for( int i = 0; i < NOF_CONSUMER_TRANSACTIONS; i++ ) { work t( c, "sel" ); - MUTEX_LOCK( cout_mutex ); - cout << "consumer transaction " << i << " started" << endl; - MUTEX_UNLOCK( cout_mutex ); + if( verbose ) { + MUTEX_LOCK( cout_mutex ); + cout << "consumer " << no << " transaction " << i << " started" << endl; + MUTEX_UNLOCK( cout_mutex ); + } for( int j = 0; j < NOF_CONSUMER_OPS; j++ ) { result r = t.prepared( "sel" ).exec( ); - for( result::const_iterator it = r.begin( ); it < r.end( ); it++ ) { + int nof_rows = 1; + for( result::const_iterator it = r.begin( ); it < r.end( ); it++, nof_rows++ ) { int x; it["x"].to( x ); } + if( verbose ) { + MUTEX_LOCK( cout_mutex ); + cout << "consumer " << no << " transaction " << i << " got " << nof_rows << " rows." << endl; + MUTEX_UNLOCK( cout_mutex ); + } + } + t.commit( ); + if( verbose ) { MUTEX_LOCK( cout_mutex ); - cout << "consumer transaction got " << r.size( ) << " rows." << endl; + cout << "consumer " << no << " transaction " << i << " terminated" << endl; MUTEX_UNLOCK( cout_mutex ); } - t.commit( ); } } catch( sql_error& e ) { cerr << "consumer error, " << e.msg( ) << ": " << e.query( ) << endl; @@ -119,12 +139,12 @@ int main( ) { MUTEX_SETUP( cout_mutex ); - for( int i = 0; i < NOF_PRODUCERS; i++ ) { - THREAD_CREATE( &prod[i], produce, NULL ); + for( int i = 0; i < NOF_PRODUCERS ; i++ ) { + THREAD_CREATE( &prod[i], produce, (void *)i ); } for( int i = 0; i < NOF_CONSUMERS; i++ ) { - THREAD_CREATE( &cons[i], consume, NULL ); + THREAD_CREATE( &cons[i], consume, (void *)i ); } for( int i = 0; i < NOF_PRODUCERS; i++ ) { -- cgit v1.2.3-54-g00ecf