sstephenson / bats

Bash Automated Testing System
MIT License
7.12k stars 518 forks source link

Missing $status while testing race condition/concurrent call #187

Open edouard-lopez opened 7 years ago

edouard-lopez commented 7 years ago

I had some concurrent calls issue in my project so I write the following test to simulate a series of parallel calls:

  1. start first bulk in background (cf. { … } &) ;
  2. start second bulk in background (cf. { … } &) ;
  3. wait for both subprocess to terminate
setup() {
    export NO_ERROR=0
}

@test "should support concurrent addition" {
    {
        for ((i=0; $i < 10; i++)); do
            mast-utils add-channel NAME=bats.test PRINTER=parallel1-$i;
        done
    } & pid_bulk1=$!
    {
        for ((i=0; $i < 10; i++)); do
            mast-utils add-channel NAME=bats.test PRINTER=parallel2-$i;
        done
    } & pid_bulk2=$!

    wait $pid_bulk1 $pid_bulk2
    channels_count=$(source /etc/mast/bats.test; echo ${#ForwardPort[@]})
    echo [$status] $channels_count

    [[ "$status" == "$NO_ERROR" ]]
    (( $channels_count == 20 ))
}

Question

As you can see I'm checking for the $status to be sure there is $NO_ERROR, but it fails. When printing $status it is empty.

Why is that ? Is there a way to test both statuses?

dimo414 commented 7 years ago

$status is set by the run function - if you're not invoking your command via run you should just be checking $? like you would in standard Bash. You can get the exit code of a background process via wait or implement some other capturing mechanism manually.