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