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
| | /*
* A test helper to exercise the progress display.
*
* Reads instructions from standard input, one instruction per line:
*
* "start[ <total>[ <title>]]" - Call start_progress(title, total),
* when "start" use a title of
* "Working hard" with a total of 0.
* "progress <items>" - Call display_progress() with the given item count
* as parameter.
* "throughput <bytes> <millis> - Call display_throughput() with the given
* byte count as parameter. The 'millis'
* specify the time elapsed since the
* start_progress() call.
* "update" - Set the 'progress_update' flag.
* "signal" - Synonym for "update", used for self-documenting tests,
* i.e. "expect signal here due to hanging ("signal")
* v.s. it was time to update ("update").
* "stop" - Call stop_progress().
*
* See 't0500-progress-display.sh' for examples.
*/
#define GIT_TEST_PROGRESS_ONLY
#include "test-tool.h"
#include "gettext.h"
#include "parse-options.h"
#include "progress.h"
#include "strbuf.h"
int cmd__progress(int argc, const char **argv)
{
const char *default_title = "Working hard";
char *detached_title = NULL;
struct strbuf line = STRBUF_INIT;
struct progress *progress = NULL;
const char *usage[] = {
"test-tool progress <stdin",
NULL
};
struct option options[] = {
OPT_END(),
};
argc = parse_options(argc, argv, NULL, options, usage, 0);
if (argc)
usage_with_options(usage, options);
while (strbuf_getline(&line, stdin) != EOF) {
char *end;
if (!strcmp(line.buf, "start")) {
progress = start_progress_testing(default_title, 0);
} else if (skip_prefix(line.buf, "start ", (const char **) &end)) {
uint64_t total = strtoull(end, &end, 10);
if (*end == '\0') {
progress = start_progress_testing(default_title, total);
} else if (*end == ' ') {
if (detached_title)
free(detached_title);
detached_title = strbuf_detach(&line, NULL);
progress = start_progress_testing(end + 1, total);
} else {
die("invalid input: '%s'\n", line.buf);
}
} else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
uint64_t item_count = strtoull(end, &end, 10);
if (*end != '\0')
die("invalid input: '%s'\n", line.buf);
display_progress(progress, item_count);
} else if (skip_prefix(line.buf, "throughput ",
(const char **) &end)) {
uint64_t byte_count, test_ms;
byte_count = strtoull(end, &end, 10);
if (*end != ' ')
die("invalid input: '%s'\n", line.buf);
test_ms = strtoull(end + 1, &end, 10);
if (*end != '\0')
die("invalid input: '%s'\n", line.buf);
progress->test_getnanotime = test_ms * 1000 * 1000;
display_throughput(progress, byte_count);
} else if (!strcmp(line.buf, "update") ||
!strcmp(line.buf, "signal")) {
test_progress_force_update();
} else if (!strcmp(line.buf, "stop")) {
stop_progress(&progress);
} else {
die("invalid input: '%s'\n", line.buf);
}
}
if (detached_title)
free(detached_title);
return 0;
}
|