1 // Copyright (C) 2007-2014 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"
29 #include "LocalTraceBufferPool.hxx"
30 #include "utilities.h"
31 #include "Basics_Utils.hxx"
34 #define setenv Kernel_Utils::setenv
37 // ============================================================================
41 // ============================================================================
44 SALOMELocalTraceTest::setUp()
48 // ============================================================================
52 // ============================================================================
55 SALOMELocalTraceTest::tearDown()
59 #define TRACEFILE "/tmp/traceUnitTest.log"
61 // ============================================================================
63 * Open and close a trace on a file, test singleton
65 // ============================================================================
68 SALOMELocalTraceTest::testSingletonBufferPool()
71 const char *theFileName = TRACEFILE;
73 std::string s = "file:";
75 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
77 std::ofstream traceFile;
78 traceFile.open(theFileName, std::ios::out | std::ios::app);
79 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
82 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
84 LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
85 CPPUNIT_ASSERT(bp1 == bp2);
86 bp1->deleteInstance(bp1);
92 #define NUM_MESSAGES 5
93 void *PrintHello(void *threadid);
95 // ============================================================================
97 * open a trace on console, multithread writing on file, close
99 // ============================================================================
102 SALOMELocalTraceTest::testLoadBufferPoolLocal()
104 std::string s = "local";
105 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
107 // --- NUM_THREADS thread creation for trace generation.
108 pthread_t threads[NUM_THREADS];
110 for(t=0;t<NUM_THREADS;t++)
112 MESSAGE("Creating thread " << t);
113 rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
114 CPPUNIT_ASSERT( !rc);
117 // --- wait for end of each thread producing trace.
119 for(t=0;t<NUM_THREADS;t++)
121 pthread_join(threads[t], NULL);
122 MESSAGE("--------------------- end of PrintHello thread " << t);
124 MESSAGE(" ---- end of PrintHello threads ---- ");
126 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
128 bp1->deleteInstance(bp1);
131 // ============================================================================
133 * open a trace on a file, multithread writing on file, close
135 // ============================================================================
138 SALOMELocalTraceTest::testLoadBufferPoolFile()
140 const char *theFileName = TRACEFILE;
142 std::string s = "file:";
145 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
147 std::ofstream traceFile;
148 traceFile.open(theFileName, std::ios::out | std::ios::trunc);
149 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
152 // --- NUM_THREADS thread creation for trace generation.
154 pthread_t threads[NUM_THREADS];
156 for(t=0;t<NUM_THREADS;t++)
158 MESSAGE("Creating thread " << t);
159 rc = pthread_create(&threads[t], NULL, PrintHello, &t) ;
160 CPPUNIT_ASSERT( !rc);
163 // --- wait for end of each thread producing trace.
165 for(t=0;t<NUM_THREADS;t++)
167 pthread_join(threads[t], NULL);
168 MESSAGE("--------------------- end of PrintHello thread " << t);
170 MESSAGE(" ---- end of PrintHello threads ---- ");
172 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
174 bp1->deleteInstance(bp1);
177 // ============================================================================
179 * NUM_THREAD are created with function PrintHello,
180 * which produces NUM_MESSAGES traces.
182 // ============================================================================
184 void *PrintHello(void *threadid)
186 #if defined(_DEBUG_) || defined(_DEBUG)
187 long id_thread = (long)threadid;
188 for (int i=0; i<NUM_MESSAGES;i++)
189 MESSAGE("Hello World! This is a trace test : " << id_thread