1 // Copyright (C) 2007-2011 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
23 #include "SALOMELocalTraceTest.hxx"
29 #include "LocalTraceBufferPool.hxx"
30 #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 const char *theFileName = TRACEFILE;
69 std::string s = "file:";
71 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
73 std::ofstream traceFile;
74 traceFile.open(theFileName, std::ios::out | std::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()
100 std::string s = "local";
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, &t) ;
111 CPPUNIT_ASSERT( !rc);
114 // --- wait for end of each thread producing trace.
116 for(t=0;t<numThread;t++)
118 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 const char *theFileName = TRACEFILE;
139 std::string s = "file:";
142 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
144 std::ofstream traceFile;
145 traceFile.open(theFileName, std::ios::out | std::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, &t) ;
157 CPPUNIT_ASSERT( !rc);
160 // --- wait for end of each thread producing trace.
162 for(t=0;t<NUM_THREADS;t++)
164 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 #if defined(_DEBUG_) || defined(_DEBUG)
184 long id_thread = (long)threadid;
185 for (int i=0; i<NUM_MESSAGES;i++)
186 MESSAGE("Hello World! This is a trace test : " << id_thread