Debian Codegraph @

nsprpub/pr/tests/fdcach.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * File: fdcach.c
8 * Description:
9 * This test verifies that the fd cache is working
10 * correctly.
11 */
12
13 #include "nspr.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 /*
19 * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
20 * preserves the ordering of the fd's when moving them between the
21 * cache.
22 */
23 #define ORDER_PRESERVED 1
24
25 /*
26 * NUM_FDS must be <= FD_CACHE_SIZE.
27 */
28 #define FD_CACHE_SIZE 1024
29 #define NUM_FDS 20
30
31 int main(int argc, char** argv) {
32 int i;
33 PRFileDesc* fds[NUM_FDS];
34 PRFileDesc* savefds[NUM_FDS];
35 int numfds = sizeof(fds) / sizeof(fds[0]);
36
37 PR_SetFDCacheSize(0, FD_CACHE_SIZE);
38
39 /* Add some fd's to the fd cache. */
40 for (i = 0; i < numfds; i++) {
41 savefds[i] = PR_NewTCPSocket();
42 if (NULL == savefds[i]) {
43 fprintf(stderr, "PR_NewTCPSocket failed\n");
44 exit(1);
45 }
46 }
47 for (i = 0; i < numfds; i++) {
48 if (PR_Close(savefds[i]) == PR_FAILURE) {
49 fprintf(stderr, "PR_Close failed\n");
50 exit(1);
51 }
52 }
53
54 /*
55 * Create some fd's. These fd's should come from
56 * the fd cache. Verify the FIFO ordering of the fd
57 * cache.
58 */
59 for (i = 0; i < numfds; i++) {
60 fds[i] = PR_NewTCPSocket();
61 if (NULL == fds[i]) {
62 fprintf(stderr, "PR_NewTCPSocket failed\n");
63 exit(1);
64 }
65 if (fds[i] != savefds[i]) {
66 fprintf(stderr, "fd cache malfunctioned\n");
67 exit(1);
68 }
69 }
70 /* Put the fd's back to the fd cache. */
71 for (i = 0; i < numfds; i++) {
72 if (PR_Close(savefds[i]) == PR_FAILURE) {
73 fprintf(stderr, "PR_Close failed\n");
74 exit(1);
75 }
76 }
77
78 /* Switch to the fd cache. */
79 PR_SetFDCacheSize(0, FD_CACHE_SIZE);
80
81 for (i = 0; i < numfds; i++) {
82 fds[i] = PR_NewTCPSocket();
83 if (NULL == fds[i]) {
84 fprintf(stderr, "PR_NewTCPSocket failed\n");
85 exit(1);
86 }
87 #ifdef ORDER_PRESERVED
88 if (fds[i] != savefds[i]) {
89 fprintf(stderr, "fd cache malfunctioned\n");
90 exit(1);
91 }
92 #else
93 savefds[i] = fds[i];
94 #endif
95 }
96 for (i = 0; i < numfds; i++) {
97 if (PR_Close(savefds[i]) == PR_FAILURE) {
98 fprintf(stderr, "PR_Close failed\n");
99 exit(1);
100 }
101 }
102
103 for (i = 0; i < numfds; i++) {
104 fds[i] = PR_NewTCPSocket();
105 if (NULL == fds[i]) {
106 fprintf(stderr, "PR_NewTCPSocket failed\n");
107 exit(1);
108 }
109 if (fds[i] != savefds[i]) {
110 fprintf(stderr, "fd cache malfunctioned\n");
111 exit(1);
112 }
113 }
114 for (i = 0; i < numfds; i++) {
115 if (PR_Close(savefds[i]) == PR_FAILURE) {
116 fprintf(stderr, "PR_Close failed\n");
117 exit(1);
118 }
119 }
120
121 PR_Cleanup();
122 printf("PASS\n");
123 return 0;
124 }