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 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
45 |
|
---|
46 | // 1
|
---|
47 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
48 | test_log ("LOG_TEST");
|
---|
49 |
|
---|
50 | // 2
|
---|
51 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
52 | test_file ("FILE_READ_TEST");
|
---|
53 |
|
---|
54 | // 3
|
---|
55 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
56 | test_callstack1 ();
|
---|
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_END_FUNCTION;
|
---|
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 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
103 |
|
---|
104 | /* initialize */
|
---|
105 | if (CF_Log_Initialize (10) < 0)
|
---|
106 | {
|
---|
107 | fprintf (stderr, "failed to init. log\n");
|
---|
108 | return ;
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (i = 0 ; i < 10 ; i++)
|
---|
112 | {
|
---|
113 | sprintf (idname, "logid%d.txt", i);
|
---|
114 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
115 | fprintf (stderr, "failed to open log\n");
|
---|
116 | }
|
---|
117 |
|
---|
118 | for (i = 0 ; i < 10 ; i++)
|
---|
119 | {
|
---|
120 | for (j = 0 ; j < 10000 ; j++)
|
---|
121 | {
|
---|
122 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
123 | if (result < 0)
|
---|
124 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (CF_Log_Close (i) < 0)
|
---|
128 | fprintf (stderr, "failed to close log\n");
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* mt {{{ */
|
---|
132 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
133 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
134 |
|
---|
135 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
136 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
137 |
|
---|
138 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
139 | // error
|
---|
140 | }
|
---|
141 |
|
---|
142 | for (i = 0 ; i < 10 ; i++)
|
---|
143 | {
|
---|
144 | if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
|
---|
145 | {
|
---|
146 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
147 | return ;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | for (i = 0 ; i < 10 ; i++)
|
---|
152 | {
|
---|
153 | if (CF_Thread_Join (&tid[i]) < 0)
|
---|
154 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
155 | if (CF_Thread_Release (&tid[i]) < 0)
|
---|
156 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
160 | // error
|
---|
161 | }
|
---|
162 | /* }}} mt */
|
---|
163 |
|
---|
164 | /* finalize */
|
---|
165 | CF_Log_Finalize ();
|
---|
166 |
|
---|
167 | CF_DEBUG_END_FUNCTION;
|
---|
168 | }
|
---|
169 |
|
---|
170 | void test_file (const char * message)
|
---|
171 | {
|
---|
172 | int fd = 0;
|
---|
173 | char buffer[128] = {0x00,};
|
---|
174 |
|
---|
175 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
176 |
|
---|
177 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
178 | if (fd < 0)
|
---|
179 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
180 |
|
---|
181 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
182 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
183 | else
|
---|
184 | CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
|
---|
185 | sizeof (buffer),
|
---|
186 | "-- %d bytes of %d bytes\n",
|
---|
187 | sizeof (buffer),
|
---|
188 | CF_File_GetSize (fd));
|
---|
189 |
|
---|
190 | CF_File_Close (fd);
|
---|
191 |
|
---|
192 | CF_DEBUG_END_FUNCTION;
|
---|
193 | }
|
---|
194 |
|
---|
195 | void test_callstack3 (void)
|
---|
196 | {
|
---|
197 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
198 | CF_DEBUG_PRINT_CALLSTACK (stdout);
|
---|
199 | CF_DEBUG_END_FUNCTION;
|
---|
200 | }
|
---|
201 | void test_callstack2 (void)
|
---|
202 | {
|
---|
203 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
204 | test_callstack3 ();
|
---|
205 | CF_DEBUG_END_FUNCTION;
|
---|
206 | }
|
---|
207 | void test_callstack1 (void)
|
---|
208 | {
|
---|
209 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
210 | test_callstack2 ();
|
---|
211 | CF_DEBUG_END_FUNCTION;
|
---|
212 | }
|
---|
213 |
|
---|
214 | int socket_echo_server (void * arg)
|
---|
215 | {
|
---|
216 | int srvsock = *((int *)arg);
|
---|
217 | int clntsock = 0;
|
---|
218 | int recvd = 0;
|
---|
219 | char buf[1024] = {0x00,};
|
---|
220 | int i = 0;
|
---|
221 |
|
---|
222 | /*------------------------------------------------------------*/
|
---|
223 | clntsock = CF_Socket_Accept (srvsock);
|
---|
224 | if (clntsock < 0)
|
---|
225 | {
|
---|
226 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
227 | return -1;
|
---|
228 | }
|
---|
229 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
230 |
|
---|
231 | for (i = 0 ; i < 100 ; i++)
|
---|
232 | {
|
---|
233 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
234 | {
|
---|
235 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
236 | return -2;
|
---|
237 | }
|
---|
238 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
239 |
|
---|
240 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
241 | {
|
---|
242 | return -3;
|
---|
243 | }
|
---|
244 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
245 | }
|
---|
246 |
|
---|
247 | CF_Socket_Close (clntsock);
|
---|
248 |
|
---|
249 | return 0;
|
---|
250 | /*------------------------------------------------------------*/
|
---|
251 | }
|
---|
252 |
|
---|
253 | int socket_echo_client (void * arg)
|
---|
254 | {
|
---|
255 | int sock = 0;
|
---|
256 | int recvd = 0;
|
---|
257 | char buf[1024] = {0x00,};
|
---|
258 | int i = 0;
|
---|
259 |
|
---|
260 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
261 |
|
---|
262 | /*------------------------------------------------------------*/
|
---|
263 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
264 | if (sock < 0)
|
---|
265 | {
|
---|
266 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
267 | return -1;
|
---|
268 | }
|
---|
269 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
270 |
|
---|
271 | for (i = 0 ; i < 100 ; i++)
|
---|
272 | {
|
---|
273 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
274 | {
|
---|
275 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
276 | return -2;
|
---|
277 | }
|
---|
278 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
279 |
|
---|
280 | memset (buf, 0x00, sizeof (buf));
|
---|
281 |
|
---|
282 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
283 | {
|
---|
284 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
285 | return -3;
|
---|
286 | }
|
---|
287 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
288 | }
|
---|
289 |
|
---|
290 | CF_Socket_Close (sock);
|
---|
291 |
|
---|
292 | return 0;
|
---|
293 | /*------------------------------------------------------------*/
|
---|
294 | }
|
---|
295 |
|
---|
296 | void test_socket (void)
|
---|
297 | {
|
---|
298 | CF_Thread stid[THREAD_POOL];
|
---|
299 | CF_Thread ctid[THREAD_POOL];
|
---|
300 |
|
---|
301 | int sock = 0;
|
---|
302 | int iter = 0;
|
---|
303 |
|
---|
304 | /*------------------------------------------------------------*/
|
---|
305 | if (CF_Log_Initialize (1) < 0)
|
---|
306 | {
|
---|
307 | fprintf (stderr, "failed to init. log\n");
|
---|
308 | return ;
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
312 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
313 |
|
---|
314 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
315 |
|
---|
316 | if (CF_Socket_Initialize () < 0)
|
---|
317 | {
|
---|
318 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
319 | return ;
|
---|
320 | }
|
---|
321 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
322 |
|
---|
323 | sock = CF_Socket_Server (PORT, 5);
|
---|
324 | if (sock < 0)
|
---|
325 | {
|
---|
326 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
327 | return ;
|
---|
328 | }
|
---|
329 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
330 |
|
---|
331 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
332 | {
|
---|
333 | if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
334 | {
|
---|
335 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
336 | return ;
|
---|
337 | }
|
---|
338 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
339 | }
|
---|
340 |
|
---|
341 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
342 | {
|
---|
343 | if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
344 | {
|
---|
345 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
346 | return ;
|
---|
347 | }
|
---|
348 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
349 | }
|
---|
350 |
|
---|
351 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
352 | {
|
---|
353 | CF_Thread_Join (&ctid[iter]);
|
---|
354 | CF_Thread_Release (&ctid[iter]);
|
---|
355 |
|
---|
356 | CF_Thread_Join (&stid[iter]);
|
---|
357 | CF_Thread_Release (&stid[iter]);
|
---|
358 |
|
---|
359 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
360 | }
|
---|
361 |
|
---|
362 | CF_Socket_Close (sock);
|
---|
363 |
|
---|
364 | CF_Socket_Finalize ();
|
---|
365 |
|
---|
366 | CF_Log_Close (LOG_SOCKET);
|
---|
367 |
|
---|
368 | CF_Log_Finalize ();
|
---|
369 | /*------------------------------------------------------------*/
|
---|
370 | }
|
---|
371 |
|
---|