nsprpub/pr/tests/multiacc.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/multiacc.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,220 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * File: multiacc.c
    1.11 + *
    1.12 + * Description:
    1.13 + * This test creates multiple threads that accept on the
    1.14 + * same listening socket.
    1.15 + */
    1.16 +
    1.17 +#include "nspr.h"
    1.18 +
    1.19 +#include <stdio.h>
    1.20 +#include <stdlib.h>
    1.21 +#include <string.h>
    1.22 +
    1.23 +#define NUM_SERVER_THREADS 10
    1.24 +
    1.25 +static int num_server_threads = NUM_SERVER_THREADS;
    1.26 +static PRThreadScope thread_scope = PR_GLOBAL_THREAD;
    1.27 +static PRBool exit_flag = PR_FALSE;
    1.28 +
    1.29 +static void ServerThreadFunc(void *arg)
    1.30 +{
    1.31 +    PRFileDesc *listenSock = (PRFileDesc *) arg;
    1.32 +    PRFileDesc *acceptSock;
    1.33 +    PRErrorCode err;
    1.34 +    PRStatus status;
    1.35 +
    1.36 +    while (!exit_flag) {
    1.37 +        acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
    1.38 +        if (NULL == acceptSock) {
    1.39 +            err = PR_GetError();
    1.40 +            if (PR_PENDING_INTERRUPT_ERROR == err) {
    1.41 +                printf("server thread is interrupted\n");
    1.42 +                fflush(stdout);
    1.43 +                continue;
    1.44 +            }
    1.45 +            fprintf(stderr, "PR_Accept failed: %d\n", err);
    1.46 +            exit(1);
    1.47 +        }
    1.48 +        status = PR_Close(acceptSock);
    1.49 +        if (PR_FAILURE == status) {
    1.50 +            fprintf(stderr, "PR_Close failed\n");
    1.51 +            exit(1);
    1.52 +        }
    1.53 +    }
    1.54 +}
    1.55 +
    1.56 +int main(int argc, char **argv)
    1.57 +{
    1.58 +    PRNetAddr serverAddr;
    1.59 +    PRFileDesc *dummySock;
    1.60 +    PRFileDesc *listenSock;
    1.61 +    PRFileDesc *clientSock;
    1.62 +    PRThread *dummyThread;
    1.63 +    PRThread **serverThreads;
    1.64 +    PRStatus status;
    1.65 +    PRUint16 port;
    1.66 +    int idx;
    1.67 +    PRInt32 nbytes;
    1.68 +    char buf[1024];
    1.69 +
    1.70 +    serverThreads = (PRThread **)
    1.71 +            PR_Malloc(num_server_threads * sizeof(PRThread *));
    1.72 +    if (NULL == serverThreads) {
    1.73 +        fprintf(stderr, "PR_Malloc failed\n");
    1.74 +        exit(1);
    1.75 +    }
    1.76 +
    1.77 +    /*
    1.78 +     * Create a dummy listening socket and have the first
    1.79 +     * (dummy) thread listen on it.  This is to ensure that
    1.80 +     * the first thread becomes the I/O continuation thread
    1.81 +     * in the pthreads implementation (see ptio.c) and remains
    1.82 +     * so throughout the test, so that we never have to
    1.83 +     * recycle the I/O continuation thread.
    1.84 +     */
    1.85 +    dummySock = PR_NewTCPSocket();
    1.86 +    if (NULL == dummySock) {
    1.87 +        fprintf(stderr, "PR_NewTCPSocket failed\n");
    1.88 +        exit(1);
    1.89 +    }
    1.90 +    memset(&serverAddr, 0, sizeof(serverAddr));
    1.91 +    status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr);
    1.92 +    if (PR_FAILURE == status) {
    1.93 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
    1.94 +        exit(1);
    1.95 +    }
    1.96 +    status = PR_Bind(dummySock, &serverAddr);
    1.97 +    if (PR_FAILURE == status) {
    1.98 +        fprintf(stderr, "PR_Bind failed\n");
    1.99 +        exit(1);
   1.100 +    }
   1.101 +    status = PR_Listen(dummySock, 5);
   1.102 +    if (PR_FAILURE == status) {
   1.103 +        fprintf(stderr, "PR_Listen failed\n");
   1.104 +        exit(1);
   1.105 +    }
   1.106 +
   1.107 +    listenSock = PR_NewTCPSocket();
   1.108 +    if (NULL == listenSock) {
   1.109 +        fprintf(stderr, "PR_NewTCPSocket failed\n");
   1.110 +        exit(1);
   1.111 +    }
   1.112 +    memset(&serverAddr, 0, sizeof(serverAddr));
   1.113 +    status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr);
   1.114 +    if (PR_FAILURE == status) {
   1.115 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
   1.116 +        exit(1);
   1.117 +    }
   1.118 +    status = PR_Bind(listenSock, &serverAddr);
   1.119 +    if (PR_FAILURE == status) {
   1.120 +        fprintf(stderr, "PR_Bind failed\n");
   1.121 +        exit(1);
   1.122 +    }
   1.123 +    status = PR_GetSockName(listenSock, &serverAddr);
   1.124 +    if (PR_FAILURE == status) {
   1.125 +        fprintf(stderr, "PR_GetSockName failed\n");
   1.126 +        exit(1);
   1.127 +    }
   1.128 +    port = PR_ntohs(serverAddr.inet.port);
   1.129 +    status = PR_Listen(listenSock, 5);
   1.130 +    if (PR_FAILURE == status) {
   1.131 +        fprintf(stderr, "PR_Listen failed\n");
   1.132 +        exit(1);
   1.133 +    }
   1.134 +
   1.135 +    printf("creating dummy thread\n");
   1.136 +    fflush(stdout);
   1.137 +    dummyThread = PR_CreateThread(PR_USER_THREAD,
   1.138 +            ServerThreadFunc, dummySock, PR_PRIORITY_NORMAL,
   1.139 +            thread_scope, PR_JOINABLE_THREAD, 0);
   1.140 +    if (NULL == dummyThread) {
   1.141 +        fprintf(stderr, "PR_CreateThread failed\n");
   1.142 +        exit(1);
   1.143 +    }
   1.144 +    printf("sleeping one second before creating server threads\n");
   1.145 +    fflush(stdout);
   1.146 +    PR_Sleep(PR_SecondsToInterval(1));
   1.147 +    for (idx = 0; idx < num_server_threads; idx++) {
   1.148 +        serverThreads[idx] = PR_CreateThread(PR_USER_THREAD,
   1.149 +                ServerThreadFunc, listenSock, PR_PRIORITY_NORMAL,
   1.150 +                thread_scope, PR_JOINABLE_THREAD, 0);
   1.151 +        if (NULL == serverThreads[idx]) {
   1.152 +            fprintf(stderr, "PR_CreateThread failed\n");
   1.153 +            exit(1);
   1.154 +        }
   1.155 +    }
   1.156 +
   1.157 +    memset(&serverAddr, 0, sizeof(serverAddr));
   1.158 +    PR_InitializeNetAddr(PR_IpAddrLoopback, port, &serverAddr);
   1.159 +    clientSock = PR_NewTCPSocket();
   1.160 +    if (NULL == clientSock) {
   1.161 +        fprintf(stderr, "PR_NewTCPSocket failed\n");
   1.162 +        exit(1);
   1.163 +    }
   1.164 +    printf("sleeping one second before connecting\n");
   1.165 +    fflush(stdout);
   1.166 +    PR_Sleep(PR_SecondsToInterval(1));
   1.167 +    status = PR_Connect(clientSock, &serverAddr, PR_INTERVAL_NO_TIMEOUT);
   1.168 +    if (PR_FAILURE == status) {
   1.169 +        fprintf(stderr, "PR_Connect failed\n");
   1.170 +        exit(1);
   1.171 +    }
   1.172 +    nbytes = PR_Read(clientSock, buf, sizeof(buf));
   1.173 +    if (nbytes != 0) {
   1.174 +        fprintf(stderr, "expected 0 bytes but got %d bytes\n", nbytes);
   1.175 +        exit(1);
   1.176 +    }
   1.177 +    status = PR_Close(clientSock);
   1.178 +    if (PR_FAILURE == status) {
   1.179 +        fprintf(stderr, "PR_Close failed\n");
   1.180 +        exit(1);
   1.181 +    }
   1.182 +    printf("sleeping one second before shutting down server threads\n");
   1.183 +    fflush(stdout);
   1.184 +    PR_Sleep(PR_SecondsToInterval(1));
   1.185 +
   1.186 +    exit_flag = PR_TRUE;
   1.187 +    status = PR_Interrupt(dummyThread);
   1.188 +    if (PR_FAILURE == status) {
   1.189 +        fprintf(stderr, "PR_Interrupt failed\n");
   1.190 +        exit(1);
   1.191 +    }
   1.192 +    status = PR_JoinThread(dummyThread);
   1.193 +    if (PR_FAILURE == status) {
   1.194 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.195 +        exit(1);
   1.196 +    }
   1.197 +    for (idx = 0; idx < num_server_threads; idx++) {
   1.198 +        status = PR_Interrupt(serverThreads[idx]);
   1.199 +        if (PR_FAILURE == status) {
   1.200 +            fprintf(stderr, "PR_Interrupt failed\n");
   1.201 +            exit(1);
   1.202 +        }
   1.203 +        status = PR_JoinThread(serverThreads[idx]);
   1.204 +        if (PR_FAILURE == status) {
   1.205 +            fprintf(stderr, "PR_JoinThread failed\n");
   1.206 +            exit(1);
   1.207 +        }
   1.208 +    }
   1.209 +    PR_Free(serverThreads);
   1.210 +    status = PR_Close(dummySock);
   1.211 +    if (PR_FAILURE == status) {
   1.212 +        fprintf(stderr, "PR_Close failed\n");
   1.213 +        exit(1);
   1.214 +    }
   1.215 +    status = PR_Close(listenSock);
   1.216 +    if (PR_FAILURE == status) {
   1.217 +        fprintf(stderr, "PR_Close failed\n");
   1.218 +        exit(1);
   1.219 +    }
   1.220 +
   1.221 +    printf("PASS\n");
   1.222 +    return 0;
   1.223 +}

mercurial