diff --git a/CEP/BB/BDBReplication/test/Makefile.am b/CEP/BB/BDBReplication/test/Makefile.am index f65bf9694a538be438d5de657f01df5d7cb1bad0..c21b9342db7ea1163598c58c41db13903817f636 100644 --- a/CEP/BB/BDBReplication/test/Makefile.am +++ b/CEP/BB/BDBReplication/test/Makefile.am @@ -1,10 +1,16 @@ common_top_builddir=@common_top_builddir@ -check_PROGRAMS = repQuote +check_PROGRAMS = tBDBReplication -repQuote_SOURCES = tBDBReplication.cc +tBDBReplication_SOURCES = tBDBReplication.cc LDADD = ../src/libbdbreplication.la DEPENDENCIES = ../src/libbdbreplication.la $(LOFAR_DEPEND) +TESTS_ENVIRONMENT = lofar_sharedir=$(lofar_sharedir) + +TESTS = \ + tBDBReplication.sh + + include $(lofar_sharedir)/Makefile.common diff --git a/CEP/BB/BDBReplication/test/tBDBReplication.cc b/CEP/BB/BDBReplication/test/tBDBReplication.cc index 97ed442d009b405998d610e85c649a80643d8e50..709a34e6266004487e5b86d06d12feebf816b201 100644 --- a/CEP/BB/BDBReplication/test/tBDBReplication.cc +++ b/CEP/BB/BDBReplication/test/tBDBReplication.cc @@ -27,85 +27,49 @@ #include <BDBReplication/BDBReplicator.h> -int -print_data (Dbc * dbc) -{ - Dbt key, value; +void write2db(DbEnv* myDbEnv, Db* myDb, char* key, char* value) { + Dbt keyT, valueT; - cout<<"\tkey\tvalue"<<endl; - cout<<"\t======\t====="<<endl; - - int flags = DB_FIRST; - while(dbc->get(&key, &value, flags) == 0) { - flags = DB_NEXT; - char buffer1[40]; - char buffer2[40]; - snprintf (buffer1, key.get_size()+1, "%s\0", (char*)key.get_data()); - snprintf (buffer2, value.get_size()+1, "%s\0", (char*)value.get_data()); - cout<<"\t"<<buffer1<<"\t"<<buffer2<<endl; - } - return 0; -} + keyT.set_data(key); + keyT.set_size(strlen(key)); -int -client (DbEnv* dbenv) -{ - DbTxn* myTxn; - dbenv->txn_begin(NULL, &myTxn, 0); + valueT.set_data(value); + valueT.set_size(strlen(value)); - Db myDb(dbenv, DB_CXX_NO_EXCEPTIONS); - Db mydDb(dbenv, DB_CXX_NO_EXCEPTIONS); - - LOG_TRACE_FLOW_STR ("opening database in environment "<<dbenv); - int minor, major, patch; - dbenv->version(&major, &minor, &patch); - LOG_TRACE_FLOW_STR("db version: "<<major<<"."<<minor<<"-"<<patch); - - try{ -// int ret = mydDb.open(myTxn, "quotes", "quotes", DB_BTREE, DB_CREATE, 0); -// if(ret !=0) { -// LOG_TRACE_FLOW_STR("Could not open database"<<dbenv->strerror(ret)); -// cerr<<"Could not open database"<<dbenv->strerror(ret)<<endl; -// } -// mydDb.close(0); - int ret = myDb.open(myTxn, "quotes", "quotes", DB_BTREE, DB_RDONLY, 0); - if(ret !=0) { - cerr<<"Could not open database"<<dbenv->strerror(ret)<<endl; - LOG_TRACE_FLOW_STR("Could not open database"<<dbenv->strerror(ret)); - } - } catch (Exception &e) { - cout<<"Exception while opening database: "<<e.what()<<endl; - exit(1); - } catch (...) { - cout<<"caught unknown exception"<<endl; - exit(1); + LOG_TRACE_FLOW_STR ("writing to master database: <"<<(char*)key<<"="<<value<<">"); + int ret = myDb->put(0, &keyT, &valueT, DB_AUTO_COMMIT); + if(ret !=0) { + LOG_TRACE_FLOW_STR("could not write to database on master:"<<myDbEnv->strerror(ret)); } - cout<<"db opened"<<endl; - myTxn->commit(0); +} - for (;;) { - cout<<"Reading from database .."<<endl; - LOG_TRACE_FLOW("Reading"); - - Dbc* cursor; - int ret = myDb.cursor(NULL, &cursor, 0); - if (ret != 0) { - cerr<<"Error while getting cursor: "<<dbenv->strerror(ret)<<endl; - exit(1); - } - - print_data (cursor); - cursor->close (); - - sleep (3); - } - - myDb.close(0); - - while(1); - return 0; -} +int readFromDb(DbEnv* myDbEnv, Db* myDb, char* key) { + Dbt keyT, valueT; + keyT.set_data(key); + keyT.set_size(strlen(key)); + + valueT.set_data(key); + valueT.set_size(strlen(key)); + + LOG_TRACE_FLOW_STR ("reading from client database: key = "<<key); + Dbc* cursorp; + myDb->cursor(NULL, &cursorp, 0); + + int ret = cursorp->get(&keyT, &valueT, DB_SET); +// DbTxn* myTxn = 0; +// myDbEnv->txn_begin(NULL, &myTxn, 0); +//myDb->get(NULL, &keyT, &valueT, DB_AUTO_COMMIT); +// myTxn->commit(0); + cursorp->close(); + + if(ret !=0) { + LOG_TRACE_FLOW_STR("could not read from database on client:"<<myDbEnv->strerror(ret)); + cerr<<"could not read from database on client:"<<myDbEnv->strerror(ret)<<endl;; + return 0; + } + return atoi((const char*)valueT.get_data()); +} int master (DbEnv* myDbEnv) @@ -117,7 +81,7 @@ master (DbEnv* myDbEnv) LOG_TRACE_FLOW_STR("creating db"); Db myDb(myDbEnv, 0); LOG_TRACE_FLOW_STR("opening database on master"); - if (myDb.open(myTxn, "quotes", "quotes", DB_BTREE, DB_CREATE, 0)!=0) + if (myDb.open(myTxn, "test", "test", DB_BTREE, DB_CREATE, 0)!=0) LOG_TRACE_FLOW("could not open database on master"); if (myTxn->commit(0) != 0) @@ -125,147 +89,115 @@ master (DbEnv* myDbEnv) Dbt key, value; - char buf[128], *rbuf; + // set 4 values in the db: + // first a = 2, b = 3, a = 5, c = 7 + // the client knows now that a*b*c should be 105 + + write2db(myDbEnv, &myDb, "a", "2"); + write2db(myDbEnv, &myDb, "b", "3"); + write2db(myDbEnv, &myDb, "a", "5"); + write2db(myDbEnv, &myDb, "c", "7"); + + while(1); + + return 0; +} +bool +client (DbEnv* dbenv) +{ + DbTxn* myTxn; + dbenv->txn_begin(NULL, &myTxn, 0); + + Db myDb(dbenv, DB_CXX_NO_EXCEPTIONS); + + LOG_TRACE_FLOW_STR ("opening database in environment "<<dbenv); + int minor, major, patch; + dbenv->version(&major, &minor, &patch); + LOG_TRACE_FLOW_STR("db version: "<<major<<"."<<minor<<"-"<<patch); + + int retries = 3; while(1) { - cout << "QUOTESERVER> "; - fflush (stdout); - - if (fgets (buf, sizeof (buf), stdin) == NULL) - break; - (void) strtok (&buf[0], " \t\n"); - rbuf = strtok (NULL, " \t\n"); - if (rbuf == NULL || rbuf[0] == '\0') { - if (strncmp (buf, "exit", 4) == 0 || strncmp (buf, "quit", 4) == 0) + try{ + int ret = myDb.open(myTxn, "test", "test", DB_BTREE, DB_RDONLY, 0); + if(ret !=0) { + cerr<<"Could not open database"<<dbenv->strerror(ret)<<endl; + LOG_TRACE_FLOW_STR("Could not open database"<<dbenv->strerror(ret)); + } else { + cout<<"db opened"<<endl; break; - continue; + } + } catch (Exception &e) { + cout<<"Exception while opening database: "<<e.what()<<endl; + exit(1); + } catch (...) { + cout<<"caught unknown exception"<<endl; + exit(1); } - - key.set_data(buf); - key.set_size(strlen (buf)); + retries--; + if (retries <= 0) exit(1); + sleep(3); + } + myTxn->commit(0); - value.set_data(rbuf); - value.set_size(strlen (rbuf)); + bool testOK = false; + for (int i=0; i<3; i++) { + LOG_TRACE_FLOW("Reading"); + + int a = readFromDb(dbenv, &myDb, "a"); + int b = readFromDb(dbenv, &myDb, "b"); + int c = readFromDb(dbenv, &myDb, "c"); - LOG_TRACE_FLOW_STR ("writing to master database: <"<<(char*)buf<<"="<<rbuf<<">"); - int ret = myDb.put(0, &key, &value, DB_AUTO_COMMIT); - if(ret !=0) { - LOG_TRACE_FLOW_STR("could not write to database on master:"<<myDbEnv->strerror(ret)); + if (a*b*c == 105) { + cerr<<"The answer is 105"<<endl; + testOK = true; + break; + } else { + cerr<<"The answer is not 105, but "<<a*b*c<<endl; + sleep(3); // wait a few seconds before a retry } - } - - return 0; -} + } + myDb.close(0); -int mainNoRep(int argc, char** argv) -{ - string itsDbEnvName = "dir0"; - LOG_TRACE_FLOW("Starting up"); - DbEnv* itsDbEnv = new DbEnv(DB_CXX_NO_EXCEPTIONS); - u_int32_t flags = DB_CREATE | DB_INIT_MPOOL | DB_INIT_TXN; - LOG_TRACE_FLOW("Opening environment"); - int ret = itsDbEnv->open(itsDbEnvName.c_str(), flags, 0); - if (ret != 0) { - LOG_TRACE_FLOW("Cannot open db environment"); - LOG_TRACE_FLOW_STR("Error: "<<itsDbEnv->strerror(ret)); - } - LOG_TRACE_FLOW("Starting master"); - try { - master (itsDbEnv); - } catch (Exception &e) { - cout<<"Exception: "<<e.what()<<endl; - } catch (DbDeadlockException &e) { - cout<<"DbException: "<<e.what()<<endl; - } catch (DbException &e) { - cout<<"DbException: "<<e.what()<<endl; - } catch (DbLockNotGrantedException &e) { - cout<<"DbException: "<<e.what()<<endl; - } catch (DbMemoryException &e) { - cout<<"DbException: "<<e.what()<<endl; - } catch (DbRunRecoveryException &e) { - cout<<"DbException: "<<e.what()<<endl; - } - return 0; -} + return testOK; +} int main (int argc, char *argv[]) { + try { INIT_LOGGER("tBDBReplication"); -#if 0 - return mainNoRep(argc, argv); -#else - int master_eid; - int my_eid; - char *myHostName; - unsigned short myPort; - char *masterHostName; - unsigned short masterPort; + unsigned short myPort = 0; + unsigned short masterPort = 0; - extern char *optarg; - enum - { MASTER, CLIENT, UNKNOWN } whoami; - int maxsites, nsites, ret, priority, verbose; - char *c, ch; - const char *home, *progname; - - master_eid = DB_EID_INVALID; - - whoami = UNKNOWN; - maxsites = nsites = ret = verbose = 0; - priority = 100; - home = "TESTDIR"; - progname = "ex_repquote"; + bool amMaster = false; + char *home = ""; + char ch; - while ((ch = getopt (argc, argv, "i:Ch:Mm:n:o:p:v")) != EOF) + while ((ch = getopt (argc, argv, "fh:m:o:")) != EOF) { switch (ch) { - case 'i': - my_eid = atoi (optarg); - if (my_eid == 0) - { - whoami = MASTER; - master_eid = 1; - } - else - { - whoami = CLIENT; - } + case 'f': + amMaster = true; break; case 'h': home = optarg; break; - case 'm': - myHostName = strtok(optarg, ":"); - if ((c = strtok (NULL, ":")) == NULL) - { - fprintf (stderr, "Bad host specification.\n"); - } - myPort = (unsigned short) atoi (c); - break; case 'o': - masterHostName = strtok(optarg, ":"); - if ((c = strtok (NULL, ":")) == NULL) - { - fprintf (stderr, "Bad host specification.\n"); - } - masterPort = atoi (c); + myPort = atoi (optarg); break; - case 'p': - priority = atoi (optarg); - break; - case 'v': - verbose = 1; + case 'm': + masterPort = atoi (optarg); break; - case '?': default: break; } + } BDBReplicator* BDBR; - if (whoami == MASTER){ - BDBR = new BDBReplicator(home,"localhost", myPort, "localhost", masterPort, true); + if (amMaster){ + BDBR = new BDBReplicator(home,"localhost", myPort, "localhost", myPort, true); } else { BDBR = new BDBReplicator(home,"localhost", myPort, "localhost", masterPort, false); }; @@ -274,18 +206,23 @@ main (int argc, char *argv[]) BDBR->startReplication(); DbEnv* myDbEnv = BDBR->getDbEnv(); - if (whoami == MASTER) + sleep(1); + + if (amMaster) { LOG_TRACE_FLOW_STR("starting master"); master (myDbEnv); } else { - // sleep (5); - client (myDbEnv); + LOG_TRACE_FLOW_STR("starting client"); + sleep(5); + if(!client (myDbEnv)) return 1; } return 0; -#endif + } catch ( ... ) { + return 1; + } } diff --git a/CEP/BB/BDBReplication/test/tBDBReplication.log_prop b/CEP/BB/BDBReplication/test/tBDBReplication.log_prop new file mode 100644 index 0000000000000000000000000000000000000000..0ef0b5ce6475e5092938167be4644b87257e41cd --- /dev/null +++ b/CEP/BB/BDBReplication/test/tBDBReplication.log_prop @@ -0,0 +1,15 @@ +# Configure the rootLogger +log4cplus.rootLogger=INFO, STDOUT +log4cplus.logger.TRC=TRACE, NOLOG +log4cplus.logger.TRC.additivity=false +log4cplus.additivity=false +# Define the STDOUT appender +log4cplus.appender.STDOUT=log4cplus::ConsoleAppender +log4cplus.appender.STDOUT.Threshhold=TRACE2 +log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout +log4cplus.appender.STDOUT.layout.ConversionPattern=%-5p [%x]%c{3} - %m%n +log4cplus.appender.STDOUT.logToStdErr=true +log4cplus.appender.NOLOG=log4cplus::NullAppender + +# Define TRC at level INFO +log4cplus.logger.BDBReplication=TRACE1, STDOUT diff --git a/CEP/BB/BDBReplication/test/tBDBReplication.run b/CEP/BB/BDBReplication/test/tBDBReplication.run new file mode 100755 index 0000000000000000000000000000000000000000..ebf8c481857a09fe86e004ce5e22e544a1b77f1a --- /dev/null +++ b/CEP/BB/BDBReplication/test/tBDBReplication.run @@ -0,0 +1,17 @@ +#!/bin/sh + +rm masterDir -rf +mkdir masterDir + +./tBDBReplication -f -h masterDir -o 8020 &> master.log & + +sleep 10 + +rm slaveDir -rf +mkdir slaveDir +./tBDBReplication -h slaveDir -m 8020 -o 8021 &> slave.log +result=$? + +killall tBDBReplication + +exit $result diff --git a/CEP/BB/BDBReplication/test/tBDBReplication.sh b/CEP/BB/BDBReplication/test/tBDBReplication.sh new file mode 100755 index 0000000000000000000000000000000000000000..44cbff0d59653ef9ac5fcc1c4145ea1266620b61 --- /dev/null +++ b/CEP/BB/BDBReplication/test/tBDBReplication.sh @@ -0,0 +1,2 @@ +#!/bin/sh +$lofar_sharedir/runtest.sh tBDBReplication > tBDBReplication.log 2>&1