]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t7900: exercise detaching via trace2 regions
authorPatrick Steinhardt <ps@pks.im>
Mon, 19 Aug 2024 07:48:02 +0000 (09:48 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 21 Aug 2024 18:33:02 +0000 (11:33 -0700)
In t7900, we exercise the `--detach` logic by checking whether the
command ended up writing anything to its output or not. This supposedly
works because we close stdin, stdout and stderr when daemonizing. But
one, it breaks on platforms where daemonize is a no-op, like Windows.
And second, that git-maintenance(1) outputs anything at all in these
tests is a bug in the first place that we'll fix in a subsequent commit.

Introduce a new trace2 region around the detach which allows us to more
explicitly check whether the detaching logic was executed. This is a
much more direct way to exercise the logic, provides a potentially
useful signal to tracing logs and also works alright on platforms which
do not have the ability to daemonize.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
[jc: dropped a stale in-code comment from a test]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/gc.c
t/t7900-maintenance.sh

index bafee330a2408aa712d92f59944928491d9a73fc..13bc0572a325d29ae7d112df1ce0be41ef005c69 100644 (file)
@@ -1428,8 +1428,11 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,
        free(lock_path);
 
        /* Failure to daemonize is ok, we'll continue in foreground. */
-       if (opts->detach > 0)
+       if (opts->detach > 0) {
+               trace2_region_enter("maintenance", "detach", the_repository);
                daemonize();
+               trace2_region_leave("maintenance", "detach", the_repository);
+       }
 
        for (i = 0; !found_selected && i < TASK__COUNT; i++)
                found_selected = tasks[i].selected_order >= 0;
index 074eadcd1c940328ce2a1db156fb13449e1a22c4..18e0bb29d7c77692200eb97bd40bc107bc1836e9 100755 (executable)
@@ -947,11 +947,9 @@ test_expect_success '--no-detach causes maintenance to not run in background' '
                git config set maintenance.loose-objects.auto 1 &&
                git config set maintenance.incremental-repack.enabled true &&
 
-               # We have no better way to check whether or not the task ran in
-               # the background than to verify whether it output anything. The
-               # next testcase checks the reverse, making this somewhat safer.
-               git maintenance run --no-detach >out 2>&1 &&
-               test_line_count = 1 out
+               GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
+                       git maintenance run --no-detach >out 2>&1 &&
+               ! test_region maintenance detach trace.txt
        )
 '
 
@@ -971,9 +969,9 @@ test_expect_success '--detach causes maintenance to run in background' '
                # process, and by reading stdout we thus essentially wait for
                # that descriptor to get closed, which indicates that the child
                # is done, too.
-               output=$(git maintenance run --detach 2>&1 9>&1) &&
-               printf "%s" "$output" >output &&
-               test_must_be_empty output
+               does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
+                       git maintenance run --detach 9>&1) &&
+               test_region maintenance detach trace.txt
        )
 '