summaryrefslogtreecommitdiff
path: root/pr/tests/server_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'pr/tests/server_test.c')
-rw-r--r--pr/tests/server_test.c337
1 files changed, 206 insertions, 131 deletions
diff --git a/pr/tests/server_test.c b/pr/tests/server_test.c
index 3840c828..93ffe4af 100644
--- a/pr/tests/server_test.c
+++ b/pr/tests/server_test.c
@@ -14,10 +14,10 @@
**
** Modification History:
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
-** The debug mode will print all of the printfs associated with this test.
-** The regress mode will be the default mode. Since the regress tool limits
+** The debug mode will print all of the printfs associated with this test.
+** The regress mode will be the default mode. Since the regress tool limits
** the output to a one line status:PASS or FAIL,all of the printf statements
-** have been handled with an if (debug_mode) statement.
+** have been handled with an if (debug_mode) statement.
***********************************************************************/
/***********************************************************************
@@ -68,7 +68,7 @@ PRCondVar *ServerStateCV;
/***********************************************************************
** PRIVATE FUNCTION: Test_Result
** DESCRIPTION: Used in conjunction with the regress tool, prints out the
-** status of the test case.
+** status of the test case.
** INPUTS: PASS/FAIL
** OUTPUTS: None
** RETURN: None
@@ -84,18 +84,18 @@ PRCondVar *ServerStateCV;
static void Test_Result (int result)
{
- switch (result)
- {
- case PASS:
- printf ("PASS\n");
- break;
- case FAIL:
- printf ("FAIL\n");
- failed_already = 1;
- break;
- default:
- break;
- }
+ switch (result)
+ {
+ case PASS:
+ printf ("PASS\n");
+ break;
+ case FAIL:
+ printf ("FAIL\n");
+ failed_already = 1;
+ break;
+ default:
+ break;
+ }
}
static void do_work(void);
@@ -108,7 +108,9 @@ SetServerState(char *waiter, PRInt32 state)
ServerState = state;
PR_NotifyCondVar(ServerStateCV);
- if (debug_mode) DPRINTF("\t%s changed state to %d\n", waiter, state);
+ if (debug_mode) {
+ DPRINTF("\t%s changed state to %d\n", waiter, state);
+ }
PR_Unlock(ServerStateCVLock);
}
@@ -120,14 +122,17 @@ WaitServerState(char *waiter, PRInt32 state)
PR_Lock(ServerStateCVLock);
- if (debug_mode) DPRINTF("\t%s waiting for state %d\n", waiter, state);
+ if (debug_mode) {
+ DPRINTF("\t%s waiting for state %d\n", waiter, state);
+ }
- while(!(ServerState & state))
+ while(!(ServerState & state)) {
PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT);
+ }
rv = ServerState;
if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
- waiter, state, ServerState);
+ waiter, state, ServerState);
PR_Unlock(ServerStateCVLock);
return rv;
@@ -149,15 +154,21 @@ WorkerThreadFunc(void *_listenSock)
char *sendBuf;
if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
- _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32);
+ _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32);
dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32);
if (!dataBuf)
- if (debug_mode) printf("\tServer could not malloc space!?\n");
+ if (debug_mode) {
+ printf("\tServer could not malloc space!?\n");
+ }
sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char));
if (!sendBuf)
- if (debug_mode) printf("\tServer could not malloc space!?\n");
+ if (debug_mode) {
+ printf("\tServer could not malloc space!?\n");
+ }
- if (debug_mode) DPRINTF("\tServer worker thread running\n");
+ if (debug_mode) {
+ DPRINTF("\tServer worker thread running\n");
+ }
while(1) {
PRInt32 bytesToRead = _client_data;
@@ -168,7 +179,9 @@ WorkerThreadFunc(void *_listenSock)
loops++;
- if (debug_mode) DPRINTF("\tServer thread going into accept\n");
+ if (debug_mode) {
+ DPRINTF("\tServer thread going into accept\n");
+ }
bytesRead = PR_AcceptRead(listenSock,
&newSock,
@@ -178,11 +191,15 @@ WorkerThreadFunc(void *_listenSock)
PR_INTERVAL_NO_TIMEOUT);
if (bytesRead < 0) {
- if (debug_mode) printf("\tServer error in accept (%d)\n", bytesRead);
+ if (debug_mode) {
+ printf("\tServer error in accept (%d)\n", bytesRead);
+ }
continue;
}
- if (debug_mode) DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead);
+ if (debug_mode) {
+ DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead);
+ }
PR_AtomicIncrement(&workerThreadsBusy);
if (workerThreadsBusy == workerThreads) {
@@ -191,19 +208,23 @@ WorkerThreadFunc(void *_listenSock)
PRThread *WorkerThread;
WorkerThread = PR_CreateThread(
- PR_SYSTEM_THREAD,
- WorkerThreadFunc,
- listenSock,
- PR_PRIORITY_NORMAL,
- ServerScope,
- PR_UNJOINABLE_THREAD,
- THREAD_STACKSIZE);
+ PR_SYSTEM_THREAD,
+ WorkerThreadFunc,
+ listenSock,
+ PR_PRIORITY_NORMAL,
+ ServerScope,
+ PR_UNJOINABLE_THREAD,
+ THREAD_STACKSIZE);
if (!WorkerThread) {
- if (debug_mode) printf("Error creating client thread %d\n", workerThreads);
+ if (debug_mode) {
+ printf("Error creating client thread %d\n", workerThreads);
+ }
} else {
PR_AtomicIncrement(&workerThreads);
- if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads);
+ if (debug_mode) {
+ DPRINTF("\tServer creates worker (%d)\n", workerThreads);
+ }
}
}
PR_Unlock(workerThreadsLock);
@@ -217,10 +238,14 @@ WorkerThreadFunc(void *_listenSock)
0,
PR_INTERVAL_NO_TIMEOUT);
if (bytesRead < 0) {
- if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead);
+ if (debug_mode) {
+ printf("\tServer error receiving data (%d)\n", bytesRead);
+ }
continue;
}
- if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead);
+ if (debug_mode) {
+ DPRINTF("\tServer received %d bytes\n", bytesRead);
+ }
}
bytesWritten = PR_Send(newSock,
@@ -230,9 +255,11 @@ WorkerThreadFunc(void *_listenSock)
PR_INTERVAL_NO_TIMEOUT);
if (bytesWritten != _server_data) {
if (debug_mode) printf("\tError sending data to client (%d, %d)\n",
- bytesWritten, PR_GetOSError());
+ bytesWritten, PR_GetOSError());
} else {
- if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten);
+ if (debug_mode) {
+ DPRINTF("\tServer sent %d bytes\n", bytesWritten);
+ }
}
PR_Close(newSock);
@@ -249,8 +276,12 @@ ServerSetup(void)
PRThread *WorkerThread;
if ((listenSocket = PR_NewTCPSocket()) == NULL) {
- if (debug_mode) printf("\tServer error creating listen socket\n");
- else Test_Result(FAIL);
+ if (debug_mode) {
+ printf("\tServer error creating listen socket\n");
+ }
+ else {
+ Test_Result(FAIL);
+ }
return NULL;
}
@@ -258,8 +289,10 @@ ServerSetup(void)
sockOpt.value.reuse_addr = PR_TRUE;
if (PR_SetSocketOption(listenSocket, &sockOpt) != PR_SUCCESS) {
if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
- PR_GetOSError());
- else Test_Result(FAIL);
+ PR_GetOSError());
+ else {
+ Test_Result(FAIL);
+ }
PR_Close(listenSocket);
return NULL;
}
@@ -271,15 +304,21 @@ ServerSetup(void)
if (PR_Bind(listenSocket, &serverAddr) != PR_SUCCESS) {
if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
- PR_GetOSError());
- else Test_Result(FAIL);
+ PR_GetOSError());
+ else {
+ Test_Result(FAIL);
+ }
PR_Close(listenSocket);
return NULL;
}
if (PR_Listen(listenSocket, 128) != PR_SUCCESS) {
- if (debug_mode) printf("\tServer error listening to server socket\n");
- else Test_Result(FAIL);
+ if (debug_mode) {
+ printf("\tServer error listening to server socket\n");
+ }
+ else {
+ Test_Result(FAIL);
+ }
PR_Close(listenSocket);
return NULL;
@@ -292,21 +331,25 @@ ServerSetup(void)
workerThreadsLock = PR_NewLock();
WorkerThread = PR_CreateThread(
- PR_SYSTEM_THREAD,
- WorkerThreadFunc,
- listenSocket,
- PR_PRIORITY_NORMAL,
- ServerScope,
- PR_UNJOINABLE_THREAD,
- THREAD_STACKSIZE);
+ PR_SYSTEM_THREAD,
+ WorkerThreadFunc,
+ listenSocket,
+ PR_PRIORITY_NORMAL,
+ ServerScope,
+ PR_UNJOINABLE_THREAD,
+ THREAD_STACKSIZE);
if (!WorkerThread) {
- if (debug_mode) printf("error creating working thread\n");
+ if (debug_mode) {
+ printf("error creating working thread\n");
+ }
PR_Close(listenSocket);
return NULL;
}
PR_AtomicIncrement(&workerThreads);
- if (debug_mode) DPRINTF("\tServer created primordial worker thread\n");
+ if (debug_mode) {
+ DPRINTF("\tServer created primordial worker thread\n");
+ }
return listenSocket;
}
@@ -324,7 +367,9 @@ ServerThreadFunc(void *unused)
SetServerState(SERVER, SERVER_STATE_DEAD);
} else {
- if (debug_mode) DPRINTF("\tServer up\n");
+ if (debug_mode) {
+ DPRINTF("\tServer up\n");
+ }
/* Tell clients they can start now. */
SetServerState(SERVER, SERVER_STATE_READY);
@@ -355,10 +400,14 @@ ClientThreadFunc(void *unused)
sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
if (!sendBuf)
- if (debug_mode) printf("\tClient could not malloc space!?\n");
+ if (debug_mode) {
+ printf("\tClient could not malloc space!?\n");
+ }
recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
if (!recvBuf)
- if (debug_mode) printf("\tClient could not malloc space!?\n");
+ if (debug_mode) {
+ printf("\tClient could not malloc space!?\n");
+ }
memset(&serverAddr, 0, sizeof(PRNetAddr));
serverAddr.inet.family = PR_AF_INET;
@@ -368,27 +417,37 @@ ClientThreadFunc(void *unused)
while(numRequests > 0) {
if ( (numRequests % 10) == 0 )
- if (debug_mode) printf(".");
- if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests);
+ if (debug_mode) {
+ printf(".");
+ }
+ if (debug_mode) {
+ DPRINTF("\tClient starting request %d\n", numRequests);
+ }
clientSocket = PR_NewTCPSocket();
if (!clientSocket) {
if (debug_mode) printf("Client error creating socket: OS error %d\n",
- PR_GetOSError());
+ PR_GetOSError());
continue;
}
- if (debug_mode) DPRINTF("\tClient connecting\n");
+ if (debug_mode) {
+ DPRINTF("\tClient connecting\n");
+ }
rv = PR_Connect(clientSocket,
&serverAddr,
PR_INTERVAL_NO_TIMEOUT);
if (!clientSocket) {
- if (debug_mode) printf("\tClient error connecting\n");
+ if (debug_mode) {
+ printf("\tClient error connecting\n");
+ }
continue;
}
- if (debug_mode) DPRINTF("\tClient connected\n");
+ if (debug_mode) {
+ DPRINTF("\tClient connected\n");
+ }
rv = PR_Send(clientSocket,
sendBuf,
@@ -396,12 +455,16 @@ ClientThreadFunc(void *unused)
0,
PR_INTERVAL_NO_TIMEOUT);
if (rv != _client_data) {
- if (debug_mode) printf("Client error sending data (%d)\n", rv);
+ if (debug_mode) {
+ printf("Client error sending data (%d)\n", rv);
+ }
PR_Close(clientSocket);
continue;
}
- if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv);
+ if (debug_mode) {
+ DPRINTF("\tClient sent %d bytes\n", rv);
+ }
bytesNeeded = _server_data;
while(bytesNeeded) {
@@ -412,10 +475,12 @@ ClientThreadFunc(void *unused)
PR_INTERVAL_NO_TIMEOUT);
if (rv <= 0) {
if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
- rv, (_server_data - bytesNeeded), _server_data);
+ rv, (_server_data - bytesNeeded), _server_data);
break;
}
- if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
+ if (debug_mode) {
+ DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
+ }
bytesNeeded -= rv;
}
@@ -447,24 +512,28 @@ RunClients(void)
clientThread = PR_CreateThread(
- PR_USER_THREAD,
- ClientThreadFunc,
- NULL,
- PR_PRIORITY_NORMAL,
- ClientScope,
- PR_UNJOINABLE_THREAD,
- THREAD_STACKSIZE);
+ PR_USER_THREAD,
+ ClientThreadFunc,
+ NULL,
+ PR_PRIORITY_NORMAL,
+ ClientScope,
+ PR_UNJOINABLE_THREAD,
+ THREAD_STACKSIZE);
if (!clientThread) {
- if (debug_mode) printf("\terror creating client thread %d\n", index);
- } else
- if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
+ if (debug_mode) {
+ printf("\terror creating client thread %d\n", index);
+ }
+ } else if (debug_mode) {
+ DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
+ }
}
PR_EnterMonitor(clientMonitor);
- while(numClients)
+ while(numClients) {
PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
+ }
PR_ExitMonitor(clientMonitor);
}
@@ -478,15 +547,17 @@ void do_work()
SetServerState(MAIN, SERVER_STATE_STARTUP);
ServerThread = PR_CreateThread(
- PR_USER_THREAD,
- ServerThreadFunc,
- NULL,
- PR_PRIORITY_NORMAL,
- ServerScope,
- PR_JOINABLE_THREAD,
- THREAD_STACKSIZE);
+ PR_USER_THREAD,
+ ServerThreadFunc,
+ NULL,
+ PR_PRIORITY_NORMAL,
+ ServerScope,
+ PR_JOINABLE_THREAD,
+ THREAD_STACKSIZE);
if (!ServerThread) {
- if (debug_mode) printf("error creating main server thread\n");
+ if (debug_mode) {
+ printf("error creating main server thread\n");
+ }
return;
}
@@ -544,60 +615,64 @@ static void Measure(void (*func)(void), const char *msg)
d = (double)PR_IntervalToMicroseconds(stop - start);
- if (debug_mode) printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
+ if (debug_mode) {
+ printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
+ }
}
int main(int argc, char **argv)
{
- /* The command line argument: -d is used to determine if the test is being run
- in debug mode. The regress tool requires only one line output:PASS or FAIL.
- All of the printfs associated with this test has been handled with a if (debug_mode)
- test.
- Usage: test_name -d
- */
- PLOptStatus os;
- PLOptState *opt = PL_CreateOptState(argc, argv, "d");
- while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
+ /* The command line argument: -d is used to determine if the test is being run
+ in debug mode. The regress tool requires only one line output:PASS or FAIL.
+ All of the printfs associated with this test has been handled with a if (debug_mode)
+ test.
+ Usage: test_name -d
+ */
+ PLOptStatus os;
+ PLOptState *opt = PL_CreateOptState(argc, argv, "d");
+ while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
{
- if (PL_OPT_BAD == os) continue;
+ if (PL_OPT_BAD == os) {
+ continue;
+ }
switch (opt->option)
{
- case 'd': /* debug mode */
- debug_mode = 1;
- break;
- default:
- break;
+ case 'd': /* debug mode */
+ debug_mode = 1;
+ break;
+ default:
+ break;
}
}
- PL_DestroyOptState(opt);
+ PL_DestroyOptState(opt);
- /* main test */
+ /* main test */
if (debug_mode) {
- printf("Enter number of iterations: \n");
- scanf("%d", &_iterations);
- printf("Enter number of clients : \n");
- scanf("%d", &_clients);
- printf("Enter size of client data : \n");
- scanf("%d", &_client_data);
- printf("Enter size of server data : \n");
- scanf("%d", &_server_data);
- }
- else
- {
-
- _iterations = 10;
- _clients = 1;
- _client_data = 10;
- _server_data = 10;
- }
+ printf("Enter number of iterations: \n");
+ scanf("%d", &_iterations);
+ printf("Enter number of clients : \n");
+ scanf("%d", &_clients);
+ printf("Enter size of client data : \n");
+ scanf("%d", &_client_data);
+ printf("Enter size of server data : \n");
+ scanf("%d", &_server_data);
+ }
+ else
+ {
+
+ _iterations = 10;
+ _clients = 1;
+ _client_data = 10;
+ _server_data = 10;
+ }
if (debug_mode) {
- printf("\n\n%d iterations with %d client threads.\n",
- _iterations, _clients);
- printf("Sending %d bytes of client data and %d bytes of server data\n",
- _client_data, _server_data);
- }
+ printf("\n\n%d iterations with %d client threads.\n",
+ _iterations, _clients);
+ printf("Sending %d bytes of client data and %d bytes of server data\n",
+ _client_data, _server_data);
+ }
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
PR_STDIO_INIT();
@@ -605,11 +680,11 @@ int main(int argc, char **argv)
ServerStateCV = PR_NewCondVar(ServerStateCVLock);
Measure(do_workUU, "server loop user/user");
- #if 0
+#if 0
Measure(do_workUK, "server loop user/kernel");
Measure(do_workKU, "server loop kernel/user");
Measure(do_workKK, "server loop kernel/kernel");
- #endif
+#endif
PR_Cleanup();