[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;
|
---|
| 26 |
|
---|
[40] | 27 | #define LOG_SOCKET 0
|
---|
| 28 | #define LOG_MT 1
|
---|
[21] | 29 |
|
---|
[15] | 30 | const char * file = "./log.txt";
|
---|
| 31 |
|
---|
[38] | 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 | {
|
---|
[40] | 39 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
[38] | 40 | }
|
---|
| 41 |
|
---|
| 42 | return (CF_THREAD_RETURN)0;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[24] | 45 | void test_log (const char * message)
|
---|
[14] | 46 | {
|
---|
[19] | 47 | int i, j;
|
---|
| 48 | char idname[16] = {0x00,};
|
---|
[38] | 49 | CF_Thread tid[10];
|
---|
[15] | 50 |
|
---|
[14] | 51 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 52 |
|
---|
[21] | 53 | /* initialize */
|
---|
[40] | 54 | if (CF_Log_Initialize (10) < 0)
|
---|
| 55 | {
|
---|
| 56 | fprintf (stderr, "failed to init. log\n");
|
---|
| 57 | return ;
|
---|
| 58 | }
|
---|
[19] | 59 |
|
---|
| 60 | for (i = 0 ; i < 10 ; i++)
|
---|
| 61 | {
|
---|
| 62 | sprintf (idname, "logid%d.txt", i);
|
---|
[40] | 63 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
| 64 | fprintf (stderr, "failed to open log\n");
|
---|
[19] | 65 | }
|
---|
| 66 |
|
---|
| 67 | for (i = 0 ; i < 10 ; i++)
|
---|
| 68 | {
|
---|
| 69 | for (j = 0 ; j < 10000 ; j++)
|
---|
[40] | 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 | }
|
---|
[19] | 75 |
|
---|
[40] | 76 | if (CF_Log_Close (i) < 0)
|
---|
| 77 | fprintf (stderr, "failed to close log\n");
|
---|
[19] | 78 | }
|
---|
| 79 |
|
---|
[38] | 80 | /* mt {{{ */
|
---|
[40] | 81 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
[38] | 82 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
| 83 |
|
---|
[40] | 84 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
[38] | 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 |
|
---|
[21] | 104 | /* finalize */
|
---|
[19] | 105 | CF_Log_Finalize ();
|
---|
[14] | 106 | }
|
---|
| 107 |
|
---|
[24] | 108 | void test_file (const char * message)
|
---|
[14] | 109 | {
|
---|
[15] | 110 | int fd = 0;
|
---|
[21] | 111 | char buffer[128] = {0x00,};
|
---|
[14] | 112 |
|
---|
[15] | 113 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 114 |
|
---|
[16] | 115 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
[15] | 116 | if (fd < 0)
|
---|
| 117 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
|
---|
| 118 |
|
---|
[38] | 119 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
[15] | 120 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
|
---|
| 121 | else
|
---|
[21] | 122 | CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
|
---|
[38] | 123 | sizeof (buffer),
|
---|
[21] | 124 | "-- %d bytes of %d bytes\n",
|
---|
[38] | 125 | sizeof (buffer),
|
---|
[21] | 126 | CF_File_GetSize (fd));
|
---|
[15] | 127 |
|
---|
| 128 | CF_File_Close (fd);
|
---|
| 129 |
|
---|
| 130 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[24] | 133 | void test_callstack3 (void)
|
---|
[15] | 134 | {
|
---|
| 135 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 136 | while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
|
---|
[14] | 137 | {
|
---|
[15] | 138 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
| 139 | gDebugCallstack.file,
|
---|
| 140 | gDebugCallstack.line,
|
---|
| 141 | gDebugCallstack.function);
|
---|
[14] | 142 | }
|
---|
[15] | 143 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 144 | }
|
---|
[24] | 145 | void test_callstack2 (void)
|
---|
[15] | 146 | {
|
---|
| 147 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 148 | test_callstack3 ();
|
---|
[15] | 149 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 150 | }
|
---|
[24] | 151 | void test_callstack1 (void)
|
---|
[15] | 152 | {
|
---|
| 153 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 154 | test_callstack2 ();
|
---|
[15] | 155 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 156 | }
|
---|
[14] | 157 |
|
---|
[21] | 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,};
|
---|
[38] | 164 | int i = 0;
|
---|
[21] | 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 | }
|
---|
[40] | 173 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
[21] | 174 |
|
---|
[38] | 175 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 176 | {
|
---|
[38] | 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 | }
|
---|
[40] | 182 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
[21] | 183 |
|
---|
[38] | 184 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
| 185 | {
|
---|
| 186 | return (CF_THREAD_RETURN)-3;
|
---|
| 187 | }
|
---|
[40] | 188 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
[21] | 189 | }
|
---|
| 190 |
|
---|
[38] | 191 | CF_Socket_Close (clntsock);
|
---|
| 192 |
|
---|
[21] | 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,};
|
---|
[38] | 202 | int i = 0;
|
---|
[21] | 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 | }
|
---|
[40] | 213 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
[21] | 214 |
|
---|
[38] | 215 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 216 | {
|
---|
[38] | 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 | }
|
---|
[40] | 222 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
[21] | 223 |
|
---|
[38] | 224 | memset (buf, 0x00, sizeof (buf));
|
---|
[21] | 225 |
|
---|
[38] | 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 | }
|
---|
[40] | 231 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
[21] | 232 | }
|
---|
| 233 |
|
---|
| 234 | CF_Socket_Close (sock);
|
---|
| 235 |
|
---|
| 236 | return (CF_THREAD_RETURN)0;
|
---|
| 237 | /*------------------------------------------------------------*/
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[24] | 240 | void test_socket (void)
|
---|
[21] | 241 | {
|
---|
| 242 | CF_Thread tid[THREAD_POOL];
|
---|
| 243 |
|
---|
| 244 | int sock = 0;
|
---|
| 245 | int iter = 0;
|
---|
| 246 | int reuse = 1;
|
---|
| 247 |
|
---|
| 248 | /*------------------------------------------------------------*/
|
---|
[40] | 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)
|
---|
[21] | 256 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
| 257 |
|
---|
[40] | 258 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
[21] | 259 |
|
---|
| 260 | if (CF_Socket_Initialize () < 0)
|
---|
| 261 | {
|
---|
| 262 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
| 263 | return ;
|
---|
| 264 | }
|
---|
[40] | 265 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
[21] | 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 | }
|
---|
[40] | 273 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
[21] | 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 | }
|
---|
[40] | 280 | CF_Log_Write (LOG_SOCKET, "SOCKET", "set socket option\n");
|
---|
[21] | 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 | }
|
---|
[40] | 289 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
[21] | 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 | }
|
---|
[40] | 300 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
[21] | 301 | }
|
---|
| 302 |
|
---|
| 303 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 304 | {
|
---|
| 305 | CF_Thread_Join (&tid[iter]);
|
---|
| 306 | CF_Thread_Release (&tid[iter]);
|
---|
[40] | 307 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
[21] | 308 | }
|
---|
| 309 |
|
---|
| 310 | CF_Socket_Close (sock);
|
---|
| 311 |
|
---|
| 312 | CF_Socket_Finalize ();
|
---|
| 313 |
|
---|
[40] | 314 | CF_Log_Close (LOG_SOCKET);
|
---|
| 315 |
|
---|
| 316 | CF_Log_Finalize ();
|
---|
[21] | 317 | /*------------------------------------------------------------*/
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[15] | 320 | int main (int argc, char ** argv)
|
---|
| 321 | {
|
---|
| 322 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
| 323 |
|
---|
[14] | 324 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 325 |
|
---|
[21] | 326 | // 1
|
---|
[16] | 327 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
[24] | 328 | test_log ("LOG_TEST");
|
---|
[14] | 329 |
|
---|
[21] | 330 | // 2
|
---|
[16] | 331 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
[24] | 332 | test_file ("FILE_READ_TEST");
|
---|
[15] | 333 |
|
---|
[21] | 334 | // 3
|
---|
[16] | 335 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
[24] | 336 | test_callstack1 ();
|
---|
[15] | 337 |
|
---|
[21] | 338 | // 4
|
---|
[38] | 339 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
|
---|
[24] | 340 | test_socket ();
|
---|
[21] | 341 |
|
---|
[14] | 342 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 343 |
|
---|
[16] | 344 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
[14] | 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 | }
|
---|
[21] | 354 |
|
---|