storage/test/test_mutex.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/test_mutex.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "storage_test_harness.h"
    1.11 +
    1.12 +#include "SQLiteMutex.h"
    1.13 +
    1.14 +using namespace mozilla;
    1.15 +using namespace mozilla::storage;
    1.16 +
    1.17 +/**
    1.18 + * This file test our sqlite3_mutex wrapper in SQLiteMutex.h.
    1.19 + */
    1.20 +
    1.21 +void
    1.22 +test_AutoLock()
    1.23 +{
    1.24 +  int lockTypes[] = {
    1.25 +    SQLITE_MUTEX_FAST,
    1.26 +    SQLITE_MUTEX_RECURSIVE,
    1.27 +  };
    1.28 +  for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
    1.29 +    // Get our test mutex (we have to allocate a SQLite mutex of the right type
    1.30 +    // too!).
    1.31 +    SQLiteMutex mutex("TestMutex");
    1.32 +    sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
    1.33 +    do_check_true(inner);
    1.34 +    mutex.initWithMutex(inner);
    1.35 +
    1.36 +    // And test that our automatic locking wrapper works as expected.
    1.37 +    mutex.assertNotCurrentThreadOwns();
    1.38 +    {
    1.39 +      SQLiteMutexAutoLock lockedScope(mutex);
    1.40 +      mutex.assertCurrentThreadOwns();
    1.41 +    }
    1.42 +    mutex.assertNotCurrentThreadOwns();
    1.43 +
    1.44 +    // Free the wrapped mutex - we don't need it anymore.
    1.45 +    sqlite3_mutex_free(inner);
    1.46 +  }
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +test_AutoUnlock()
    1.51 +{
    1.52 +  int lockTypes[] = {
    1.53 +    SQLITE_MUTEX_FAST,
    1.54 +    SQLITE_MUTEX_RECURSIVE,
    1.55 +  };
    1.56 +  for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
    1.57 +    // Get our test mutex (we have to allocate a SQLite mutex of the right type
    1.58 +    // too!).
    1.59 +    SQLiteMutex mutex("TestMutex");
    1.60 +    sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
    1.61 +    do_check_true(inner);
    1.62 +    mutex.initWithMutex(inner);
    1.63 +
    1.64 +    // And test that our automatic unlocking wrapper works as expected.
    1.65 +    {
    1.66 +      SQLiteMutexAutoLock lockedScope(mutex);
    1.67 +
    1.68 +      {
    1.69 +        SQLiteMutexAutoUnlock unlockedScope(mutex);
    1.70 +        mutex.assertNotCurrentThreadOwns();
    1.71 +      }
    1.72 +      mutex.assertCurrentThreadOwns();
    1.73 +    }
    1.74 +
    1.75 +    // Free the wrapped mutex - we don't need it anymore.
    1.76 +    sqlite3_mutex_free(inner);
    1.77 +  }
    1.78 +}
    1.79 +
    1.80 +void (*gTests[])(void) = {
    1.81 +  test_AutoLock,
    1.82 +  test_AutoUnlock,
    1.83 +};
    1.84 +
    1.85 +const char *file = __FILE__;
    1.86 +#define TEST_NAME "SQLiteMutex"
    1.87 +#define TEST_FILE file
    1.88 +#include "storage_test_harness_tail.h"

mercurial