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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is storage test code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "storage_test_harness.h"
41 :
42 : #include "SQLiteMutex.h"
43 :
44 : using namespace mozilla;
45 : using namespace mozilla::storage;
46 :
47 : /**
48 : * This file test our sqlite3_mutex wrapper in SQLiteMutex.h.
49 : */
50 :
51 : void
52 1 : test_AutoLock()
53 : {
54 : int lockTypes[] = {
55 : SQLITE_MUTEX_FAST,
56 : SQLITE_MUTEX_RECURSIVE,
57 1 : };
58 3 : for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
59 : // Get our test mutex (we have to allocate a SQLite mutex of the right type
60 : // too!).
61 4 : SQLiteMutex mutex("TestMutex");
62 2 : sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
63 2 : do_check_true(inner);
64 2 : mutex.initWithMutex(inner);
65 :
66 : // And test that our automatic locking wrapper works as expected.
67 2 : mutex.assertNotCurrentThreadOwns();
68 : {
69 4 : SQLiteMutexAutoLock lockedScope(mutex);
70 2 : mutex.assertCurrentThreadOwns();
71 : }
72 2 : mutex.assertNotCurrentThreadOwns();
73 :
74 : // Free the wrapped mutex - we don't need it anymore.
75 2 : sqlite3_mutex_free(inner);
76 : }
77 1 : }
78 :
79 : void
80 1 : test_AutoUnlock()
81 : {
82 : int lockTypes[] = {
83 : SQLITE_MUTEX_FAST,
84 : SQLITE_MUTEX_RECURSIVE,
85 1 : };
86 3 : for (size_t i = 0; i < ArrayLength(lockTypes); i++) {
87 : // Get our test mutex (we have to allocate a SQLite mutex of the right type
88 : // too!).
89 4 : SQLiteMutex mutex("TestMutex");
90 2 : sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]);
91 2 : do_check_true(inner);
92 2 : mutex.initWithMutex(inner);
93 :
94 : // And test that our automatic unlocking wrapper works as expected.
95 : {
96 4 : SQLiteMutexAutoLock lockedScope(mutex);
97 :
98 : {
99 4 : SQLiteMutexAutoUnlock unlockedScope(mutex);
100 2 : mutex.assertNotCurrentThreadOwns();
101 : }
102 2 : mutex.assertCurrentThreadOwns();
103 : }
104 :
105 : // Free the wrapped mutex - we don't need it anymore.
106 2 : sqlite3_mutex_free(inner);
107 : }
108 1 : }
109 :
110 : void (*gTests[])(void) = {
111 : test_AutoLock,
112 : test_AutoUnlock,
113 : };
114 :
115 : const char *file = __FILE__;
116 : #define TEST_NAME "SQLiteMutex"
117 : #define TEST_FILE file
118 : #include "storage_test_harness_tail.h"
|