]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMELocalTrace/Test/SALOMELocalTraceTest.cxx
Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMELocalTrace / Test / SALOMELocalTraceTest.cxx
1
2 #include "SALOMELocalTraceTest.hxx"
3
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <cstdlib>
8 #include "LocalTraceBufferPool.hxx"
9 #include "utilities.h"
10
11 using namespace std;
12
13
14 // ============================================================================
15 /*!
16  *
17  */
18 // ============================================================================
19
20 void 
21 SALOMELocalTraceTest::setUp()
22 {
23 }
24
25 // ============================================================================
26 /*!
27  *
28  */
29 // ============================================================================
30
31 void 
32 SALOMELocalTraceTest::tearDown()
33 {
34 }
35
36 #define TRACEFILE "/tmp/traceUnitTest.log"
37
38 // ============================================================================
39 /*!
40  *  Open and close a trace on a file, test singleton
41  */
42 // ============================================================================
43
44 void 
45 SALOMELocalTraceTest::testSingletonBufferPool()
46 {
47   // --- trace on file
48   char *theFileName = TRACEFILE;
49
50   string s = "file:";
51   s += theFileName;
52   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
53
54   ofstream traceFile;
55   traceFile.open(theFileName, ios::out | ios::app);
56   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
57   traceFile.close();
58
59   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
60   CPPUNIT_ASSERT(bp1);
61   LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance();
62   CPPUNIT_ASSERT(bp1 == bp2);
63   bp1->deleteInstance(bp1);
64 }
65
66
67
68 #define NUM_THREADS  2
69 #define NUM_MESSAGES 5
70 void *PrintHello(void *threadid);
71
72 // ============================================================================
73 /*!
74  *  open a trace on console, multithread writing on file, close
75  */
76 // ============================================================================
77
78 void 
79 SALOMELocalTraceTest::testLoadBufferPoolLocal()
80 {
81   string s = "local";
82   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
83
84   // --- numThread thread creation for trace generation.
85   int numThread = 2;
86   pthread_t threads[numThread];
87   int rc, t;
88   for(t=0;t<numThread;t++)
89     {
90       MESSAGE("Creating thread " << t);
91       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
92       CPPUNIT_ASSERT( !rc);
93     }
94
95   // --- wait for end of each thread producing trace.
96
97   for(t=0;t<numThread;t++)
98     {
99       int ret = pthread_join(threads[t], NULL);
100       MESSAGE("--------------------- end of PrintHello thread " << t);
101     }
102   MESSAGE(" ---- end of PrintHello threads ---- ");
103
104   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
105   CPPUNIT_ASSERT(bp1);
106   bp1->deleteInstance(bp1);
107 }
108
109 // ============================================================================
110 /*!
111  *  open a trace on a file, multithread writing on file, close
112  */
113 // ============================================================================
114
115 void 
116 SALOMELocalTraceTest::testLoadBufferPoolFile()
117 {
118   char *theFileName = TRACEFILE;
119
120   string s = "file:";
121   s += theFileName;
122   //s = "local";
123   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
124
125   ofstream traceFile;
126   traceFile.open(theFileName, ios::out | ios::trunc);
127   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
128   traceFile.close();
129
130   // --- NUM_THREADS thread creation for trace generation.
131
132   pthread_t threads[NUM_THREADS];
133   int rc, t;
134   for(t=0;t<NUM_THREADS;t++)
135     {
136       MESSAGE("Creating thread " << t);
137       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t) ;
138       CPPUNIT_ASSERT( !rc);
139     }
140
141   // --- wait for end of each thread producing trace.
142
143   for(t=0;t<NUM_THREADS;t++)
144     {
145       int ret = pthread_join(threads[t], NULL);
146       MESSAGE("--------------------- end of PrintHello thread " << t);
147     }
148   MESSAGE(" ---- end of PrintHello threads ---- ");
149
150   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
151   CPPUNIT_ASSERT(bp1);
152   bp1->deleteInstance(bp1);
153 }
154
155 // ============================================================================
156 /*!
157  * NUM_THREAD are created with function PrintHello,
158  * which produces NUM_MESSAGES traces.
159  */
160 // ============================================================================
161
162 void *PrintHello(void *threadid)
163 {
164   int id_thread = (int)threadid;
165   for (int i=0; i<NUM_MESSAGES;i++)
166     MESSAGE("Hello World! This is a trace test : " << id_thread 
167             << " - iter " << i);
168   pthread_exit(NULL);
169 }