unit1399.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "urldata.h"
  26. #include "progress.h"
  27. static int usec_magnitude = 1000000;
  28. static bool unit_setup(void)
  29. {
  30. return CURLE_OK;
  31. }
  32. static void unit_stop(void)
  33. {
  34. }
  35. /*
  36. * Invoke Curl_pgrsTime for TIMER_STARTSINGLE to trigger the behavior that
  37. * manages is_t_startransfer_set, but fake the t_startsingle time for purposes
  38. * of the test.
  39. */
  40. static void fake_t_startsingle_time(struct Curl_easy *data,
  41. struct curltime fake_now,
  42. int seconds_offset)
  43. {
  44. Curl_pgrsTime(data, TIMER_STARTSINGLE);
  45. data->progress.t_startsingle.tv_sec = fake_now.tv_sec + seconds_offset;
  46. data->progress.t_startsingle.tv_usec = fake_now.tv_usec;
  47. }
  48. static bool usec_matches_seconds(timediff_t time_usec, int expected_seconds)
  49. {
  50. int time_sec = (int)(time_usec / usec_magnitude);
  51. bool same = (time_sec == expected_seconds);
  52. fprintf(stderr, "is %d us same as %d seconds? %s\n",
  53. (int)time_usec, expected_seconds,
  54. same?"Yes":"No");
  55. return same;
  56. }
  57. static void expect_timer_seconds(struct Curl_easy *data, int seconds)
  58. {
  59. char msg[64];
  60. msnprintf(msg, sizeof(msg), "about %d seconds should have passed", seconds);
  61. fail_unless(usec_matches_seconds(data->progress.t_nslookup, seconds), msg);
  62. fail_unless(usec_matches_seconds(data->progress.t_connect, seconds), msg);
  63. fail_unless(usec_matches_seconds(data->progress.t_appconnect, seconds), msg);
  64. fail_unless(usec_matches_seconds(data->progress.t_pretransfer, seconds),
  65. msg);
  66. fail_unless(usec_matches_seconds(data->progress.t_starttransfer, seconds),
  67. msg);
  68. }
  69. /* Scenario: simulate a redirect. When a redirect occurs, t_nslookup,
  70. * t_connect, t_appconnect, t_pretransfer, and t_starttransfer are additive.
  71. * E.g., if t_starttransfer took 2 seconds initially and took another 1
  72. * second for the redirect request, then the resulting t_starttransfer should
  73. * be 3 seconds. */
  74. UNITTEST_START
  75. struct Curl_easy data;
  76. struct curltime now = Curl_now();
  77. data.progress.t_nslookup = 0;
  78. data.progress.t_connect = 0;
  79. data.progress.t_appconnect = 0;
  80. data.progress.t_pretransfer = 0;
  81. data.progress.t_starttransfer = 0;
  82. data.progress.t_redirect = 0;
  83. data.progress.start.tv_sec = now.tv_sec - 2;
  84. data.progress.start.tv_usec = now.tv_usec;
  85. fake_t_startsingle_time(&data, now, -2);
  86. Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
  87. Curl_pgrsTime(&data, TIMER_CONNECT);
  88. Curl_pgrsTime(&data, TIMER_APPCONNECT);
  89. Curl_pgrsTime(&data, TIMER_PRETRANSFER);
  90. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  91. expect_timer_seconds(&data, 2);
  92. /* now simulate the redirect */
  93. data.progress.t_redirect = data.progress.t_starttransfer + 1;
  94. fake_t_startsingle_time(&data, now, -1);
  95. Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
  96. Curl_pgrsTime(&data, TIMER_CONNECT);
  97. Curl_pgrsTime(&data, TIMER_APPCONNECT);
  98. Curl_pgrsTime(&data, TIMER_PRETRANSFER);
  99. /* ensure t_starttransfer is only set on the first invocation by attempting
  100. * to set it twice */
  101. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  102. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  103. expect_timer_seconds(&data, 3);
  104. UNITTEST_STOP