[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 |
|
---|
[49] | 24 | CF_Mutex globalMutex;
|
---|
[14] | 25 |
|
---|
[40] | 26 | #define LOG_SOCKET 0
|
---|
| 27 | #define LOG_MT 1
|
---|
[21] | 28 |
|
---|
[15] | 29 | const char * file = "./log.txt";
|
---|
| 30 |
|
---|
[63] | 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 |
|
---|
[57] | 70 | int test_log_mt (void * arg)
|
---|
[38] | 71 | {
|
---|
[49] | 72 | static int cnt = 0;
|
---|
[38] | 73 | int i = 0;
|
---|
[49] | 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 | }
|
---|
[38] | 83 |
|
---|
[49] | 84 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
| 85 |
|
---|
[38] | 86 | for (i = 0 ; i < 100000 ; i++)
|
---|
| 87 | {
|
---|
[40] | 88 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
[38] | 89 | }
|
---|
| 90 |
|
---|
[49] | 91 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
| 92 |
|
---|
[57] | 93 | return 0;
|
---|
[38] | 94 | }
|
---|
| 95 |
|
---|
[24] | 96 | void test_log (const char * message)
|
---|
[14] | 97 | {
|
---|
[19] | 98 | int i, j;
|
---|
| 99 | char idname[16] = {0x00,};
|
---|
[38] | 100 | CF_Thread tid[10];
|
---|
[15] | 101 |
|
---|
[51] | 102 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[14] | 103 |
|
---|
[21] | 104 | /* initialize */
|
---|
[40] | 105 | if (CF_Log_Initialize (10) < 0)
|
---|
| 106 | {
|
---|
| 107 | fprintf (stderr, "failed to init. log\n");
|
---|
| 108 | return ;
|
---|
| 109 | }
|
---|
[19] | 110 |
|
---|
| 111 | for (i = 0 ; i < 10 ; i++)
|
---|
| 112 | {
|
---|
| 113 | sprintf (idname, "logid%d.txt", i);
|
---|
[40] | 114 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
| 115 | fprintf (stderr, "failed to open log\n");
|
---|
[19] | 116 | }
|
---|
| 117 |
|
---|
| 118 | for (i = 0 ; i < 10 ; i++)
|
---|
| 119 | {
|
---|
| 120 | for (j = 0 ; j < 10000 ; j++)
|
---|
[40] | 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 | }
|
---|
[19] | 126 |
|
---|
[40] | 127 | if (CF_Log_Close (i) < 0)
|
---|
| 128 | fprintf (stderr, "failed to close log\n");
|
---|
[19] | 129 | }
|
---|
| 130 |
|
---|
[38] | 131 | /* mt {{{ */
|
---|
[40] | 132 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
[38] | 133 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
| 134 |
|
---|
[40] | 135 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
[38] | 136 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
| 137 |
|
---|
[49] | 138 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
| 139 | // error
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[38] | 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);
|
---|
[62] | 155 | if (CF_Thread_Release (&tid[i]) < 0)
|
---|
| 156 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
[38] | 157 | }
|
---|
[61] | 158 |
|
---|
| 159 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
| 160 | // error
|
---|
| 161 | }
|
---|
[38] | 162 | /* }}} mt */
|
---|
| 163 |
|
---|
[21] | 164 | /* finalize */
|
---|
[19] | 165 | CF_Log_Finalize ();
|
---|
[51] | 166 |
|
---|
| 167 | CF_DEBUG_END_FUNCTION;
|
---|
[14] | 168 | }
|
---|
| 169 |
|
---|
[24] | 170 | void test_file (const char * message)
|
---|
[14] | 171 | {
|
---|
[15] | 172 | int fd = 0;
|
---|
[21] | 173 | char buffer[128] = {0x00,};
|
---|
[14] | 174 |
|
---|
[51] | 175 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[15] | 176 |
|
---|
[16] | 177 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
[15] | 178 | if (fd < 0)
|
---|
[51] | 179 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
[15] | 180 |
|
---|
[38] | 181 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
[51] | 182 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
[15] | 183 | else
|
---|
[51] | 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));
|
---|
[15] | 189 |
|
---|
| 190 | CF_File_Close (fd);
|
---|
| 191 |
|
---|
[51] | 192 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 193 | }
|
---|
| 194 |
|
---|
[24] | 195 | void test_callstack3 (void)
|
---|
[15] | 196 | {
|
---|
[51] | 197 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[63] | 198 | CF_DEBUG_PRINT_CALLSTACK (stdout);
|
---|
[51] | 199 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 200 | }
|
---|
[24] | 201 | void test_callstack2 (void)
|
---|
[15] | 202 | {
|
---|
[51] | 203 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[24] | 204 | test_callstack3 ();
|
---|
[51] | 205 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 206 | }
|
---|
[24] | 207 | void test_callstack1 (void)
|
---|
[15] | 208 | {
|
---|
[51] | 209 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[24] | 210 | test_callstack2 ();
|
---|
[51] | 211 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 212 | }
|
---|
[14] | 213 |
|
---|
[57] | 214 | int socket_echo_server (void * arg)
|
---|
[21] | 215 | {
|
---|
| 216 | int srvsock = *((int *)arg);
|
---|
| 217 | int clntsock = 0;
|
---|
| 218 | int recvd = 0;
|
---|
| 219 | char buf[1024] = {0x00,};
|
---|
[38] | 220 | int i = 0;
|
---|
[21] | 221 |
|
---|
| 222 | /*------------------------------------------------------------*/
|
---|
[56] | 223 | clntsock = CF_Socket_Accept (srvsock);
|
---|
[21] | 224 | if (clntsock < 0)
|
---|
| 225 | {
|
---|
| 226 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
[57] | 227 | return -1;
|
---|
[21] | 228 | }
|
---|
[40] | 229 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
[21] | 230 |
|
---|
[38] | 231 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 232 | {
|
---|
[38] | 233 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
| 234 | {
|
---|
| 235 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
[57] | 236 | return -2;
|
---|
[38] | 237 | }
|
---|
[40] | 238 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
[21] | 239 |
|
---|
[38] | 240 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
| 241 | {
|
---|
[57] | 242 | return -3;
|
---|
[38] | 243 | }
|
---|
[40] | 244 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
[21] | 245 | }
|
---|
| 246 |
|
---|
[38] | 247 | CF_Socket_Close (clntsock);
|
---|
| 248 |
|
---|
[57] | 249 | return 0;
|
---|
[21] | 250 | /*------------------------------------------------------------*/
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[57] | 253 | int socket_echo_client (void * arg)
|
---|
[21] | 254 | {
|
---|
| 255 | int sock = 0;
|
---|
| 256 | int recvd = 0;
|
---|
| 257 | char buf[1024] = {0x00,};
|
---|
[38] | 258 | int i = 0;
|
---|
[21] | 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");
|
---|
[57] | 267 | return -1;
|
---|
[21] | 268 | }
|
---|
[40] | 269 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
[21] | 270 |
|
---|
[38] | 271 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 272 | {
|
---|
[38] | 273 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
| 274 | {
|
---|
| 275 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
[57] | 276 | return -2;
|
---|
[38] | 277 | }
|
---|
[40] | 278 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
[21] | 279 |
|
---|
[38] | 280 | memset (buf, 0x00, sizeof (buf));
|
---|
[21] | 281 |
|
---|
[38] | 282 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
| 283 | {
|
---|
| 284 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
[57] | 285 | return -3;
|
---|
[38] | 286 | }
|
---|
[40] | 287 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
[21] | 288 | }
|
---|
| 289 |
|
---|
| 290 | CF_Socket_Close (sock);
|
---|
| 291 |
|
---|
[57] | 292 | return 0;
|
---|
[21] | 293 | /*------------------------------------------------------------*/
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[24] | 296 | void test_socket (void)
|
---|
[21] | 297 | {
|
---|
[62] | 298 | CF_Thread stid[THREAD_POOL];
|
---|
| 299 | CF_Thread ctid[THREAD_POOL];
|
---|
[21] | 300 |
|
---|
| 301 | int sock = 0;
|
---|
| 302 | int iter = 0;
|
---|
| 303 |
|
---|
| 304 | /*------------------------------------------------------------*/
|
---|
[40] | 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)
|
---|
[21] | 312 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
| 313 |
|
---|
[40] | 314 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
[21] | 315 |
|
---|
| 316 | if (CF_Socket_Initialize () < 0)
|
---|
| 317 | {
|
---|
| 318 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
| 319 | return ;
|
---|
| 320 | }
|
---|
[40] | 321 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
[21] | 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 | }
|
---|
[40] | 329 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
[21] | 330 |
|
---|
| 331 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 332 | {
|
---|
[62] | 333 | if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
[21] | 334 | {
|
---|
| 335 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 336 | return ;
|
---|
| 337 | }
|
---|
[40] | 338 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
[21] | 339 | }
|
---|
| 340 |
|
---|
| 341 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 342 | {
|
---|
[62] | 343 | if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
[21] | 344 | {
|
---|
| 345 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 346 | return ;
|
---|
| 347 | }
|
---|
[40] | 348 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
[21] | 349 | }
|
---|
| 350 |
|
---|
| 351 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 352 | {
|
---|
[62] | 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 |
|
---|
[40] | 359 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
[21] | 360 | }
|
---|
| 361 |
|
---|
| 362 | CF_Socket_Close (sock);
|
---|
| 363 |
|
---|
| 364 | CF_Socket_Finalize ();
|
---|
| 365 |
|
---|
[40] | 366 | CF_Log_Close (LOG_SOCKET);
|
---|
| 367 |
|
---|
| 368 | CF_Log_Finalize ();
|
---|
[21] | 369 | /*------------------------------------------------------------*/
|
---|
| 370 | }
|
---|
| 371 |
|
---|