Back to index

salome-kernel  6.5.0
SALOMELocalTraceTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SALOMELocalTraceTest.hxx"
00024 
00025 #include <iostream>
00026 #include <fstream>
00027 #include <string>
00028 #include <cstdlib>
00029 #include "LocalTraceBufferPool.hxx"
00030 #include "utilities.h"
00031 
00032 
00033 // ============================================================================
00037 // ============================================================================
00038 
00039 void 
00040 SALOMELocalTraceTest::setUp()
00041 {
00042 }
00043 
00044 // ============================================================================
00048 // ============================================================================
00049 
00050 void 
00051 SALOMELocalTraceTest::tearDown()
00052 {
00053 }
00054 
00055 #define TRACEFILE "/tmp/traceUnitTest.log"
00056 
00057 // ============================================================================
00061 // ============================================================================
00062 
00063 void 
00064 SALOMELocalTraceTest::testSingletonBufferPool()
00065 {
00066   // --- trace on file
00067   const char *theFileName = TRACEFILE;
00068 
00069   std::string s = "file:";
00070   s += theFileName;
00071   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
00072 
00073   std::ofstream traceFile;
00074   traceFile.open(theFileName, std::ios::out | std::ios::app);
00075   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
00076   traceFile.close();
00077 
00078   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
00079   CPPUNIT_ASSERT(bp1);
00080   LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
00081   CPPUNIT_ASSERT(bp1 == bp2);
00082   bp1->deleteInstance(bp1);
00083 }
00084 
00085 
00086 
00087 #define NUM_THREADS  2
00088 #define NUM_MESSAGES 5
00089 void *PrintHello(void *threadid);
00090 
00091 // ============================================================================
00095 // ============================================================================
00096 
00097 void 
00098 SALOMELocalTraceTest::testLoadBufferPoolLocal()
00099 {
00100   std::string s = "local";
00101   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
00102 
00103   // --- numThread thread creation for trace generation.
00104   int numThread = 2;
00105   pthread_t threads[numThread];
00106   int rc, t;
00107   for(t=0;t<numThread;t++)
00108     {
00109       MESSAGE("Creating thread " << t);
00110       rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
00111       CPPUNIT_ASSERT( !rc);
00112     }
00113 
00114   // --- wait for end of each thread producing trace.
00115 
00116   for(t=0;t<numThread;t++)
00117     {
00118       pthread_join(threads[t], NULL);
00119       MESSAGE("--------------------- end of PrintHello thread " << t);
00120     }
00121   MESSAGE(" ---- end of PrintHello threads ---- ");
00122 
00123   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
00124   CPPUNIT_ASSERT(bp1);
00125   bp1->deleteInstance(bp1);
00126 }
00127 
00128 // ============================================================================
00132 // ============================================================================
00133 
00134 void 
00135 SALOMELocalTraceTest::testLoadBufferPoolFile()
00136 {
00137   const char *theFileName = TRACEFILE;
00138 
00139   std::string s = "file:";
00140   s += theFileName;
00141   //s = "local";
00142   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
00143 
00144   std::ofstream traceFile;
00145   traceFile.open(theFileName, std::ios::out | std::ios::trunc);
00146   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
00147   traceFile.close();
00148 
00149   // --- NUM_THREADS thread creation for trace generation.
00150 
00151   pthread_t threads[NUM_THREADS];
00152   int rc, t;
00153   for(t=0;t<NUM_THREADS;t++)
00154     {
00155       MESSAGE("Creating thread " << t);
00156       rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
00157       CPPUNIT_ASSERT( !rc);
00158     }
00159 
00160   // --- wait for end of each thread producing trace.
00161 
00162   for(t=0;t<NUM_THREADS;t++)
00163     {
00164       pthread_join(threads[t], NULL);
00165       MESSAGE("--------------------- end of PrintHello thread " << t);
00166     }
00167   MESSAGE(" ---- end of PrintHello threads ---- ");
00168 
00169   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
00170   CPPUNIT_ASSERT(bp1);
00171   bp1->deleteInstance(bp1);
00172 }
00173 
00174 // ============================================================================
00179 // ============================================================================
00180 
00181 void *PrintHello(void *threadid)
00182 {
00183 #if defined(_DEBUG_) || defined(_DEBUG)
00184   long id_thread = (long)threadid;
00185   for (int i=0; i<NUM_MESSAGES;i++)
00186   MESSAGE("Hello World! This is a trace test : " << id_thread 
00187         << " - iter " << i);
00188 #endif
00189   pthread_exit(NULL);
00190 }