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