fdcach.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * File: fdcach.c
  7. * Description:
  8. * This test verifies that the fd cache is working
  9. * correctly.
  10. */
  11. #include "nspr.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. /*
  15. * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
  16. * preserves the ordering of the fd's when moving them between the
  17. * cache.
  18. */
  19. #define ORDER_PRESERVED 1
  20. /*
  21. * NUM_FDS must be <= FD_CACHE_SIZE.
  22. */
  23. #define FD_CACHE_SIZE 1024
  24. #define NUM_FDS 20
  25. int main(int argc, char **argv)
  26. {
  27. int i;
  28. PRFileDesc *fds[NUM_FDS];
  29. PRFileDesc *savefds[NUM_FDS];
  30. int numfds = sizeof(fds)/sizeof(fds[0]);
  31. PR_SetFDCacheSize(0, FD_CACHE_SIZE);
  32. /* Add some fd's to the fd cache. */
  33. for (i = 0; i < numfds; i++) {
  34. savefds[i] = PR_NewTCPSocket();
  35. if (NULL == savefds[i]) {
  36. fprintf(stderr, "PR_NewTCPSocket failed\n");
  37. exit(1);
  38. }
  39. }
  40. for (i = 0; i < numfds; i++) {
  41. if (PR_Close(savefds[i]) == PR_FAILURE) {
  42. fprintf(stderr, "PR_Close failed\n");
  43. exit(1);
  44. }
  45. }
  46. /*
  47. * Create some fd's. These fd's should come from
  48. * the fd cache. Verify the FIFO ordering of the fd
  49. * cache.
  50. */
  51. for (i = 0; i < numfds; i++) {
  52. fds[i] = PR_NewTCPSocket();
  53. if (NULL == fds[i]) {
  54. fprintf(stderr, "PR_NewTCPSocket failed\n");
  55. exit(1);
  56. }
  57. if (fds[i] != savefds[i]) {
  58. fprintf(stderr, "fd cache malfunctioned\n");
  59. exit(1);
  60. }
  61. }
  62. /* Put the fd's back to the fd cache. */
  63. for (i = 0; i < numfds; i++) {
  64. if (PR_Close(savefds[i]) == PR_FAILURE) {
  65. fprintf(stderr, "PR_Close failed\n");
  66. exit(1);
  67. }
  68. }
  69. /* Switch to the fd cache. */
  70. PR_SetFDCacheSize(0, FD_CACHE_SIZE);
  71. for (i = 0; i < numfds; i++) {
  72. fds[i] = PR_NewTCPSocket();
  73. if (NULL == fds[i]) {
  74. fprintf(stderr, "PR_NewTCPSocket failed\n");
  75. exit(1);
  76. }
  77. #ifdef ORDER_PRESERVED
  78. if (fds[i] != savefds[i]) {
  79. fprintf(stderr, "fd cache malfunctioned\n");
  80. exit(1);
  81. }
  82. #else
  83. savefds[i] = fds[i];
  84. #endif
  85. }
  86. for (i = 0; i < numfds; i++) {
  87. if (PR_Close(savefds[i]) == PR_FAILURE) {
  88. fprintf(stderr, "PR_Close failed\n");
  89. exit(1);
  90. }
  91. }
  92. for (i = 0; i < numfds; i++) {
  93. fds[i] = PR_NewTCPSocket();
  94. if (NULL == fds[i]) {
  95. fprintf(stderr, "PR_NewTCPSocket failed\n");
  96. exit(1);
  97. }
  98. if (fds[i] != savefds[i]) {
  99. fprintf(stderr, "fd cache malfunctioned\n");
  100. exit(1);
  101. }
  102. }
  103. for (i = 0; i < numfds; i++) {
  104. if (PR_Close(savefds[i]) == PR_FAILURE) {
  105. fprintf(stderr, "PR_Close failed\n");
  106. exit(1);
  107. }
  108. }
  109. PR_Cleanup();
  110. printf("PASS\n");
  111. return 0;
  112. }