Skip to content

Commit c565775

Browse files
committed
Fix race condition in test
The write wait guarantees that the input was written, but not that the output was read into the log. I modified the tests to partition the output so the order fo the assertions matters. This prevents the text `> hello` from earlier from triggering a false positive.
1 parent ff23f11 commit c565775

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

test/integration/background_stdin_test.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,24 @@ def test_print_output_on_exit
6767
on_success_dir: dir.join(SUCCESS_DIRNAME)
6868
).call
6969

70-
assert_include(actual: io.string, include_str: "Warning background task is still running, cleaning up: `background_ungraceful_exit`")
71-
assert_include(actual: io.string, include_str: "Log contents for `/usr/bin/env bash -c")
72-
assert_include(actual: io.string, include_str: "You said: hello")
70+
logs = io.string
71+
72+
match_after = partition_match_after(actual: logs, include_str: "Warning background task is still running, cleaning up: `background_ungraceful_exit`")
73+
match_after = partition_match_after(actual: match_after, include_str: "Log contents for `/usr/bin/env bash -c")
74+
partition_match_after(actual: match_after, include_str: "> hello")
7375
end
7476
end
7577
end
7678

77-
def assert_include(actual:, include_str:)
78-
assert actual.include?(include_str), "Expected to find `#{include_str}` in output, but did not. Output:\n#{actual}"
79+
# Finds the include_str if it exists or raises an error
80+
# Returns the contents of that string and everything after it
81+
#
82+
# Used to handle the case where output might be in the logs twice and we want to verify the order
83+
def partition_match_after(actual:, include_str:)
84+
_before, match, after = actual.partition(include_str)
85+
found = match && !match.empty?
86+
assert found, "Expected to find `#{include_str}` in output, but did not. Output:\n#{actual}"
87+
[match, after].join
7988
end
8089

8190
def loop_script

0 commit comments

Comments
 (0)