68 lines
2 KiB
Bash
68 lines
2 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
|
|
|
||
|
|
fail() {
|
||
|
|
echo "changes_consumer_handler.sh: FAIL: $1" >&2
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
tmp_dir="$(mktemp -d /tmp/changes-consumer-handler.XXXXXX)"
|
||
|
|
cleanup() {
|
||
|
|
rm -rf "${tmp_dir}"
|
||
|
|
}
|
||
|
|
trap cleanup EXIT
|
||
|
|
|
||
|
|
mkdir -p "${tmp_dir}/bin"
|
||
|
|
cat > "${tmp_dir}/bin/curl" <<'MOCK'
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
printf '%s\n%s' '{"events":[{"kind":"edge_upsert","edge_ref":"e1"}],"next_cursor":"g1_next"}' '200'
|
||
|
|
MOCK
|
||
|
|
chmod +x "${tmp_dir}/bin/curl"
|
||
|
|
|
||
|
|
out_file="${tmp_dir}/out.log"
|
||
|
|
err_file="${tmp_dir}/err.log"
|
||
|
|
|
||
|
|
# Case 1: handler succeeds -> cursor advances.
|
||
|
|
cursor_success="${tmp_dir}/cursor.success"
|
||
|
|
printf '%s' "g1_start" > "${cursor_success}"
|
||
|
|
|
||
|
|
PATH="${tmp_dir}/bin:${PATH}" \
|
||
|
|
CURSOR_FILE="${cursor_success}" \
|
||
|
|
SYNC_LIMIT=10 \
|
||
|
|
SYNC_WAIT_MS=1 \
|
||
|
|
SOCK="${tmp_dir}/fake.sock" \
|
||
|
|
BASE="http://localhost" \
|
||
|
|
SPACE="app1" \
|
||
|
|
CHANGES_EVENT_HANDLER='printf "%s\n" "${EVENT_JSON}" >/dev/null' \
|
||
|
|
"${ROOT_DIR}/scripts/changes_consumer.sh" --once >"${out_file}" 2>"${err_file}" || fail "consumer failed on handler success case"
|
||
|
|
|
||
|
|
[[ -f "${cursor_success}" ]] || fail "cursor file missing after handler success case"
|
||
|
|
[[ "$(cat "${cursor_success}")" == "g1_next" ]] || fail "cursor did not advance after handler success"
|
||
|
|
|
||
|
|
# Case 2: handler fails -> cursor does not advance.
|
||
|
|
cursor_fail="${tmp_dir}/cursor.fail"
|
||
|
|
printf '%s' "g1_start" > "${cursor_fail}"
|
||
|
|
|
||
|
|
set +e
|
||
|
|
PATH="${tmp_dir}/bin:${PATH}" \
|
||
|
|
CURSOR_FILE="${cursor_fail}" \
|
||
|
|
SYNC_LIMIT=10 \
|
||
|
|
SYNC_WAIT_MS=1 \
|
||
|
|
SOCK="${tmp_dir}/fake.sock" \
|
||
|
|
BASE="http://localhost" \
|
||
|
|
SPACE="app1" \
|
||
|
|
CHANGES_EVENT_HANDLER='exit 1' \
|
||
|
|
"${ROOT_DIR}/scripts/changes_consumer.sh" --once >"${out_file}" 2>"${err_file}"
|
||
|
|
rc=$?
|
||
|
|
set -e
|
||
|
|
|
||
|
|
[[ "${rc}" -ne 0 ]] || fail "expected non-zero exit when handler fails"
|
||
|
|
[[ -f "${cursor_fail}" ]] || fail "cursor file missing after handler failure case"
|
||
|
|
[[ "$(cat "${cursor_fail}")" == "g1_start" ]] || fail "cursor advanced despite handler failure"
|
||
|
|
grep -q "cursor not advanced" "${err_file}" || fail "missing handler-failure cursor warning"
|
||
|
|
|
||
|
|
echo "changes_consumer_handler.sh: PASS"
|