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