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