1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "SALOMELocalTraceTest.hxx"
28 #include "LocalTraceBufferPool.hxx"
29 #include "utilities.h"
34 // ============================================================================
38 // ============================================================================
41 SALOMELocalTraceTest::setUp()
45 // ============================================================================
49 // ============================================================================
52 SALOMELocalTraceTest::tearDown()
56 #define TRACEFILE "/tmp/traceUnitTest.log"
58 // ============================================================================
60 * Open and close a trace on a file, test singleton
62 // ============================================================================
65 SALOMELocalTraceTest::testSingletonBufferPool()
68 const char *theFileName = TRACEFILE;
72 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
75 traceFile.open(theFileName, ios::out | ios::app);
76 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
79 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
81 LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
82 CPPUNIT_ASSERT(bp1 == bp2);
83 bp1->deleteInstance(bp1);
89 #define NUM_MESSAGES 5
90 void *PrintHello(void *threadid);
92 // ============================================================================
94 * open a trace on console, multithread writing on file, close
96 // ============================================================================
99 SALOMELocalTraceTest::testLoadBufferPoolLocal()
102 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
104 // --- numThread thread creation for trace generation.
106 pthread_t threads[numThread];
108 for(t=0;t<numThread;t++)
110 MESSAGE("Creating thread " << t);
111 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
112 CPPUNIT_ASSERT( !rc);
115 // --- wait for end of each thread producing trace.
117 for(t=0;t<numThread;t++)
119 pthread_join(threads[t], NULL);
120 MESSAGE("--------------------- end of PrintHello thread " << t);
122 MESSAGE(" ---- end of PrintHello threads ---- ");
124 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
126 bp1->deleteInstance(bp1);
129 // ============================================================================
131 * open a trace on a file, multithread writing on file, close
133 // ============================================================================
136 SALOMELocalTraceTest::testLoadBufferPoolFile()
138 const char *theFileName = TRACEFILE;
143 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
146 traceFile.open(theFileName, ios::out | ios::trunc);
147 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
150 // --- NUM_THREADS thread creation for trace generation.
152 pthread_t threads[NUM_THREADS];
154 for(t=0;t<NUM_THREADS;t++)
156 MESSAGE("Creating thread " << t);
157 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
158 CPPUNIT_ASSERT( !rc);
161 // --- wait for end of each thread producing trace.
163 for(t=0;t<NUM_THREADS;t++)
165 pthread_join(threads[t], NULL);
166 MESSAGE("--------------------- end of PrintHello thread " << t);
168 MESSAGE(" ---- end of PrintHello threads ---- ");
170 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
172 bp1->deleteInstance(bp1);
175 // ============================================================================
177 * NUM_THREAD are created with function PrintHello,
178 * which produces NUM_MESSAGES traces.
180 // ============================================================================
182 void *PrintHello(void *threadid)
184 #if defined(_DEBUG_) || defined(_DEBUG)
185 long id_thread = (long)threadid;
186 for (int i=0; i<NUM_MESSAGES;i++)
187 MESSAGE("Hello World! This is a trace test : " << id_thread