-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from buildkite-plugins/toote_race_condition
FS backend race condition
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
lock () { | ||
local file="${1}.lock" | ||
( | ||
set -o noclobber | ||
date > "${file}" | ||
) 2>/dev/null | ||
} | ||
|
||
wait_and_lock () { | ||
local file="${1}.lock" | ||
local max_attempts="${2:-5}" | ||
|
||
for ATTEMPT in $(seq 1 "${max_attempts}"); do | ||
if ! lock "${1}"; then | ||
echo 'Waiting for folder lock' | ||
sleep "${ATTEMPT}" | ||
else | ||
return 0 | ||
fi | ||
done | ||
|
||
return 1 | ||
} | ||
|
||
release_lock () { | ||
local file="${1}.lock" | ||
rm -f "${file}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env bats | ||
|
||
setup() { | ||
load "${BATS_PLUGIN_PATH}/load.bash" | ||
|
||
source "${PWD}/lib/lock.bash" | ||
} | ||
|
||
@test 'basic locking works as expected' { | ||
LOCKFILE="${BATS_TEST_TMPDIR}/lock.file" | ||
|
||
run lock "${LOCKFILE}" | ||
assert_success | ||
assert_output '' | ||
|
||
run lock "${LOCKFILE}" | ||
assert_failure | ||
assert_output '' | ||
|
||
run release_lock "${LOCKFILE}" | ||
assert_success | ||
assert_output '' | ||
|
||
run lock "${LOCKFILE}" | ||
assert_success | ||
assert_output '' | ||
|
||
rm -f "${LOCKFILE}" | ||
} | ||
|
||
|
||
@test 'lock times out' { | ||
LOCKFILE="${BATS_TEST_TMPDIR}/lock.file" | ||
|
||
run wait_and_lock "${LOCKFILE}" | ||
assert_success | ||
assert_output '' | ||
|
||
run wait_and_lock "${LOCKFILE}" 1 | ||
assert_failure | ||
assert_equal "$(echo "${output}" | wc -l)" "1" | ||
|
||
run wait_and_lock "${LOCKFILE}" | ||
assert_failure | ||
assert_equal "$(echo "${output}" | wc -l)" "5" | ||
|
||
run release_lock "${LOCKFILE}" | ||
assert_success | ||
assert_output '' | ||
|
||
rm -f "${LOCKFILE}" | ||
} |