lockT.cc

Go to the documentation of this file.
00001 // lockT.C
00002 
00003 #include <iostream>
00004 
00005 using std::cerr ;
00006 using std::cout ;
00007 using std::endl ;
00008 
00009 #include "lockT.h"
00010 #include "BESCache.h"
00011 #include "BESException.h"
00012 #include <test_config.h>
00013 
00014 int
00015 lockT::run(void)
00016 {
00017     cout << endl << "*****************************************" << endl;
00018     cout << "Entered lockT::run" << endl;
00019     int retVal = 0;
00020 
00021     try
00022     {
00023         string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00024         BESCache cache( cache_dir, "lock_test", 1 ) ;
00025 
00026         cout << endl << "*****************************************" << endl;
00027         cout << "lock, then try to lock again" << endl;
00028         try
00029         {
00030             cout << "get first lock" << endl ;
00031             if( cache.lock( 2, 10 ) == false )
00032             {
00033                 cerr << "failed to lock the cache" << endl ;
00034                 return 1 ;
00035             }
00036         }
00037         catch( BESException &e )
00038         {
00039             cerr << "locking test failed" << endl ;
00040             cerr << e.get_message() << endl ;
00041             cache.unlock() ;
00042             return 1 ;
00043         }
00044         catch( ... )
00045         {
00046             cerr << "locking test failed" << endl ;
00047             cerr << "Unknown error" << endl ;
00048             cache.unlock() ;
00049             return 1 ;
00050         }
00051 
00052         try
00053         {
00054             cout << "try to lock again" << endl ;
00055             if( cache.lock( 2, 10 ) == true )
00056             {
00057                 cerr << "successfully got the lock, should not have" << endl ;
00058                 cache.unlock() ;
00059                 return 1 ;
00060             }
00061         }
00062         catch( BESException &e )
00063         {
00064             cout << "failed to get lock, good" << endl ;
00065             cout << e.get_message() << endl ;
00066         }
00067         catch( ... )
00068         {
00069             cerr << "failed to get lock, unkown exception" << endl ;
00070             cache.unlock() ;
00071             return 1 ;
00072         }
00073 
00074         cout << endl << "*****************************************" << endl;
00075         cout << "unlock" << endl;
00076         if( cache.unlock() == false )
00077         {
00078             cerr << "failed to release the lock" << endl ;
00079             return 1 ;
00080         }
00081 
00082         cout << endl << "*****************************************" << endl;
00083         cout << "lock the cache, create another cache and try to lock" << endl;
00084         try
00085         {
00086             cout << "locking first" << endl;
00087             if( cache.lock( 2, 10 ) == false )
00088             {
00089                 cerr << "failed to lock the cache" << endl ;
00090                 return 1 ;
00091             }
00092         }
00093         catch( BESException &e )
00094         {
00095             cerr << "2 cache locking failed" << endl ;
00096             cerr << e.get_message() << endl ;
00097             cache.unlock() ;
00098             return 1 ;
00099         }
00100         catch( ... )
00101         {
00102             cerr << "2 cache locking failed" << endl ;
00103             cerr << "Unknown error" << endl ;
00104             cache.unlock() ;
00105             return 1 ;
00106         }
00107 
00108         cout << "creating second" << endl;
00109         BESCache cache2( cache_dir, "lock_test", 1 ) ;
00110         try
00111         {
00112             cout << "locking second" << endl;
00113             if( cache2.lock( 2, 10 ) == false )
00114             {
00115                 cout << "failed to lock the cache, good" << endl ;
00116             }
00117         }
00118         catch( BESException &e )
00119         {
00120             cerr << "2 cache locking failed" << endl ;
00121             cerr << e.get_message() << endl ;
00122             cache.unlock() ;
00123             return 1 ;
00124         }
00125         catch( ... )
00126         {
00127             cerr << "2 cache locking failed" << endl ;
00128             cerr << "Unknown error" << endl ;
00129             cache.unlock() ;
00130             return 1 ;
00131         }
00132 
00133         cout << endl << "*****************************************" << endl;
00134         cout << "unlock the first cache" << endl;
00135         if( cache.unlock() == false )
00136         {
00137             cerr << "failed to release the lock" << endl ;
00138             return 1 ;
00139         }
00140 
00141         cout << endl << "*****************************************" << endl;
00142         cout << "lock the second cache" << endl;
00143         try
00144         {
00145             if( cache2.lock( 2, 10 ) == true )
00146             {
00147                 cout << "got the lock, good" << endl ;
00148             }
00149         }
00150         catch( BESException &e )
00151         {
00152             cerr << "locking second cache failed" << endl ;
00153             cerr << e.get_message() << endl ;
00154             cache.unlock() ;
00155             return 1 ;
00156         }
00157         catch( ... )
00158         {
00159             cerr << "locking second cache failed" << endl ;
00160             cerr << "Unknown error" << endl ;
00161             cache.unlock() ;
00162             return 1 ;
00163         }
00164 
00165         cout << endl << "*****************************************" << endl;
00166         cout << "unlock the second cache" << endl;
00167         if( cache2.unlock() == false )
00168         {
00169             cerr << "failed to release the lock" << endl ;
00170             return 1 ;
00171         }
00172     }
00173     catch( BESException &e )
00174     {
00175         cerr << "Failed to use the cache" << endl ;
00176         cerr << e.get_message() << endl ;
00177         return 1 ;
00178     }
00179     catch( ... )
00180     {
00181         cerr << "Failed to use the cache" << endl ;
00182         cerr << "Unknown error" << endl ;
00183         return 1 ;
00184     }
00185 
00186     cout << endl << "*****************************************" << endl;
00187     cout << "Returning from lockT::run" << endl;
00188 
00189     return retVal;
00190 }
00191 
00192 int
00193 main(int argC, char **argV) {
00194     string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/bes.conf" ;
00195     putenv( (char *)env_var.c_str() ) ;
00196     Application *app = new lockT();
00197     return app->main(argC, argV);
00198 }
00199 

Generated on Sat Jan 19 04:05:37 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4