Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / SALOMELocalTrace / Test / SALOMELocalTraceTest.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SALOMELocalTraceTest.hxx"
23
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27 #include <cstdlib>
28 #include "LocalTraceBufferPool.hxx"
29 #include "utilities.h"
30
31 using namespace std;
32
33
34 // ============================================================================
35 /*!
36  *
37  */
38 // ============================================================================
39
40 void 
41 SALOMELocalTraceTest::setUp()
42 {
43 }
44
45 // ============================================================================
46 /*!
47  *
48  */
49 // ============================================================================
50
51 void 
52 SALOMELocalTraceTest::tearDown()
53 {
54 }
55
56 #define TRACEFILE "/tmp/traceUnitTest.log"
57
58 // ============================================================================
59 /*!
60  *  Open and close a trace on a file, test singleton
61  */
62 // ============================================================================
63
64 void 
65 SALOMELocalTraceTest::testSingletonBufferPool()
66 {
67   // --- trace on file
68   const char *theFileName = TRACEFILE;
69
70   string s = "file:";
71   s += theFileName;
72   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
73
74   ofstream traceFile;
75   traceFile.open(theFileName, ios::out | ios::app);
76   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
77   traceFile.close();
78
79   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
80   CPPUNIT_ASSERT(bp1);
81   LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
82   CPPUNIT_ASSERT(bp1 == bp2);
83   bp1->deleteInstance(bp1);
84 }
85
86
87
88 #define NUM_THREADS  2
89 #define NUM_MESSAGES 5
90 void *PrintHello(void *threadid);
91
92 // ============================================================================
93 /*!
94  *  open a trace on console, multithread writing on file, close
95  */
96 // ============================================================================
97
98 void 
99 SALOMELocalTraceTest::testLoadBufferPoolLocal()
100 {
101   string s = "local";
102   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
103
104   // --- numThread thread creation for trace generation.
105   int numThread = 2;
106   pthread_t threads[numThread];
107   int rc, t;
108   for(t=0;t<numThread;t++)
109     {
110       MESSAGE("Creating thread " << t);
111       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
112       CPPUNIT_ASSERT( !rc);
113     }
114
115   // --- wait for end of each thread producing trace.
116
117   for(t=0;t<numThread;t++)
118     {
119       pthread_join(threads[t], NULL);
120       MESSAGE("--------------------- end of PrintHello thread " << t);
121     }
122   MESSAGE(" ---- end of PrintHello threads ---- ");
123
124   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
125   CPPUNIT_ASSERT(bp1);
126   bp1->deleteInstance(bp1);
127 }
128
129 // ============================================================================
130 /*!
131  *  open a trace on a file, multithread writing on file, close
132  */
133 // ============================================================================
134
135 void 
136 SALOMELocalTraceTest::testLoadBufferPoolFile()
137 {
138   const char *theFileName = TRACEFILE;
139
140   string s = "file:";
141   s += theFileName;
142   //s = "local";
143   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
144
145   ofstream traceFile;
146   traceFile.open(theFileName, ios::out | ios::trunc);
147   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
148   traceFile.close();
149
150   // --- NUM_THREADS thread creation for trace generation.
151
152   pthread_t threads[NUM_THREADS];
153   int rc, t;
154   for(t=0;t<NUM_THREADS;t++)
155     {
156       MESSAGE("Creating thread " << t);
157       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
158       CPPUNIT_ASSERT( !rc);
159     }
160
161   // --- wait for end of each thread producing trace.
162
163   for(t=0;t<NUM_THREADS;t++)
164     {
165       pthread_join(threads[t], NULL);
166       MESSAGE("--------------------- end of PrintHello thread " << t);
167     }
168   MESSAGE(" ---- end of PrintHello threads ---- ");
169
170   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
171   CPPUNIT_ASSERT(bp1);
172   bp1->deleteInstance(bp1);
173 }
174
175 // ============================================================================
176 /*!
177  * NUM_THREAD are created with function PrintHello,
178  * which produces NUM_MESSAGES traces.
179  */
180 // ============================================================================
181
182 void *PrintHello(void *threadid)
183 {
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 
188         << " - iter " << i);
189 #endif
190   pthread_exit(NULL);
191 }