From 9afed7d10dab367b8ffcf4c403e0645e49dbd91e Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 8 Oct 2022 17:25:36 +0900 Subject: [PATCH] test: Add test cases for mutex and wmutex functions There are currently no shellspec test cases for the mutex and wmutex modules, making it impossible to check if they are working correctly. This commit adds test cases for all functions in the mutex and wmutex modules. --- test/mutex_spec.sh | 184 ++++++++++++++++++++++++++++++++++++++++++++ test/wmutex_spec.sh | 182 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 366 insertions(+) create mode 100644 test/mutex_spec.sh create mode 100644 test/wmutex_spec.sh diff --git a/test/mutex_spec.sh b/test/mutex_spec.sh new file mode 100644 index 0000000..0e44291 --- /dev/null +++ b/test/mutex_spec.sh @@ -0,0 +1,184 @@ +#!/bin/bash + +. toolbox.sh +include "mutex" + +Describe "mutex_trylock()" + It "returns success if the lock was created" + _test_mutex_trylock_success() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! mutex_trylock "$mutex"; then + return 2 + elif ! [ -L "$mutex" ]; then + err=3 + elif ! mutex_unlock "$mutex"; then + err=4 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + When call _test_mutex_trylock_success + The status should equal 0 + End + + It "returns failure if the lock was not created" + _test_mutex_trylock_failure() { + local mutex + + if ! mutex=$(mktemp --directory --dry-run); then + return 1 + fi + + mutex+="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + if mutex_trylock "$mutex"; then + mutex_unlock "$mutex" + return 2 + elif [ -L "$mutex" ]; then + return 3 + fi + + return 0 + } + When call _test_mutex_trylock_failure + The status should equal 0 + End +End + +Describe "mutex_lock()" + It "returns success if the lock was created" + _test_mutex_lock_success() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! mutex_lock "$mutex" 1; then + return 2 + elif ! [ -L "$mutex" ]; then + err=3 + elif ! mutex_unlock "$mutex"; then + err=4 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + When call _test_mutex_lock_success + The status should equal 0 + End + + It "returns failure if the lock was not created" + _test_mutex_lock_failure() { + local mutex + + if ! mutex=$(mktemp --directory --dry-run); then + return 1 + fi + + mutex+="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + if mutex_lock "$mutex" 1; then + mutex_unlock "$mutex" + return 2 + elif [ -L "$mutex" ]; then + return 3 + fi + + return 0 + } + When call _test_mutex_lock_failure + The status should equal 0 + End +End + +Describe "mutex_unlock()" + It "returns success if the lock was removed" + _test_mutex_unlock_success() { + local mutex + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! mutex_lock "$mutex" 1; then + return 2 + elif ! mutex_unlock "$mutex"; then + return 3 + elif [ -L "$mutex" ]; then + return 4 + fi + + return 0 + } + + When call _test_mutex_unlock_success + The status should equal 0 + End + + It "returns failure if the lock was not removed" + _test_mutex_unlock_failure() { + local mutex + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif [ -L "$mutex" ]; then + return 2 + elif mutex_unlock "$mutex"; then + return 3 + fi + + return 0 + } + + When call _test_mutex_unlock_failure + The status should equal 0 + End + + It "returns failure if the mutex belongs to a different process" + _test_mutex_unlock_foreign() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! mutex_lock "$mutex" 1; then + return 2 + elif ! [ -L "$mutex" ]; then + return 3 + fi + + if bash -c ". toolbox.sh; include mutex; mutex_unlock \"$mutex\""; then + err=4 + elif ! [ -L "$mutex" ]; then + err=5 + elif ! mutex_unlock "$mutex"; then + err=6 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + + When call _test_mutex_unlock_foreign + The status should equal 0 + End +End diff --git a/test/wmutex_spec.sh b/test/wmutex_spec.sh new file mode 100644 index 0000000..61adfec --- /dev/null +++ b/test/wmutex_spec.sh @@ -0,0 +1,182 @@ +#!/bin/bash + +. toolbox.sh +include "wmutex" + +Describe "wmutex_trylock()" + It "returns success if the lock was created" + _test_wmutex_trylock_success() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! wmutex_trylock "$mutex"; then + return 2 + elif ! [ -L "$mutex" ]; then + err=3 + elif ! wmutex_unlock "$mutex"; then + err=4 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + When call _test_wmutex_trylock_success + The status should equal 0 + End + + It "returns failure if the lock was not created" + _test_wmutex_trylock_failure() { + local mutex + + if ! mutex=$(mktemp --directory --dry-run); then + return 1 + fi + + mutex+="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + if wmutex_trylock "$mutex"; then + wmutex_unlock "$mutex" + return 2 + elif [ -L "$mutex" ]; then + return 3 + fi + + return 0 + } + When call _test_wmutex_trylock_failure + The status should equal 0 + End +End + +Describe "wmutex_lock()" + It "returns success if the lock was created" + _test_wmutex_lock_success() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! wmutex_lock "$mutex" 1; then + return 2 + elif ! [ -L "$mutex" ]; then + err=3 + elif ! wmutex_unlock "$mutex"; then + err=4 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + When call _test_wmutex_lock_success + The status should equal 0 + End + + It "returns failure if the lock was not created" + _test_wmutex_lock_failure() { + local mutex + + if ! mutex=$(mktemp --directory --dry-run); then + return 1 + fi + + mutex+="/$RANDOM/$RANDOM/$RANDOM/$RANDOM" + + if wmutex_lock "$mutex" 1; then + wmutex_unlock "$mutex" + return 2 + elif [ -L "$mutex" ]; then + return 3 + fi + + return 0 + } + When call _test_wmutex_lock_failure + The status should equal 0 + End +End + +Describe "wmutex_unlock()" + It "returns success if the lock was removed" + _test_wmutex_unlock_success() { + local mutex + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! wmutex_lock "$mutex" 1; then + return 2 + elif ! wmutex_unlock "$mutex"; then + return 3 + elif [ -L "$mutex" ]; then + return 4 + fi + + return 0 + } + + When call _test_wmutex_unlock_success + The status should equal 0 + End + + It "returns failure if the lock was not removed" + _test_wmutex_unlock_failure() { + local mutex + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif [ -L "$mutex" ]; then + return 2 + elif wmutex_unlock "$mutex"; then + return 3 + fi + + return 0 + } + + When call _test_wmutex_unlock_failure + The status should equal 0 + End + + It "returns success if the mutex belongs to a different process" + _test_wmutex_unlock_foreign() { + local mutex + local -i err + + err=0 + + if ! mutex=$(mktemp --dry-run); then + return 1 + elif ! wmutex_lock "$mutex" 1; then + return 2 + elif ! [ -L "$mutex" ]; then + return 3 + fi + + if ! bash -c ". toolbox.sh; include wmutex; wmutex_unlock \"$mutex\""; then + err=4 + elif [ -L "$mutex" ]; then + err=5 + fi + + if [ -L "$mutex" ]; then + rm -f "$mutex" + fi + + return "$err" + } + + When call _test_wmutex_unlock_foreign + The status should equal 0 + End +End -- 2.47.3