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