Salome HOME
Try to kill Salome application when a YACS Launcher job is killed
[modules/kernel.git] / src / SALOMELocalTrace / Test / SALOMELocalTraceTest.cxx
1 // Copyright (C) 2007-2011  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
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   const char *theFileName = TRACEFILE;
68
69   std::string s = "file:";
70   s += theFileName;
71   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
72
73   std::ofstream traceFile;
74   traceFile.open(theFileName, std::ios::out | std::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   std::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, &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       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   const char *theFileName = TRACEFILE;
138
139   std::string s = "file:";
140   s += theFileName;
141   //s = "local";
142   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
143
144   std::ofstream traceFile;
145   traceFile.open(theFileName, std::ios::out | std::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, &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       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 #if defined(_DEBUG_) || defined(_DEBUG)
184   long id_thread = (long)threadid;
185   for (int i=0; i<NUM_MESSAGES;i++)
186   MESSAGE("Hello World! This is a trace test : " << id_thread 
187         << " - iter " << i);
188 #endif
189   pthread_exit(NULL);
190 }