1 | /**
|
---|
2 | * @file test.c
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | */
|
---|
5 | #include "cf_file.h"
|
---|
6 | #include "cf_log.h"
|
---|
7 | #include "cf_socket.h"
|
---|
8 | #include "cf_thread.h"
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * before including "cf_debug.h",
|
---|
12 | * if _DEBUG is defined, the debugging code will be act.
|
---|
13 | */
|
---|
14 | #include "cf_debug.h"
|
---|
15 |
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include <errno.h>
|
---|
19 |
|
---|
20 | #define THREAD_POOL 5
|
---|
21 | #define PORT 1234
|
---|
22 |
|
---|
23 | CF_Mutex globalMutex;
|
---|
24 |
|
---|
25 | #define LOG_SOCKET 0
|
---|
26 | #define LOG_MT 1
|
---|
27 |
|
---|
28 | const char * file = "./log.txt";
|
---|
29 |
|
---|
30 | int test_log_mt (void * arg);
|
---|
31 | void test_log (const char * message);
|
---|
32 | void test_file (const char * message);
|
---|
33 | void test_callstack3 (void);
|
---|
34 | void test_callstack2 (void);
|
---|
35 | void test_callstack1 (void);
|
---|
36 | int socket_echo_server (void * arg);
|
---|
37 | int socket_echo_client (void * arg);
|
---|
38 | void test_socket (void);
|
---|
39 |
|
---|
40 | int main (int argc, char ** argv)
|
---|
41 | {
|
---|
42 | CF_DEBUG_INITIALIZE;
|
---|
43 |
|
---|
44 | // 1
|
---|
45 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
46 | test_log ("LOG_TEST");
|
---|
47 |
|
---|
48 | // 2
|
---|
49 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
50 | test_file ("FILE_READ_TEST");
|
---|
51 |
|
---|
52 | // 3
|
---|
53 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
54 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
55 | test_callstack1 ();
|
---|
56 | CF_DEBUG_END_FUNCTION;
|
---|
57 |
|
---|
58 | // 4
|
---|
59 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
|
---|
60 | test_socket ();
|
---|
61 |
|
---|
62 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
63 |
|
---|
64 | CF_DEBUG_FINALIZE;
|
---|
65 |
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | int test_log_mt (void * arg)
|
---|
70 | {
|
---|
71 | static int cnt = 0;
|
---|
72 | int i = 0;
|
---|
73 | int th = 0;
|
---|
74 |
|
---|
75 | if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section
|
---|
76 | // error
|
---|
77 | }
|
---|
78 | th = cnt++;
|
---|
79 | if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section
|
---|
80 | // error
|
---|
81 | }
|
---|
82 |
|
---|
83 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
84 |
|
---|
85 | for (i = 0 ; i < 100000 ; i++)
|
---|
86 | {
|
---|
87 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
88 | }
|
---|
89 |
|
---|
90 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
91 |
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void test_log (const char * message)
|
---|
96 | {
|
---|
97 | int i, j;
|
---|
98 | char idname[16] = {0x00,};
|
---|
99 | CF_Thread tid[10];
|
---|
100 |
|
---|
101 | /* initialize */
|
---|
102 | if (CF_Log_Initialize (10) < 0)
|
---|
103 | {
|
---|
104 | fprintf (stderr, "failed to init. log\n");
|
---|
105 | return ;
|
---|
106 | }
|
---|
107 |
|
---|
108 | for (i = 0 ; i < 10 ; i++)
|
---|
109 | {
|
---|
110 | sprintf (idname, "logid%d.txt", i);
|
---|
111 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
112 | fprintf (stderr, "failed to open log\n");
|
---|
113 | }
|
---|
114 |
|
---|
115 | for (i = 0 ; i < 10 ; i++)
|
---|
116 | {
|
---|
117 | for (j = 0 ; j < 10000 ; j++)
|
---|
118 | {
|
---|
119 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
120 | if (result < 0)
|
---|
121 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (CF_Log_Close (i) < 0)
|
---|
125 | fprintf (stderr, "failed to close log\n");
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* mt {{{ */
|
---|
129 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
130 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
131 |
|
---|
132 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
133 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
134 |
|
---|
135 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
136 | // error
|
---|
137 | }
|
---|
138 |
|
---|
139 | for (i = 0 ; i < 10 ; i++)
|
---|
140 | {
|
---|
141 | if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
|
---|
142 | {
|
---|
143 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
144 | return ;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | for (i = 0 ; i < 10 ; i++)
|
---|
149 | {
|
---|
150 | if (CF_Thread_Join (&tid[i]) < 0)
|
---|
151 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
152 | if (CF_Thread_Release (&tid[i]) < 0)
|
---|
153 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
157 | // error
|
---|
158 | }
|
---|
159 | /* }}} mt */
|
---|
160 |
|
---|
161 | /* finalize */
|
---|
162 | CF_Log_Finalize ();
|
---|
163 | }
|
---|
164 |
|
---|
165 | void test_file (const char * message)
|
---|
166 | {
|
---|
167 | int fd = 0;
|
---|
168 | char buffer[128] = {0x00,};
|
---|
169 |
|
---|
170 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
171 | if (fd < 0)
|
---|
172 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
173 |
|
---|
174 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
175 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
176 | else
|
---|
177 | CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
|
---|
178 | sizeof (buffer),
|
---|
179 | "-- %d bytes of %d bytes\n",
|
---|
180 | sizeof (buffer),
|
---|
181 | CF_File_GetSize (fd));
|
---|
182 |
|
---|
183 | CF_File_Close (fd);
|
---|
184 | }
|
---|
185 |
|
---|
186 | void test_callstack3 (void)
|
---|
187 | {
|
---|
188 | // ...
|
---|
189 |
|
---|
190 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
191 | CF_DEBUG_PRINT_CALLSTACK (stderr);
|
---|
192 | CF_DEBUG_END_FUNCTION;
|
---|
193 |
|
---|
194 | // ...
|
---|
195 | }
|
---|
196 | void test_callstack2 (void)
|
---|
197 | {
|
---|
198 | // ...
|
---|
199 |
|
---|
200 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
201 | test_callstack3 ();
|
---|
202 | CF_DEBUG_END_FUNCTION;
|
---|
203 |
|
---|
204 | // ...
|
---|
205 | }
|
---|
206 | void test_callstack1 (void)
|
---|
207 | {
|
---|
208 | // ...
|
---|
209 |
|
---|
210 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
211 | test_callstack2 ();
|
---|
212 | CF_DEBUG_END_FUNCTION;
|
---|
213 |
|
---|
214 | // ...
|
---|
215 | }
|
---|
216 |
|
---|
217 | int socket_echo_server (void * arg)
|
---|
218 | {
|
---|
219 | int srvsock = *((int *)arg);
|
---|
220 | int clntsock = 0;
|
---|
221 | int recvd = 0;
|
---|
222 | char buf[1024] = {0x00,};
|
---|
223 | int i = 0;
|
---|
224 |
|
---|
225 | /*------------------------------------------------------------*/
|
---|
226 | clntsock = CF_Socket_Accept (srvsock);
|
---|
227 | if (clntsock < 0)
|
---|
228 | {
|
---|
229 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
230 | return -1;
|
---|
231 | }
|
---|
232 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
233 |
|
---|
234 | for (i = 0 ; i < 100 ; i++)
|
---|
235 | {
|
---|
236 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
237 | {
|
---|
238 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
239 | return -2;
|
---|
240 | }
|
---|
241 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
242 |
|
---|
243 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
244 | {
|
---|
245 | return -3;
|
---|
246 | }
|
---|
247 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
248 | }
|
---|
249 |
|
---|
250 | CF_Socket_Close (clntsock);
|
---|
251 |
|
---|
252 | return 0;
|
---|
253 | /*------------------------------------------------------------*/
|
---|
254 | }
|
---|
255 |
|
---|
256 | int socket_echo_client (void * arg)
|
---|
257 | {
|
---|
258 | int sock = 0;
|
---|
259 | int recvd = 0;
|
---|
260 | char buf[1024] = {0x00,};
|
---|
261 | int i = 0;
|
---|
262 |
|
---|
263 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
264 |
|
---|
265 | /*------------------------------------------------------------*/
|
---|
266 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
267 | if (sock < 0)
|
---|
268 | {
|
---|
269 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
270 | return -1;
|
---|
271 | }
|
---|
272 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
273 |
|
---|
274 | for (i = 0 ; i < 100 ; i++)
|
---|
275 | {
|
---|
276 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
277 | {
|
---|
278 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
279 | return -2;
|
---|
280 | }
|
---|
281 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
282 |
|
---|
283 | memset (buf, 0x00, sizeof (buf));
|
---|
284 |
|
---|
285 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
286 | {
|
---|
287 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
288 | return -3;
|
---|
289 | }
|
---|
290 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
291 | }
|
---|
292 |
|
---|
293 | CF_Socket_Close (sock);
|
---|
294 |
|
---|
295 | return 0;
|
---|
296 | /*------------------------------------------------------------*/
|
---|
297 | }
|
---|
298 |
|
---|
299 | void test_socket (void)
|
---|
300 | {
|
---|
301 | CF_Thread stid[THREAD_POOL];
|
---|
302 | CF_Thread ctid[THREAD_POOL];
|
---|
303 |
|
---|
304 | int sock = 0;
|
---|
305 | int iter = 0;
|
---|
306 |
|
---|
307 | /*------------------------------------------------------------*/
|
---|
308 | if (CF_Log_Initialize (1) < 0)
|
---|
309 | {
|
---|
310 | fprintf (stderr, "failed to init. log\n");
|
---|
311 | return ;
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
315 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
316 |
|
---|
317 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
318 |
|
---|
319 | if (CF_Socket_Initialize () < 0)
|
---|
320 | {
|
---|
321 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
322 | return ;
|
---|
323 | }
|
---|
324 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
325 |
|
---|
326 | sock = CF_Socket_Server (PORT, 5);
|
---|
327 | if (sock < 0)
|
---|
328 | {
|
---|
329 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
330 | return ;
|
---|
331 | }
|
---|
332 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
333 |
|
---|
334 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
335 | {
|
---|
336 | if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
337 | {
|
---|
338 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
339 | return ;
|
---|
340 | }
|
---|
341 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
342 | }
|
---|
343 |
|
---|
344 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
345 | {
|
---|
346 | if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
347 | {
|
---|
348 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
349 | return ;
|
---|
350 | }
|
---|
351 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
352 | }
|
---|
353 |
|
---|
354 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
355 | {
|
---|
356 | CF_Thread_Join (&ctid[iter]);
|
---|
357 | CF_Thread_Release (&ctid[iter]);
|
---|
358 |
|
---|
359 | CF_Thread_Join (&stid[iter]);
|
---|
360 | CF_Thread_Release (&stid[iter]);
|
---|
361 |
|
---|
362 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
363 | }
|
---|
364 |
|
---|
365 | CF_Socket_Close (sock);
|
---|
366 |
|
---|
367 | CF_Socket_Finalize ();
|
---|
368 |
|
---|
369 | CF_Log_Close (LOG_SOCKET);
|
---|
370 |
|
---|
371 | CF_Log_Finalize ();
|
---|
372 | /*------------------------------------------------------------*/
|
---|
373 | }
|
---|
374 |
|
---|