test_mutex.cpp (2035B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "storage_test_harness.h" 8 9 #include "SQLiteMutex.h" 10 11 using namespace mozilla; 12 using namespace mozilla::storage; 13 14 /** 15 * This file test our sqlite3_mutex wrapper in SQLiteMutex.h. 16 */ 17 18 TEST(storage_mutex, AutoLock) 19 { 20 int lockTypes[] = { 21 SQLITE_MUTEX_FAST, 22 SQLITE_MUTEX_RECURSIVE, 23 }; 24 for (int lockType : lockTypes) { 25 // Get our test mutex (we have to allocate a SQLite mutex of the right type 26 // too!). 27 SQLiteMutex mutex("TestMutex"); 28 sqlite3_mutex* inner = sqlite3_mutex_alloc(lockType); 29 do_check_true(inner); 30 mutex.initWithMutex(inner); 31 32 // And test that our automatic locking wrapper works as expected. 33 mutex.assertNotCurrentThreadOwns(); 34 { 35 SQLiteMutexAutoLock lockedScope(mutex); 36 mutex.assertCurrentThreadOwns(); 37 } 38 mutex.assertNotCurrentThreadOwns(); 39 40 // Free the wrapped mutex - we don't need it anymore. 41 sqlite3_mutex_free(inner); 42 } 43 } 44 45 TEST(storage_mutex, AutoUnlock) 46 { 47 int lockTypes[] = { 48 SQLITE_MUTEX_FAST, 49 SQLITE_MUTEX_RECURSIVE, 50 }; 51 for (int lockType : lockTypes) { 52 // Get our test mutex (we have to allocate a SQLite mutex of the right type 53 // too!). 54 SQLiteMutex mutex("TestMutex"); 55 sqlite3_mutex* inner = sqlite3_mutex_alloc(lockType); 56 do_check_true(inner); 57 mutex.initWithMutex(inner); 58 59 // And test that our automatic unlocking wrapper works as expected. 60 { 61 SQLiteMutexAutoLock lockedScope(mutex); 62 63 { 64 SQLiteMutexAutoUnlock unlockedScope(mutex); 65 mutex.assertNotCurrentThreadOwns(); 66 } 67 mutex.assertCurrentThreadOwns(); 68 } 69 70 // Free the wrapped mutex - we don't need it anymore. 71 sqlite3_mutex_free(inner); 72 } 73 }