test-getaddrinfo.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle,
  32. int status,
  33. struct addrinfo* res) {
  34. ASSERT(handle == getaddrinfo_handle);
  35. getaddrinfo_cbs++;
  36. free(handle);
  37. uv_freeaddrinfo(res);
  38. }
  39. static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
  40. int status,
  41. struct addrinfo* res) {
  42. int i;
  43. int* data = (int*)handle->data;
  44. for (i = 0; i < CONCURRENT_COUNT; i++) {
  45. if (&getaddrinfo_handles[i] == handle) {
  46. ASSERT(i == *data);
  47. callback_counts[i]++;
  48. break;
  49. }
  50. }
  51. ASSERT (i < CONCURRENT_COUNT);
  52. free(data);
  53. uv_freeaddrinfo(res);
  54. getaddrinfo_cbs++;
  55. }
  56. TEST_IMPL(getaddrinfo_basic) {
  57. int r;
  58. getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t));
  59. r = uv_getaddrinfo(uv_default_loop(),
  60. getaddrinfo_handle,
  61. &getaddrinfo_basic_cb,
  62. name,
  63. NULL,
  64. NULL);
  65. ASSERT(r == 0);
  66. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  67. ASSERT(getaddrinfo_cbs == 1);
  68. MAKE_VALGRIND_HAPPY();
  69. return 0;
  70. }
  71. TEST_IMPL(getaddrinfo_concurrent) {
  72. int i, r;
  73. int* data;
  74. for (i = 0; i < CONCURRENT_COUNT; i++) {
  75. callback_counts[i] = 0;
  76. data = (int*)malloc(sizeof(int));
  77. *data = i;
  78. getaddrinfo_handles[i].data = data;
  79. r = uv_getaddrinfo(uv_default_loop(),
  80. &getaddrinfo_handles[i],
  81. &getaddrinfo_cuncurrent_cb,
  82. name,
  83. NULL,
  84. NULL);
  85. ASSERT(r == 0);
  86. }
  87. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  88. for (i = 0; i < CONCURRENT_COUNT; i++) {
  89. ASSERT(callback_counts[i] == 1);
  90. }
  91. MAKE_VALGRIND_HAPPY();
  92. return 0;
  93. }