1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
21 #include "SALOMELocalTraceTest.hxx"
27 #include "LocalTraceBufferPool.hxx"
28 #include "utilities.h"
33 // ============================================================================
37 // ============================================================================
40 SALOMELocalTraceTest::setUp()
44 // ============================================================================
48 // ============================================================================
51 SALOMELocalTraceTest::tearDown()
55 #define TRACEFILE "/tmp/traceUnitTest.log"
57 // ============================================================================
59 * Open and close a trace on a file, test singleton
61 // ============================================================================
64 SALOMELocalTraceTest::testSingletonBufferPool()
67 char *theFileName = TRACEFILE;
71 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
74 traceFile.open(theFileName, ios::out | ios::app);
75 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
78 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
80 LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
81 CPPUNIT_ASSERT(bp1 == bp2);
82 bp1->deleteInstance(bp1);
88 #define NUM_MESSAGES 5
89 void *PrintHello(void *threadid);
91 // ============================================================================
93 * open a trace on console, multithread writing on file, close
95 // ============================================================================
98 SALOMELocalTraceTest::testLoadBufferPoolLocal()
101 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
103 // --- numThread thread creation for trace generation.
105 pthread_t threads[numThread];
107 for(t=0;t<numThread;t++)
109 MESSAGE("Creating thread " << t);
110 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
111 CPPUNIT_ASSERT( !rc);
114 // --- wait for end of each thread producing trace.
116 for(t=0;t<numThread;t++)
118 int ret = pthread_join(threads[t], NULL);
119 MESSAGE("--------------------- end of PrintHello thread " << t);
121 MESSAGE(" ---- end of PrintHello threads ---- ");
123 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
125 bp1->deleteInstance(bp1);
128 // ============================================================================
130 * open a trace on a file, multithread writing on file, close
132 // ============================================================================
135 SALOMELocalTraceTest::testLoadBufferPoolFile()
137 char *theFileName = TRACEFILE;
142 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
145 traceFile.open(theFileName, ios::out | ios::trunc);
146 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
149 // --- NUM_THREADS thread creation for trace generation.
151 pthread_t threads[NUM_THREADS];
153 for(t=0;t<NUM_THREADS;t++)
155 MESSAGE("Creating thread " << t);
156 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
157 CPPUNIT_ASSERT( !rc);
160 // --- wait for end of each thread producing trace.
162 for(t=0;t<NUM_THREADS;t++)
164 int ret = pthread_join(threads[t], NULL);
165 MESSAGE("--------------------- end of PrintHello thread " << t);
167 MESSAGE(" ---- end of PrintHello threads ---- ");
169 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
171 bp1->deleteInstance(bp1);
174 // ============================================================================
176 * NUM_THREAD are created with function PrintHello,
177 * which produces NUM_MESSAGES traces.
179 // ============================================================================
181 void *PrintHello(void *threadid)
183 int id_thread = (int)threadid;
184 for (int i=0; i<NUM_MESSAGES;i++)
185 MESSAGE("Hello World! This is a trace test : " << id_thread