// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
-#include "util/interprocess_sync_file.h"
+#include <util/interprocess_sync_file.h>
+#include <util/threads/thread.h>
+
#include <gtest/gtest.h>
+#include <boost/bind.hpp>
+
#include <unistd.h>
+using namespace isc::util::thread;
using namespace std;
namespace isc {
EXPECT_EQ (0, unlink(TEST_DATA_TOPBUILDDIR "/test_lockfile"));
}
+void*
+threadTest(bool* locked)
+{
+ InterprocessSyncFile sync2("test");
+ InterprocessSyncLocker locker2(sync2);
+
+ *locked = !locker2.tryLock();
+
+ return NULL;
+}
+
+TEST(InterprocessSyncFileTest, TestLockThreaded) {
+ InterprocessSyncFile sync("test");
+ InterprocessSyncLocker locker(sync);
+
+ EXPECT_FALSE(locker.isLocked());
+ EXPECT_TRUE(locker.lock());
+ EXPECT_TRUE(locker.isLocked());
+
+ bool locked_in_other_thread = false;
+
+ // Here, we check that a lock has been taken by creating a new
+ // thread and checking from the new thread that a lock exists. The
+ // lock attempt must fail to pass our check.
+ Thread thread(boost::bind(&threadTest, &locked_in_other_thread));
+ thread.wait();
+
+ EXPECT_TRUE(locked_in_other_thread);
+
+ // Release the lock and try again. This time, the attempt must pass.
+
+ EXPECT_TRUE(locker.unlock());
+ EXPECT_FALSE(locker.isLocked());
+
+ Thread thread2(boost::bind(&threadTest, &locked_in_other_thread));
+ thread2.wait();
+
+ EXPECT_FALSE(locked_in_other_thread);
+
+ EXPECT_EQ (0, unlink(TEST_DATA_TOPBUILDDIR "/test_lockfile"));
+}
+
TEST(InterprocessSyncFileTest, TestMultipleFilesDirect) {
InterprocessSyncFile sync("test1");
InterprocessSyncLocker locker(sync);