[23] | 1 | /**
|
---|
[35] | 2 | * @file test.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
| 4 | * @version 0.1
|
---|
[14] | 5 | */
|
---|
| 6 | #include "cf_file.h"
|
---|
| 7 | #include "cf_log.h"
|
---|
| 8 | #include "cf_socket.h"
|
---|
| 9 | #include "cf_thread.h"
|
---|
| 10 |
|
---|
[25] | 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 |
|
---|
[15] | 17 | #include <stdlib.h>
|
---|
[21] | 18 | #include <string.h>
|
---|
| 19 | #include <errno.h>
|
---|
[15] | 20 |
|
---|
[21] | 21 | #define THREAD_POOL 5
|
---|
| 22 | #define PORT 1234
|
---|
| 23 |
|
---|
[14] | 24 | CF_Debug_Ctx gDebugCtx;
|
---|
| 25 | CF_Debug_CallStack gDebugCallstack;
|
---|
[49] | 26 | CF_Mutex globalMutex;
|
---|
[14] | 27 |
|
---|
[40] | 28 | #define LOG_SOCKET 0
|
---|
| 29 | #define LOG_MT 1
|
---|
[21] | 30 |
|
---|
[15] | 31 | const char * file = "./log.txt";
|
---|
| 32 |
|
---|
[38] | 33 | CF_THREAD_RETURN CF_THREAD_CALL test_log_mt (void * arg)
|
---|
| 34 | {
|
---|
[49] | 35 | static int cnt = 0;
|
---|
[38] | 36 | int i = 0;
|
---|
[49] | 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 | }
|
---|
[38] | 46 |
|
---|
[49] | 47 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
| 48 |
|
---|
[38] | 49 | for (i = 0 ; i < 100000 ; i++)
|
---|
| 50 | {
|
---|
[40] | 51 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
[38] | 52 | }
|
---|
| 53 |
|
---|
[49] | 54 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
| 55 |
|
---|
[38] | 56 | return (CF_THREAD_RETURN)0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[24] | 59 | void test_log (const char * message)
|
---|
[14] | 60 | {
|
---|
[19] | 61 | int i, j;
|
---|
| 62 | char idname[16] = {0x00,};
|
---|
[38] | 63 | CF_Thread tid[10];
|
---|
[15] | 64 |
|
---|
[14] | 65 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 66 |
|
---|
[21] | 67 | /* initialize */
|
---|
[40] | 68 | if (CF_Log_Initialize (10) < 0)
|
---|
| 69 | {
|
---|
| 70 | fprintf (stderr, "failed to init. log\n");
|
---|
| 71 | return ;
|
---|
| 72 | }
|
---|
[19] | 73 |
|
---|
| 74 | for (i = 0 ; i < 10 ; i++)
|
---|
| 75 | {
|
---|
| 76 | sprintf (idname, "logid%d.txt", i);
|
---|
[40] | 77 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
| 78 | fprintf (stderr, "failed to open log\n");
|
---|
[19] | 79 | }
|
---|
| 80 |
|
---|
| 81 | for (i = 0 ; i < 10 ; i++)
|
---|
| 82 | {
|
---|
| 83 | for (j = 0 ; j < 10000 ; j++)
|
---|
[40] | 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 | }
|
---|
[19] | 89 |
|
---|
[40] | 90 | if (CF_Log_Close (i) < 0)
|
---|
| 91 | fprintf (stderr, "failed to close log\n");
|
---|
[19] | 92 | }
|
---|
| 93 |
|
---|
[38] | 94 | /* mt {{{ */
|
---|
[40] | 95 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
[38] | 96 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
| 97 |
|
---|
[40] | 98 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
[38] | 99 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
| 100 |
|
---|
[49] | 101 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
| 102 | // error
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[38] | 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 |
|
---|
[49] | 114 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
| 115 | // error
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[38] | 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 |
|
---|
[21] | 125 | /* finalize */
|
---|
[19] | 126 | CF_Log_Finalize ();
|
---|
[14] | 127 | }
|
---|
| 128 |
|
---|
[24] | 129 | void test_file (const char * message)
|
---|
[14] | 130 | {
|
---|
[15] | 131 | int fd = 0;
|
---|
[21] | 132 | char buffer[128] = {0x00,};
|
---|
[14] | 133 |
|
---|
[15] | 134 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 135 |
|
---|
[16] | 136 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
[15] | 137 | if (fd < 0)
|
---|
| 138 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
|
---|
| 139 |
|
---|
[38] | 140 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
[15] | 141 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
|
---|
| 142 | else
|
---|
[21] | 143 | CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
|
---|
[38] | 144 | sizeof (buffer),
|
---|
[21] | 145 | "-- %d bytes of %d bytes\n",
|
---|
[38] | 146 | sizeof (buffer),
|
---|
[21] | 147 | CF_File_GetSize (fd));
|
---|
[15] | 148 |
|
---|
| 149 | CF_File_Close (fd);
|
---|
| 150 |
|
---|
| 151 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[24] | 154 | void test_callstack3 (void)
|
---|
[15] | 155 | {
|
---|
| 156 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 157 | while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
|
---|
[14] | 158 | {
|
---|
[15] | 159 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
| 160 | gDebugCallstack.file,
|
---|
| 161 | gDebugCallstack.line,
|
---|
| 162 | gDebugCallstack.function);
|
---|
[14] | 163 | }
|
---|
[15] | 164 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 165 | }
|
---|
[24] | 166 | void test_callstack2 (void)
|
---|
[15] | 167 | {
|
---|
| 168 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 169 | test_callstack3 ();
|
---|
[15] | 170 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 171 | }
|
---|
[24] | 172 | void test_callstack1 (void)
|
---|
[15] | 173 | {
|
---|
| 174 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 175 | test_callstack2 ();
|
---|
[15] | 176 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 177 | }
|
---|
[14] | 178 |
|
---|
[21] | 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,};
|
---|
[38] | 185 | int i = 0;
|
---|
[21] | 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 | }
|
---|
[40] | 194 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
[21] | 195 |
|
---|
[38] | 196 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 197 | {
|
---|
[38] | 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 | }
|
---|
[40] | 203 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
[21] | 204 |
|
---|
[38] | 205 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
| 206 | {
|
---|
| 207 | return (CF_THREAD_RETURN)-3;
|
---|
| 208 | }
|
---|
[40] | 209 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
[21] | 210 | }
|
---|
| 211 |
|
---|
[38] | 212 | CF_Socket_Close (clntsock);
|
---|
| 213 |
|
---|
[21] | 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,};
|
---|
[38] | 223 | int i = 0;
|
---|
[21] | 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 | }
|
---|
[40] | 234 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
[21] | 235 |
|
---|
[38] | 236 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 237 | {
|
---|
[38] | 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 | }
|
---|
[40] | 243 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
[21] | 244 |
|
---|
[38] | 245 | memset (buf, 0x00, sizeof (buf));
|
---|
[21] | 246 |
|
---|
[38] | 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 | }
|
---|
[40] | 252 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
[21] | 253 | }
|
---|
| 254 |
|
---|
| 255 | CF_Socket_Close (sock);
|
---|
| 256 |
|
---|
| 257 | return (CF_THREAD_RETURN)0;
|
---|
| 258 | /*------------------------------------------------------------*/
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[24] | 261 | void test_socket (void)
|
---|
[21] | 262 | {
|
---|
| 263 | CF_Thread tid[THREAD_POOL];
|
---|
| 264 |
|
---|
| 265 | int sock = 0;
|
---|
| 266 | int iter = 0;
|
---|
| 267 |
|
---|
| 268 | /*------------------------------------------------------------*/
|
---|
[40] | 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)
|
---|
[21] | 276 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
| 277 |
|
---|
[40] | 278 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
[21] | 279 |
|
---|
| 280 | if (CF_Socket_Initialize () < 0)
|
---|
| 281 | {
|
---|
| 282 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
| 283 | return ;
|
---|
| 284 | }
|
---|
[40] | 285 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
[21] | 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 | }
|
---|
[40] | 293 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
[21] | 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 | }
|
---|
[40] | 302 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
[21] | 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 | }
|
---|
[40] | 313 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
[21] | 314 | }
|
---|
| 315 |
|
---|
| 316 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 317 | {
|
---|
| 318 | CF_Thread_Join (&tid[iter]);
|
---|
| 319 | CF_Thread_Release (&tid[iter]);
|
---|
[40] | 320 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
[21] | 321 | }
|
---|
| 322 |
|
---|
| 323 | CF_Socket_Close (sock);
|
---|
| 324 |
|
---|
| 325 | CF_Socket_Finalize ();
|
---|
| 326 |
|
---|
[40] | 327 | CF_Log_Close (LOG_SOCKET);
|
---|
| 328 |
|
---|
| 329 | CF_Log_Finalize ();
|
---|
[21] | 330 | /*------------------------------------------------------------*/
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[15] | 333 | int main (int argc, char ** argv)
|
---|
| 334 | {
|
---|
| 335 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
| 336 |
|
---|
[14] | 337 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 338 |
|
---|
[21] | 339 | // 1
|
---|
[16] | 340 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
[24] | 341 | test_log ("LOG_TEST");
|
---|
[14] | 342 |
|
---|
[21] | 343 | // 2
|
---|
[16] | 344 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
[24] | 345 | test_file ("FILE_READ_TEST");
|
---|
[15] | 346 |
|
---|
[21] | 347 | // 3
|
---|
[16] | 348 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
[24] | 349 | test_callstack1 ();
|
---|
[15] | 350 |
|
---|
[21] | 351 | // 4
|
---|
[38] | 352 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
|
---|
[24] | 353 | test_socket ();
|
---|
[21] | 354 |
|
---|
[14] | 355 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 356 |
|
---|
[16] | 357 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
[14] | 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 | }
|
---|
[21] | 367 |
|
---|