Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / SALOMELocalTrace / Test / SALOMELocalTraceTest.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include "SALOMELocalTraceTest.hxx"
22
23 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <cstdlib>
27 #include "LocalTraceBufferPool.hxx"
28 #include "utilities.h"
29
30 using namespace std;
31
32
33 // ============================================================================
34 /*!
35  *
36  */
37 // ============================================================================
38
39 void 
40 SALOMELocalTraceTest::setUp()
41 {
42 }
43
44 // ============================================================================
45 /*!
46  *
47  */
48 // ============================================================================
49
50 void 
51 SALOMELocalTraceTest::tearDown()
52 {
53 }
54
55 #define TRACEFILE "/tmp/traceUnitTest.log"
56
57 // ============================================================================
58 /*!
59  *  Open and close a trace on a file, test singleton
60  */
61 // ============================================================================
62
63 void 
64 SALOMELocalTraceTest::testSingletonBufferPool()
65 {
66   // --- trace on file
67   char *theFileName = TRACEFILE;
68
69   string s = "file:";
70   s += theFileName;
71   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
72
73   ofstream traceFile;
74   traceFile.open(theFileName, ios::out | ios::app);
75   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
76   traceFile.close();
77
78   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
79   CPPUNIT_ASSERT(bp1);
80   LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
81   CPPUNIT_ASSERT(bp1 == bp2);
82   bp1->deleteInstance(bp1);
83 }
84
85
86
87 #define NUM_THREADS  2
88 #define NUM_MESSAGES 5
89 void *PrintHello(void *threadid);
90
91 // ============================================================================
92 /*!
93  *  open a trace on console, multithread writing on file, close
94  */
95 // ============================================================================
96
97 void 
98 SALOMELocalTraceTest::testLoadBufferPoolLocal()
99 {
100   string s = "local";
101   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
102
103   // --- numThread thread creation for trace generation.
104   int numThread = 2;
105   pthread_t threads[numThread];
106   int rc, t;
107   for(t=0;t<numThread;t++)
108     {
109       MESSAGE("Creating thread " << t);
110       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
111       CPPUNIT_ASSERT( !rc);
112     }
113
114   // --- wait for end of each thread producing trace.
115
116   for(t=0;t<numThread;t++)
117     {
118       int ret = pthread_join(threads[t], NULL);
119       MESSAGE("--------------------- end of PrintHello thread " << t);
120     }
121   MESSAGE(" ---- end of PrintHello threads ---- ");
122
123   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
124   CPPUNIT_ASSERT(bp1);
125   bp1->deleteInstance(bp1);
126 }
127
128 // ============================================================================
129 /*!
130  *  open a trace on a file, multithread writing on file, close
131  */
132 // ============================================================================
133
134 void 
135 SALOMELocalTraceTest::testLoadBufferPoolFile()
136 {
137   char *theFileName = TRACEFILE;
138
139   string s = "file:";
140   s += theFileName;
141   //s = "local";
142   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
143
144   ofstream traceFile;
145   traceFile.open(theFileName, ios::out | ios::trunc);
146   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
147   traceFile.close();
148
149   // --- NUM_THREADS thread creation for trace generation.
150
151   pthread_t threads[NUM_THREADS];
152   int rc, t;
153   for(t=0;t<NUM_THREADS;t++)
154     {
155       MESSAGE("Creating thread " << t);
156       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
157       CPPUNIT_ASSERT( !rc);
158     }
159
160   // --- wait for end of each thread producing trace.
161
162   for(t=0;t<NUM_THREADS;t++)
163     {
164       int ret = pthread_join(threads[t], NULL);
165       MESSAGE("--------------------- end of PrintHello thread " << t);
166     }
167   MESSAGE(" ---- end of PrintHello threads ---- ");
168
169   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
170   CPPUNIT_ASSERT(bp1);
171   bp1->deleteInstance(bp1);
172 }
173
174 // ============================================================================
175 /*!
176  * NUM_THREAD are created with function PrintHello,
177  * which produces NUM_MESSAGES traces.
178  */
179 // ============================================================================
180
181 void *PrintHello(void *threadid)
182 {
183   long id_thread = (long)threadid;
184   for (int i=0; i<NUM_MESSAGES;i++)
185   MESSAGE("Hello World! This is a trace test : " << id_thread 
186         << " - iter " << i);
187   pthread_exit(NULL);
188 }