storage/test/test_mutex.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:b1d79b5bdd6f
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 void
19 test_AutoLock()
20 {
21 int lockTypes[] = {
22 SQLITE_MUTEX_FAST,
23 SQLITE_MUTEX_RECURSIVE,
24 };
25 for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
26 // Get our test mutex (we have to allocate a SQLite mutex of the right type
27 // too!).
28 SQLiteMutex mutex("TestMutex");
29 sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
30 do_check_true(inner);
31 mutex.initWithMutex(inner);
32
33 // And test that our automatic locking wrapper works as expected.
34 mutex.assertNotCurrentThreadOwns();
35 {
36 SQLiteMutexAutoLock lockedScope(mutex);
37 mutex.assertCurrentThreadOwns();
38 }
39 mutex.assertNotCurrentThreadOwns();
40
41 // Free the wrapped mutex - we don't need it anymore.
42 sqlite3_mutex_free(inner);
43 }
44 }
45
46 void
47 test_AutoUnlock()
48 {
49 int lockTypes[] = {
50 SQLITE_MUTEX_FAST,
51 SQLITE_MUTEX_RECURSIVE,
52 };
53 for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
54 // Get our test mutex (we have to allocate a SQLite mutex of the right type
55 // too!).
56 SQLiteMutex mutex("TestMutex");
57 sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
58 do_check_true(inner);
59 mutex.initWithMutex(inner);
60
61 // And test that our automatic unlocking wrapper works as expected.
62 {
63 SQLiteMutexAutoLock lockedScope(mutex);
64
65 {
66 SQLiteMutexAutoUnlock unlockedScope(mutex);
67 mutex.assertNotCurrentThreadOwns();
68 }
69 mutex.assertCurrentThreadOwns();
70 }
71
72 // Free the wrapped mutex - we don't need it anymore.
73 sqlite3_mutex_free(inner);
74 }
75 }
76
77 void (*gTests[])(void) = {
78 test_AutoLock,
79 test_AutoUnlock,
80 };
81
82 const char *file = __FILE__;
83 #define TEST_NAME "SQLiteMutex"
84 #define TEST_FILE file
85 #include "storage_test_harness_tail.h"

mercurial