test-getaddrinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdlib.h>
  24. #define CONCURRENT_COUNT 10
  25. static const char* name = "localhost";
  26. static int getaddrinfo_cbs = 0;
  27. /* data used for running multiple calls concurrently */
  28. static uv_getaddrinfo_t* getaddrinfo_handle;
  29. static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT];
  30. static int callback_counts[CONCURRENT_COUNT];
  31. static int fail_cb_called;
  32. static void getaddrinfo_fail_cb(uv_getaddrinfo_t* req,
  33. int status,
  34. struct addrinfo* res) {
  35. ASSERT(fail_cb_called == 0);
  36. ASSERT(status < 0);
  37. ASSERT(res == NULL);
  38. uv_freeaddrinfo(res); /* Should not crash. */
  39. fail_cb_called++;
  40. }
  41. static void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle,
  42. int status,
  43. struct addrinfo* res) {
  44. ASSERT(handle == getaddrinfo_handle);
  45. getaddrinfo_cbs++;
  46. free(handle);
  47. uv_freeaddrinfo(res);
  48. }
  49. static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
  50. int status,
  51. struct addrinfo* res) {
  52. int i;
  53. int* data = (int*)handle->data;
  54. for (i = 0; i < CONCURRENT_COUNT; i++) {
  55. if (&getaddrinfo_handles[i] == handle) {
  56. ASSERT(i == *data);
  57. callback_counts[i]++;
  58. break;
  59. }
  60. }
  61. ASSERT (i < CONCURRENT_COUNT);
  62. free(data);
  63. uv_freeaddrinfo(res);
  64. getaddrinfo_cbs++;
  65. }
  66. TEST_IMPL(getaddrinfo_fail) {
  67. uv_getaddrinfo_t req;
  68. ASSERT(0 == uv_getaddrinfo(uv_default_loop(),
  69. &req,
  70. getaddrinfo_fail_cb,
  71. "xyzzy.xyzzy.xyzzy",
  72. NULL,
  73. NULL));
  74. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  75. ASSERT(fail_cb_called == 1);
  76. MAKE_VALGRIND_HAPPY();
  77. return 0;
  78. }
  79. TEST_IMPL(getaddrinfo_basic) {
  80. int r;
  81. getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t));
  82. r = uv_getaddrinfo(uv_default_loop(),
  83. getaddrinfo_handle,
  84. &getaddrinfo_basic_cb,
  85. name,
  86. NULL,
  87. NULL);
  88. ASSERT(r == 0);
  89. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  90. ASSERT(getaddrinfo_cbs == 1);
  91. MAKE_VALGRIND_HAPPY();
  92. return 0;
  93. }
  94. TEST_IMPL(getaddrinfo_concurrent) {
  95. int i, r;
  96. int* data;
  97. for (i = 0; i < CONCURRENT_COUNT; i++) {
  98. callback_counts[i] = 0;
  99. data = (int*)malloc(sizeof(int));
  100. *data = i;
  101. getaddrinfo_handles[i].data = data;
  102. r = uv_getaddrinfo(uv_default_loop(),
  103. &getaddrinfo_handles[i],
  104. &getaddrinfo_cuncurrent_cb,
  105. name,
  106. NULL,
  107. NULL);
  108. ASSERT(r == 0);
  109. }
  110. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  111. for (i = 0; i < CONCURRENT_COUNT; i++) {
  112. ASSERT(callback_counts[i] == 1);
  113. }
  114. MAKE_VALGRIND_HAPPY();
  115. return 0;
  116. }