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/. */
9 * This test verifies that the fd cache is working
19 * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
20 * preserves the ordering of the fd's when moving them between the
23 #define ORDER_PRESERVED 1
26 * NUM_FDS must be <= FD_CACHE_SIZE.
28 #define FD_CACHE_SIZE 1024
31 int main(int argc
, char **argv
)
34 PRFileDesc
*fds
[NUM_FDS
];
35 PRFileDesc
*savefds
[NUM_FDS
];
36 int numfds
= sizeof(fds
)/sizeof(fds
[0]);
38 PR_SetFDCacheSize(0, FD_CACHE_SIZE
);
40 /* Add some fd's to the fd cache. */
41 for (i
= 0; i
< numfds
; i
++) {
42 savefds
[i
] = PR_NewTCPSocket();
43 if (NULL
== savefds
[i
]) {
44 fprintf(stderr
, "PR_NewTCPSocket failed\n");
48 for (i
= 0; i
< numfds
; i
++) {
49 if (PR_Close(savefds
[i
]) == PR_FAILURE
) {
50 fprintf(stderr
, "PR_Close failed\n");
56 * Create some fd's. These fd's should come from
57 * the fd cache. Verify the FIFO ordering of the fd
60 for (i
= 0; i
< numfds
; i
++) {
61 fds
[i
] = PR_NewTCPSocket();
63 fprintf(stderr
, "PR_NewTCPSocket failed\n");
66 if (fds
[i
] != savefds
[i
]) {
67 fprintf(stderr
, "fd cache malfunctioned\n");
71 /* Put the fd's back to the fd cache. */
72 for (i
= 0; i
< numfds
; i
++) {
73 if (PR_Close(savefds
[i
]) == PR_FAILURE
) {
74 fprintf(stderr
, "PR_Close failed\n");
79 /* Switch to the fd cache. */
80 PR_SetFDCacheSize(0, FD_CACHE_SIZE
);
82 for (i
= 0; i
< numfds
; i
++) {
83 fds
[i
] = PR_NewTCPSocket();
85 fprintf(stderr
, "PR_NewTCPSocket failed\n");
88 #ifdef ORDER_PRESERVED
89 if (fds
[i
] != savefds
[i
]) {
90 fprintf(stderr
, "fd cache malfunctioned\n");
97 for (i
= 0; i
< numfds
; i
++) {
98 if (PR_Close(savefds
[i
]) == PR_FAILURE
) {
99 fprintf(stderr
, "PR_Close failed\n");
104 for (i
= 0; i
< numfds
; i
++) {
105 fds
[i
] = PR_NewTCPSocket();
106 if (NULL
== fds
[i
]) {
107 fprintf(stderr
, "PR_NewTCPSocket failed\n");
110 if (fds
[i
] != savefds
[i
]) {
111 fprintf(stderr
, "fd cache malfunctioned\n");
115 for (i
= 0; i
< numfds
; i
++) {
116 if (PR_Close(savefds
[i
]) == PR_FAILURE
) {
117 fprintf(stderr
, "PR_Close failed\n");