From: Matthias Kruk Date: Tue, 20 Apr 2021 14:18:41 +0000 (+0900) Subject: test/mutex: Add testcases for mutex module X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=36a9b8d3c0cae10b66aa710133c871dc7d4c9be2;p=toolbox test/mutex: Add testcases for mutex module This commit adds testcases for all functions of the mutex module. --- diff --git a/test/mutex.sh b/test/mutex.sh new file mode 100755 index 0000000..5edbc16 --- /dev/null +++ b/test/mutex.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bats + +. toolbox.sh +include "mutex" + +setup() { + delete=() + + return 0 +} + + +teardown() { + if (( ${#delete[@]} > 0 )); then + echo "${delete[*]}" >> /tmp/teardown.bats + rm -f "${delete[@]}" + fi + + return 0 +} + +@test "mutex_trylock() returns success if the lock was created" { + local name + + name="test_$RANDOM" + + mutex_trylock "$name" + [ -L "$name" ] + + delete+=("$name") +} + +@test "mutex_trylock() returns failure if the lock was not created" { + local name + + name="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + ! [ -d "${name%/*}" ] + ! mutex_trylock "$name" +} + +@test "mutex_lock() returns success if the lock was created" { + local name + + name="test_$RANDOM" + + mutex_lock "$name" + [ -L "$name" ] + + delete+=("$name") +} + +@test "mutex_lock() returns failure if the lock was not created" { + local name + + # Unlikely to exist path + name="/$RANDOM/$RANDOM/RANDOM/$RANDOM" + + ! [ -d "${name%/*}" ] + ! mutex_lock "$name" + ! [ -L "$name" ] +} + +@test "mutex_unlock() returns success if the lock was removed" { + local name + + name="test_$RANDOM" + + mutex_lock "$name" + mutex_unlock "$name" + ! [ -e "$name" ] +} + +@test "mutex_unlock() returns failure if the lock was not removed" { + local name + + # Unlikely to exist path + name="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + ! [ -d "${name%/*}" ] + ! mutex_unlock "$name" +} + +@test "mutex_unlock() returns failure if mutex belongs to a different process" { + local name + + name="test_$RANDOM" + + mutex_lock "$name" + delete+=("$name") + + ( ! mutex_unlock "$name" ) +}