]> git.corax.cc Git - toolbox/commitdiff
test/mutex: Add testcases for mutex module
authorMatthias Kruk <m@m10k.eu>
Tue, 20 Apr 2021 14:18:41 +0000 (23:18 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 20 Apr 2021 14:18:41 +0000 (23:18 +0900)
This commit adds testcases for all functions of the mutex module.

test/mutex.sh [new file with mode: 0755]

diff --git a/test/mutex.sh b/test/mutex.sh
new file mode 100755 (executable)
index 0000000..5edbc16
--- /dev/null
@@ -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" )
+}