Skip to content

Commit

Permalink
test: add --jobs tests
Browse files Browse the repository at this point in the history
Testing that parallel execution actually works is a bit difficult, so
the most trivial way of doing it is to just have a bunch of sleep tests
and make sure that they run faster than would be possible if they were
run in serial.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Feb 10, 2019
1 parent da65d1b commit 7e6dd94
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ ARG bashver=latest

FROM bash:${bashver}

RUN ln -s /opt/bats/bin/bats /usr/sbin/bats
# Install parallel and accept the citation notice (we aren't using this in a
# context where it make sense to cite GNU Parallel).
RUN apk add --no-cache parallel && \
mkdir -p ~/.parallel && touch ~/.parallel/will-cite

RUN ln -s /opt/bats/bin/bats /usr/sbin/bats
COPY . /opt/bats/

ENTRYPOINT ["bash", "/usr/sbin/bats"]
16 changes: 16 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,19 @@ END_OF_ERR_MSG
[ "${lines[5]}" = '# bar' ]
[ "${lines[6]}" = '# baz' ]
}

@test "parallel test execution with --jobs" {
type -p parallel &>/dev/null || skip "--jobs requires GNU parallel"

SECONDS=0
run bats --jobs 10 "$FIXTURE_ROOT/parallel.bats"
duration="$SECONDS"
[ "$status" -eq 0 ]
# Make sure the lines are in-order.
[[ "${lines[0]}" == "1..10" ]]
for t in {1..10}; do
[[ "${lines[$t]}" == "ok $t slow test $t" ]]
done
# In theory it should take 3s, but let's give it bit of extra time instead.
[[ "$duration" -lt 20 ]]
}
39 changes: 39 additions & 0 deletions test/fixtures/bats/parallel.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@test "slow test 1" {
sleep 3s
}

@test "slow test 2" {
sleep 3s
}

@test "slow test 3" {
sleep 3s
}

@test "slow test 4" {
sleep 3s
}

@test "slow test 5" {
sleep 3s
}

@test "slow test 6" {
sleep 3s
}

@test "slow test 7" {
sleep 3s
}

@test "slow test 8" {
sleep 3s
}

@test "slow test 9" {
sleep 3s
}

@test "slow test 10" {
sleep 3s
}
1 change: 1 addition & 0 deletions test/fixtures/suite/parallel/parallel1.bats
1 change: 1 addition & 0 deletions test/fixtures/suite/parallel/parallel2.bats
1 change: 1 addition & 0 deletions test/fixtures/suite/parallel/parallel3.bats
1 change: 1 addition & 0 deletions test/fixtures/suite/parallel/parallel4.bats
20 changes: 20 additions & 0 deletions test/suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,23 @@ fixtures suite
[ "${lines[1]}" = 'ok 1 baz in a' ]
[ "${lines[2]}" = 'ok 2 bar_in_b' ]
}

@test "parallel suite execution with --jobs" {
type -p parallel &>/dev/null || skip "--jobs requires GNU parallel"

SECONDS=0
run bats --jobs 40 "$FIXTURE_ROOT/parallel"
duration="$SECONDS"
[ "$status" -eq 0 ]
# Make sure the lines are in-order.
[[ "${lines[0]}" == "1..40" ]]
i=0
for s in {1..4}; do
for t in {1..10}; do
((++i))
[[ "${lines[$i]}" == "ok $i slow test $t" ]]
done
done
# In theory it should take 3s, but let's give it bit of extra time instead.
[[ "$duration" -lt 20 ]]
}

0 comments on commit 7e6dd94

Please sign in to comment.