aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/unit1399.c
blob: d951504ea95cbf5c9308e4800588ede76ddb2017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
#include "curlcheck.h"

#include "urldata.h"
#include "progress.h"

static int usec_magnitude = 1000000;

static bool unit_setup(void)
{
  return CURLE_OK;
}

static void unit_stop(void)
{

}

static bool usec_matches_seconds(time_t time_usec, int expected_seconds)
{
  int time_sec = (int)(time_usec / usec_magnitude);
  bool same = (time_sec == expected_seconds);
  fprintf(stderr, "is %d us same as %d seconds? %s\n",
          (int)time_usec, expected_seconds,
          same?"Yes":"No");
  return same;
}

UNITTEST_START
  struct Curl_easy data;
  struct timeval now = Curl_tvnow();

  data.progress.t_starttransfer = 0;
  data.progress.t_redirect = 0;

  /*
  * Set the startsingle time to a second ago. This time is used by
  * Curl_pgrsTime to calculate how much time the events takes.
  * t_starttransfer should be updated to reflect the difference from this time
  * when `Curl_pgrsTime is invoked.
  */
  data.progress.t_startsingle.tv_sec = now.tv_sec - 1;
  data.progress.t_startsingle.tv_usec = now.tv_usec;

  Curl_pgrsTime(&data, TIMER_STARTTRANSFER);

  fail_unless(usec_matches_seconds(data.progress.t_starttransfer, 1),
              "about 1 second should have passed");

  /*
  * Update the startsingle time to a second ago to simulate another second has
  * passed.
  * Now t_starttransfer should not be changed, as t_starttransfer has already
  * occurred and another invocation of `Curl_pgrsTime` for TIMER_STARTTRANSFER
  * is superfluous.
  */
  data.progress.t_startsingle.tv_sec = now.tv_sec - 2;
  data.progress.t_startsingle.tv_usec = now.tv_usec;

  Curl_pgrsTime(&data, TIMER_STARTTRANSFER);

  fail_unless(usec_matches_seconds(data.progress.t_starttransfer, 1),
              "about 1 second should have passed");

  /*
  * Simulate what happens after a redirect has occurred.
  *
  * Since the value of t_starttransfer is set to the value from the first
  * request, it should be updated when a transfer occurs such that
  * t_starttransfer is the starttransfer time of the redirect request.
  */
  data.progress.t_startsingle.tv_sec = now.tv_sec - 3;
  data.progress.t_startsingle.tv_usec = now.tv_usec;
  data.progress.t_redirect = data.progress.t_starttransfer + 1;

  Curl_pgrsTime(&data, TIMER_STARTTRANSFER);

  fail_unless(usec_matches_seconds(data.progress.t_starttransfer, 3),
              "about 3 second should have passed");
UNITTEST_STOP