1 // Copyright (C) 2007-2020 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, or (at your option) any later version.
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"
30 #include "LocalTraceBufferPool.hxx"
31 #include "utilities.h"
32 #include "Basics_Utils.hxx"
33 #include "Basics_DirUtils.hxx"
36 #define setenv Kernel_Utils::setenv
39 // ============================================================================
43 // ============================================================================
46 SALOMELocalTraceTest::setUp()
50 // ============================================================================
54 // ============================================================================
57 SALOMELocalTraceTest::tearDown()
62 SALOMELocalTraceTest::_getTraceFileName()
64 std::string dir = Kernel_Utils::GetTmpDir();
65 return dir + "traceUnitTest-SALOMELocalTraceTest.log";
68 // ============================================================================
70 * Open and close a trace on a file, test singleton
72 // ============================================================================
75 SALOMELocalTraceTest::testSingletonBufferPool()
78 std::string theFileName = _getTraceFileName();
80 std::string s = "file:";
82 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
84 std::ofstream traceFile;
85 traceFile.open(theFileName.c_str(), std::ios::out | std::ios::app);
86 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
89 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
91 LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
92 CPPUNIT_ASSERT(bp1 == bp2);
93 bp1->deleteInstance(bp1);
99 #define NUM_MESSAGES 5
100 void *PrintHello(void *threadid);
102 // ============================================================================
104 * open a trace on console, multithread writing on file, close
106 // ============================================================================
109 SALOMELocalTraceTest::testLoadBufferPoolLocal()
111 std::string s = "local";
112 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
114 // --- NUM_THREADS thread creation for trace generation.
115 pthread_t threads[NUM_THREADS];
117 for(t=0;t<NUM_THREADS;t++)
119 MESSAGE("Creating thread " << t);
120 rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
121 CPPUNIT_ASSERT( !rc);
124 // --- wait for end of each thread producing trace.
126 for(t=0;t<NUM_THREADS;t++)
128 pthread_join(threads[t], NULL);
129 MESSAGE("--------------------- end of PrintHello thread " << t);
131 MESSAGE(" ---- end of PrintHello threads ---- ");
133 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
135 bp1->deleteInstance(bp1);
138 // ============================================================================
140 * open a trace on a file, multithread writing on file, close
142 // ============================================================================
145 SALOMELocalTraceTest::testLoadBufferPoolFile()
147 std::string theFileName = _getTraceFileName();
149 std::string s = "file:";
152 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
154 std::ofstream traceFile;
155 traceFile.open(theFileName.c_str(), std::ios::out | std::ios::trunc);
156 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
159 // --- NUM_THREADS thread creation for trace generation.
161 pthread_t threads[NUM_THREADS];
163 for(t=0;t<NUM_THREADS;t++)
165 MESSAGE("Creating thread " << t);
166 rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
167 CPPUNIT_ASSERT( !rc);
170 // --- wait for end of each thread producing trace.
172 for(t=0;t<NUM_THREADS;t++)
174 pthread_join(threads[t], NULL);
175 MESSAGE("--------------------- end of PrintHello thread " << t);
177 MESSAGE(" ---- end of PrintHello threads ---- ");
179 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
181 bp1->deleteInstance(bp1);
184 // ============================================================================
186 * NUM_THREAD are created with function PrintHello,
187 * which produces NUM_MESSAGES traces.
189 // ============================================================================
191 void *PrintHello(void *threadid)
193 SALOME_UNUSED(threadid); // unused in release mode
194 #if defined(_DEBUG_) || defined(_DEBUG)
195 long id_thread = (long)threadid;
196 for (int i=0; i<NUM_MESSAGES;i++)
197 MESSAGE("Hello World! This is a trace test : " << id_thread