unit1399.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.haxx.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. ***************************************************************************/
  22. #include "curlcheck.h"
  23. #include "urldata.h"
  24. #include "progress.h"
  25. static int usec_magnitude = 1000000;
  26. static bool unit_setup(void)
  27. {
  28. return CURLE_OK;
  29. }
  30. static void unit_stop(void)
  31. {
  32. }
  33. /*
  34. * Invoke Curl_pgrsTime for TIMER_STARTSINGLE to trigger the behavior that
  35. * manages is_t_startransfer_set, but fake the t_startsingle time for purposes
  36. * of the test.
  37. */
  38. static void fake_t_startsingle_time(struct Curl_easy *data,
  39. struct curltime fake_now,
  40. int seconds_offset)
  41. {
  42. Curl_pgrsTime(data, TIMER_STARTSINGLE);
  43. data->progress.t_startsingle.tv_sec = fake_now.tv_sec + seconds_offset;
  44. data->progress.t_startsingle.tv_usec = fake_now.tv_usec;
  45. }
  46. static bool usec_matches_seconds(time_t time_usec, int expected_seconds)
  47. {
  48. int time_sec = (int)(time_usec / usec_magnitude);
  49. bool same = (time_sec == expected_seconds);
  50. fprintf(stderr, "is %d us same as %d seconds? %s\n",
  51. (int)time_usec, expected_seconds,
  52. same?"Yes":"No");
  53. return same;
  54. }
  55. static void expect_timer_seconds(struct Curl_easy *data, int seconds)
  56. {
  57. char msg[64];
  58. snprintf(msg, sizeof(msg), "about %d seconds should have passed", seconds);
  59. fail_unless(usec_matches_seconds(data->progress.t_nslookup, seconds), msg);
  60. fail_unless(usec_matches_seconds(data->progress.t_connect, seconds), msg);
  61. fail_unless(usec_matches_seconds(data->progress.t_appconnect, seconds), msg);
  62. fail_unless(usec_matches_seconds(data->progress.t_pretransfer, seconds),
  63. msg);
  64. fail_unless(usec_matches_seconds(data->progress.t_starttransfer, seconds),
  65. msg);
  66. }
  67. /* Scenario: simulate a redirect. When a redirect occurs, t_nslookup,
  68. * t_connect, t_appconnect, t_pretransfer, and t_starttransfer are addative.
  69. * E.g., if t_starttransfer took 2 seconds initially and took another 1
  70. * second for the redirect request, then the resulting t_starttransfer should
  71. * be 3 seconds. */
  72. UNITTEST_START
  73. struct Curl_easy data;
  74. struct curltime now = Curl_now();
  75. data.progress.t_nslookup = 0;
  76. data.progress.t_connect = 0;
  77. data.progress.t_appconnect = 0;
  78. data.progress.t_pretransfer = 0;
  79. data.progress.t_starttransfer = 0;
  80. data.progress.t_redirect = 0;
  81. data.progress.start.tv_sec = now.tv_sec - 2;
  82. data.progress.start.tv_usec = now.tv_usec;
  83. fake_t_startsingle_time(&data, now, -2);
  84. Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
  85. Curl_pgrsTime(&data, TIMER_CONNECT);
  86. Curl_pgrsTime(&data, TIMER_APPCONNECT);
  87. Curl_pgrsTime(&data, TIMER_PRETRANSFER);
  88. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  89. expect_timer_seconds(&data, 2);
  90. /* now simulate the redirect */
  91. data.progress.t_redirect = data.progress.t_starttransfer + 1;
  92. fake_t_startsingle_time(&data, now, -1);
  93. Curl_pgrsTime(&data, TIMER_NAMELOOKUP);
  94. Curl_pgrsTime(&data, TIMER_CONNECT);
  95. Curl_pgrsTime(&data, TIMER_APPCONNECT);
  96. Curl_pgrsTime(&data, TIMER_PRETRANSFER);
  97. /* ensure t_starttransfer is only set on the first invocation by attempting
  98. * to set it twice */
  99. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  100. Curl_pgrsTime(&data, TIMER_STARTTRANSFER);
  101. expect_timer_seconds(&data, 3);
  102. UNITTEST_STOP