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