1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/fdcach.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,227 @@ 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: fdcach.c 1.11 + * Description: 1.12 + * This test verifies that the fd cache and stack are working 1.13 + * correctly. 1.14 + */ 1.15 + 1.16 +#include "nspr.h" 1.17 + 1.18 +#include <stdio.h> 1.19 +#include <stdlib.h> 1.20 + 1.21 +/* 1.22 + * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize 1.23 + * preserves the ordering of the fd's when moving them between the 1.24 + * cache and the stack. 1.25 + */ 1.26 +#define ORDER_PRESERVED 1 1.27 + 1.28 +/* 1.29 + * NUM_FDS must be <= FD_CACHE_SIZE. 1.30 + */ 1.31 +#define FD_CACHE_SIZE 1024 1.32 +#define NUM_FDS 20 1.33 + 1.34 +int main(int argc, char **argv) 1.35 +{ 1.36 + int i; 1.37 + PRFileDesc *fds[NUM_FDS]; 1.38 + PRFileDesc *savefds[NUM_FDS]; 1.39 + int numfds = sizeof(fds)/sizeof(fds[0]); 1.40 + 1.41 + /* 1.42 + * Switch between cache and stack when they are empty. 1.43 + * Then start with the fd cache. 1.44 + */ 1.45 + PR_SetFDCacheSize(0, FD_CACHE_SIZE); 1.46 + PR_SetFDCacheSize(0, 0); 1.47 + PR_SetFDCacheSize(0, FD_CACHE_SIZE); 1.48 + 1.49 + /* Add some fd's to the fd cache. */ 1.50 + for (i = 0; i < numfds; i++) { 1.51 + savefds[i] = PR_NewTCPSocket(); 1.52 + if (NULL == savefds[i]) { 1.53 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.54 + exit(1); 1.55 + } 1.56 + } 1.57 + for (i = 0; i < numfds; i++) { 1.58 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.59 + fprintf(stderr, "PR_Close failed\n"); 1.60 + exit(1); 1.61 + } 1.62 + } 1.63 + 1.64 + /* 1.65 + * Create some fd's. These fd's should come from 1.66 + * the fd cache. Verify the FIFO ordering of the fd 1.67 + * cache. 1.68 + */ 1.69 + for (i = 0; i < numfds; i++) { 1.70 + fds[i] = PR_NewTCPSocket(); 1.71 + if (NULL == fds[i]) { 1.72 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.73 + exit(1); 1.74 + } 1.75 + if (fds[i] != savefds[i]) { 1.76 + fprintf(stderr, "fd cache malfunctioned\n"); 1.77 + exit(1); 1.78 + } 1.79 + } 1.80 + /* Put the fd's back to the fd cache. */ 1.81 + for (i = 0; i < numfds; i++) { 1.82 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.83 + fprintf(stderr, "PR_Close failed\n"); 1.84 + exit(1); 1.85 + } 1.86 + } 1.87 + 1.88 + /* Switch to the fd stack. */ 1.89 + PR_SetFDCacheSize(0, 0); 1.90 + 1.91 + /* 1.92 + * Create some fd's. These fd's should come from 1.93 + * the fd stack. 1.94 + */ 1.95 + for (i = 0; i < numfds; i++) { 1.96 + fds[i] = PR_NewTCPSocket(); 1.97 + if (NULL == fds[i]) { 1.98 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.99 + exit(1); 1.100 + } 1.101 +#ifdef ORDER_PRESERVED 1.102 + if (fds[i] != savefds[numfds-1-i]) { 1.103 + fprintf(stderr, "fd stack malfunctioned\n"); 1.104 + exit(1); 1.105 + } 1.106 +#else 1.107 + savefds[numfds-1-i] = fds[i]; 1.108 +#endif 1.109 + } 1.110 + /* Put the fd's back to the fd stack. */ 1.111 + for (i = 0; i < numfds; i++) { 1.112 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.113 + fprintf(stderr, "PR_Close failed\n"); 1.114 + exit(1); 1.115 + } 1.116 + } 1.117 + 1.118 + /* 1.119 + * Now create some fd's and verify the LIFO ordering of 1.120 + * the fd stack. 1.121 + */ 1.122 + for (i = 0; i < numfds; i++) { 1.123 + fds[i] = PR_NewTCPSocket(); 1.124 + if (NULL == fds[i]) { 1.125 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.126 + exit(1); 1.127 + } 1.128 + if (fds[i] != savefds[numfds-1-i]) { 1.129 + fprintf(stderr, "fd stack malfunctioned\n"); 1.130 + exit(1); 1.131 + } 1.132 + } 1.133 + /* Put the fd's back to the fd stack. */ 1.134 + for (i = 0; i < numfds; i++) { 1.135 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.136 + fprintf(stderr, "PR_Close failed\n"); 1.137 + exit(1); 1.138 + } 1.139 + } 1.140 + 1.141 + /* Switch to the fd cache. */ 1.142 + PR_SetFDCacheSize(0, FD_CACHE_SIZE); 1.143 + 1.144 + for (i = 0; i < numfds; i++) { 1.145 + fds[i] = PR_NewTCPSocket(); 1.146 + if (NULL == fds[i]) { 1.147 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.148 + exit(1); 1.149 + } 1.150 +#ifdef ORDER_PRESERVED 1.151 + if (fds[i] != savefds[i]) { 1.152 + fprintf(stderr, "fd cache malfunctioned\n"); 1.153 + exit(1); 1.154 + } 1.155 +#else 1.156 + savefds[i] = fds[i]; 1.157 +#endif 1.158 + } 1.159 + for (i = 0; i < numfds; i++) { 1.160 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.161 + fprintf(stderr, "PR_Close failed\n"); 1.162 + exit(1); 1.163 + } 1.164 + } 1.165 + 1.166 + for (i = 0; i < numfds; i++) { 1.167 + fds[i] = PR_NewTCPSocket(); 1.168 + if (NULL == fds[i]) { 1.169 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.170 + exit(1); 1.171 + } 1.172 + if (fds[i] != savefds[i]) { 1.173 + fprintf(stderr, "fd cache malfunctioned\n"); 1.174 + exit(1); 1.175 + } 1.176 + } 1.177 + for (i = 0; i < numfds; i++) { 1.178 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.179 + fprintf(stderr, "PR_Close failed\n"); 1.180 + exit(1); 1.181 + } 1.182 + } 1.183 + 1.184 + /* Switch to the fd stack. */ 1.185 + PR_SetFDCacheSize(0, 0); 1.186 + 1.187 + for (i = 0; i < numfds; i++) { 1.188 + fds[i] = PR_NewTCPSocket(); 1.189 + if (NULL == fds[i]) { 1.190 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.191 + exit(1); 1.192 + } 1.193 +#ifdef ORDER_PRESERVED 1.194 + if (fds[i] != savefds[numfds-1-i]) { 1.195 + fprintf(stderr, "fd stack malfunctioned\n"); 1.196 + exit(1); 1.197 + } 1.198 +#else 1.199 + savefds[numfds-1-i]; 1.200 +#endif 1.201 + } 1.202 + for (i = 0; i < numfds; i++) { 1.203 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.204 + fprintf(stderr, "PR_Close failed\n"); 1.205 + exit(1); 1.206 + } 1.207 + } 1.208 + 1.209 + for (i = 0; i < numfds; i++) { 1.210 + fds[i] = PR_NewTCPSocket(); 1.211 + if (NULL == fds[i]) { 1.212 + fprintf(stderr, "PR_NewTCPSocket failed\n"); 1.213 + exit(1); 1.214 + } 1.215 + if (fds[i] != savefds[numfds-1-i]) { 1.216 + fprintf(stderr, "fd stack malfunctioned\n"); 1.217 + exit(1); 1.218 + } 1.219 + } 1.220 + for (i = 0; i < numfds; i++) { 1.221 + if (PR_Close(savefds[i]) == PR_FAILURE) { 1.222 + fprintf(stderr, "PR_Close failed\n"); 1.223 + exit(1); 1.224 + } 1.225 + } 1.226 + 1.227 + PR_Cleanup(); 1.228 + printf("PASS\n"); 1.229 + return 0; 1.230 +}