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