testcache.cxx

来自「vovida的软交换」· CXX 代码 · 共 376 行

CXX
376
字号
#include "Record.hxx"#include "../dataObjects/test/TestRecord.hxx"#include "NetworkAddress.h"#include "Tcp_ClientSocket.hxx"#include "Cache.hxx"#include "cpLog.h"#include <unistd.h>char c;/*  * This is a test file, don't blame me if it doesn't look nice */#define WAIT_TIME 7#define newline "\n"#define headline(X) \  cout << newline << newline << newline << newline << newline;\  cout << newline << newline << "testCache.cxx: ***** "<< X << " *****" <<  newline;\#define tout cout << "testCache.cxx: "#define show_record(X) \  tout << "Showing record contents from memory at " << X << " ..." << newline;\  X->displayRecord();\  tout << "... end of record contents" << newline;\#define show_status \  cout << newline << newline;\  cout << "---------------------------------------------" << newline;\  cache->displayCache();\  cout << newline;\  cout << "testCache.cxx: **** HIT RETURN TO DO NEXT TEST **** ";\  cin.get(c); \int test1(void) {     tout << "Cache Test Starting ..." << newline << newline;  tout << "Getting instance of cache" << newline;  Cache* cache = Cache::getInstance();  tout << "Got Cache" << newline << newline;  show_status;  // Now do some actual tests  // Get simple record  headline("Get record 'colinc'");  Record* rec;  if (!(rec = cache->getRecord(TEST_RECORD, "colinc"))) {     // Fail   };  show_record(rec);  show_status;    // Get same record  headline("Getting record 'colinc' (should be from cache)");  Record* rec2 = cache->getRecord(TEST_RECORD, "colinc");  show_record(rec2);  show_status;   // Delete references to rec and rec2  headline("Deleting local pointers to rec1 and rec2, shouldn't affect cache");  delete rec; delete rec2;  show_status;  // Let's fetch another object  headline("Fetching second object");  rec2 = cache->getRecord(TEST_RECORD, "suhac");  show_record(rec2);  show_status;  // Get the first object again  headline("Retrieving first object (for third time, should be in cache)");  rec = cache->getRecord(TEST_RECORD, "colinc");  show_record(rec);  show_status;  // Modify local record  cout << " 2 \n";  headline("Changing record locally, shouldn't affect cache");  ((TestRecord*)rec)->setData("Cameron, Colin");  show_record(rec);  cout << " 2 \n";  show_status;    cout << newline << "=== Database Change Tests ===" << newline;  headline("Changing record 'colinc' in database");  cache->changeRecord(rec);  show_status;  headline("Wait for cache update, should remove record 'colinc'"); sleep(WAIT_TIME);  show_status;  headline("Changing record 'colinc' back to previous state");  ((TestRecord*)rec)->setData("Colin Cameron");  cache->changeRecord(rec);  show_status;  headline("Wait for cache update, can't remove absent record 'colinc'"); sleep(WAIT_TIME);  show_status;  headline("Fetch Record 'unknown'");  rec2 = cache->getRecord(TEST_RECORD, "unknown");  show_record(rec2);  show_status;    headline("Wait for cache purge"); sleep(WAIT_TIME);  show_status;  headline("Delete record 'unknown' for the database");  cache->deleteRecord(rec2);  show_status;  headline("Wait for cache update"); sleep(WAIT_TIME);  show_status;  headline("And reinsert record 'unknown' to the the database");  cache->insertRecord(rec2);  show_status;  headline("See what the cache does"); sleep(WAIT_TIME);  show_status;  // Insert into db (will not change anything really)  cout << newline << "testCache.cxx: Destroying Cache" << newline;  cache->destroy();    return 0;}int test2(void) {     tout << "Cache Test Starting ..." << newline << newline;  tout << "Getting instance of cache" << newline;  Cache* cache = Cache::getInstance();  tout << "Got Cache" << newline << newline;  show_status;  // Now do some actual tests  headline("Init cache");  cache->initCache(TEST_RECORD);  show_status;    // Get simple record  headline("Get 3 records (changes timestamps)");  Record* rec3 = cache->getRecord(TEST_RECORD, "unknown");  sleep(1);  Record* rec2 = cache->getRecord(TEST_RECORD, "suhac");  sleep(1);  Record* rec = cache->getRecord(TEST_RECORD, "colinc");  show_record(rec);  show_status; sleep(WAIT_TIME);  show_status;  cout << newline << "testCache.cxx: Destroying Cache" << newline;  cache->destroy();    return 0;}void call_back_func(RecordType rType, KeyType key) {   cout << "testCache.cxx: **** Call Back Function Called\n";  cout << "testCache.cxx: **** " << Record::stringRecordType(rType)        << " with key " << key << " was updated !\n";  cout << "testCache.cxx: ****\n";}int test3(void) {     tout << "Cache Test Starting ..." << newline << newline;  tout << "Getting instance of cache" << newline;  Cache* cache = Cache::getInstance();  tout << "Got Cache" << newline << newline;  show_status;  // Now do some actual tests  string hostname = "127.0.0.1";  headline("Register for updates ...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;    headline("... and deregister for updates");  cout << "Returns : "        << cache->deregisterForUpdates(hostname, TEST_RECORD) << newline;  show_status;  headline("Register for updates ...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;    headline("Register for updates (should fail)...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;  headline("... and deregister for updates");  cout << "Returns : "        << cache->deregisterForUpdates(hostname, TEST_RECORD) << newline;  show_status;  headline("Register for updates ...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;  // Test the automatic shutdown  cout << newline << "testCache.cxx: Destroying Cache" << newline;  cache->destroy();    return 0;}int test4(void) {     tout << "Cache Test Starting ..." << newline << newline;  tout << "Getting instance of cache" << newline;  Cache* cache = Cache::getInstance();  tout << "Got Cache" << newline << newline;  show_status;  // Now do some actual tests  string hostname = "127.0.0.1";  headline("Register for updates ...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;  // Get simple record  headline("Get record 'colinc'");  Record* rec;  if (!(rec = cache->getRecord(TEST_RECORD, "colinc"))) {     // Fail   };  show_record(rec);  show_status;  // Modify local record  cout << " 2 \n";  ((TestRecord*)rec)->setData("Cameron, Colin");  headline("Changing record 'colinc' in database");  cache->changeRecord(rec);  show_status;  headline("Wait for cache update, should remove record 'colinc'"); sleep(WAIT_TIME);  show_status;  headline("Changing record 'colinc' back to previous state");  ((TestRecord*)rec)->setData("Colin Cameron");  cache->changeRecord(rec);  show_status;  // ************** Force Update **********************  headline("Test update ...");  /*  { // Scope these variables ...    try {       NetworkAddress netAddr(6000);      TcpClientSocket mySocket(netAddr, true, false);        mySocket.connect(); // connect      Connection& conn = mySocket.getConn();      string s = "colinc";      conn.writeData(s);      }     catch (VException &e) {       cout << "Failed \n";    }    }*/  // **********************  headline("Force update now"); show_status;  headline("Wait for cache update, can't remove absent record 'colinc'"); sleep(WAIT_TIME);  show_status;  headline("... and deregister for updates");  cout << "Returns : "        << cache->deregisterForUpdates(hostname, TEST_RECORD) << newline;  show_status;// Test the automatic shutdown  cout << newline << "testCache.cxx: Destroying Cache" << newline;  cache->destroy();    return 0;}int test5(void) {     tout << "Cache Test Starting ..." << newline << newline;  tout << "Getting instance of cache" << newline;  Cache* cache = Cache::getInstance();  tout << "Got Cache" << newline << newline;  show_status;  // Now do some actual tests  string hostname = "127.0.0.1";  headline("Register for updates ...");  cout << "Returns : "        << cache->registerForUpdates(hostname, TEST_RECORD, call_back_func)       << newline;  show_status;  c = 'z';  while (c != 'x') {     // Get simple record    headline("Repear get record 'colinc' [ hit 'x' then return to quit ]");    Record* rec;    if (!(rec = cache->getRecord(TEST_RECORD, "colinc"))) {       cout << "Record not in database, shouldn't be in cache!\n";    } else {       show_record(rec);    }    show_status;  }  headline("... and deregister for updates");  cout << "Returns : "        << cache->deregisterForUpdates(hostname, TEST_RECORD) << newline;  show_status;// Test the automatic shutdown  cout << newline << "testCache.cxx: Destroying Cache" << newline;  cache->destroy();    return 0;}int main(int argc, char* argv[]) {      // set to LOG_DEBUG using the enum / macro   cpLogSetPriority(LOG_DEBUG);       // log to the file output.log   if(!cpLogOpen("testCache.log"))   {       cerr << "could not log to testCache.log" << endl;   }  while (argc > 1) {     cout << "Running test " << argv[1][0] << "!\n\n";    switch (argv[1][0]) {       case '1': return test1(); break;      case '2': return test2(); break;      case '3': return test3(); break;      case '4': return test4(); break;      case '5': return test5(); break;      default: return test1(); break;    }    argc--;  } }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?