#!/bin/sh
# Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Checks that the locker interprocess sync locks are acquired and
# released correctly.

failcount=0
tempfile=@abs_builddir@/logger_lock_test_tempfile_$$
destfile=@abs_builddir@/logger_lock_test_destfile_$$

passfail() {
    if [ $1 -eq 0 ]; then
        echo " pass"
    else
        echo " FAIL"
        failcount=`expr $failcount + $1`
    fi
}

echo -n  "Testing that logger acquires and releases locks correctly:"
cat > $tempfile << .
LOGGER_LOCK_TEST: MUTEXLOCK
LOGGER_LOCK_TEST: LOCK
INFO  [kea.log] LOG_LOCK_TEST_MESSAGE this is a test message.
LOGGER_LOCK_TEST: UNLOCK
.
rm -f $destfile
KEA_LOGGER_SEVERITY=INFO KEA_LOGGER_DESTINATION=stdout ./logger_lock_test | \
    sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\]/[\1]/' > $destfile
cut -d' ' -f3- $destfile | diff $tempfile -
passfail $?

# Tidy up.
rm -f $tempfile $destfile

exit $failcount
