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