storage/test/storage_test_harness.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:596b2f787b48
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 "TestHarness.h"
8 #include "nsMemory.h"
9 #include "nsThreadUtils.h"
10 #include "nsDirectoryServiceDefs.h"
11 #include "mozIStorageService.h"
12 #include "mozIStorageConnection.h"
13 #include "mozIStorageStatementCallback.h"
14 #include "mozIStorageCompletionCallback.h"
15 #include "mozIStorageBindingParamsArray.h"
16 #include "mozIStorageBindingParams.h"
17 #include "mozIStorageAsyncStatement.h"
18 #include "mozIStorageStatement.h"
19 #include "mozIStoragePendingStatement.h"
20 #include "mozIStorageError.h"
21 #include "nsThreadUtils.h"
22
23 static int gTotalTests = 0;
24 static int gPassedTests = 0;
25
26 #define do_check_true(aCondition) \
27 PR_BEGIN_MACRO \
28 gTotalTests++; \
29 if (aCondition) { \
30 gPassedTests++; \
31 } else { \
32 fail("%s | Expected true, got false at line %d", __FILE__, __LINE__); \
33 } \
34 PR_END_MACRO
35
36 #define do_check_false(aCondition) \
37 PR_BEGIN_MACRO \
38 gTotalTests++; \
39 if (!aCondition) { \
40 gPassedTests++; \
41 } else { \
42 fail("%s | Expected false, got true at line %d", __FILE__, __LINE__); \
43 } \
44 PR_END_MACRO
45
46 #define do_check_success(aResult) \
47 do_check_true(NS_SUCCEEDED(aResult))
48
49 #ifdef LINUX
50 // XXX Linux opt builds on tinderbox are orange due to linking with stdlib.
51 // This is sad and annoying, but it's a workaround that works.
52 #define do_check_eq(aExpected, aActual) \
53 do_check_true(aExpected == aActual)
54 #else
55 #include <sstream>
56
57 // Print nsresult as uint32_t
58 std::ostream& operator<<(std::ostream& aStream, const nsresult aInput)
59 {
60 return aStream << static_cast<uint32_t>(aInput);
61 }
62
63 #define do_check_eq(aExpected, aActual) \
64 PR_BEGIN_MACRO \
65 gTotalTests++; \
66 if (aExpected == aActual) { \
67 gPassedTests++; \
68 } else { \
69 std::ostringstream temp; \
70 temp << __FILE__ << " | Expected '" << aExpected << "', got '"; \
71 temp << aActual <<"' at line " << __LINE__; \
72 fail(temp.str().c_str()); \
73 } \
74 PR_END_MACRO
75 #endif
76
77 already_AddRefed<mozIStorageService>
78 getService()
79 {
80 nsCOMPtr<mozIStorageService> ss =
81 do_GetService("@mozilla.org/storage/service;1");
82 do_check_true(ss);
83 return ss.forget();
84 }
85
86 already_AddRefed<mozIStorageConnection>
87 getMemoryDatabase()
88 {
89 nsCOMPtr<mozIStorageService> ss = getService();
90 nsCOMPtr<mozIStorageConnection> conn;
91 nsresult rv = ss->OpenSpecialDatabase("memory", getter_AddRefs(conn));
92 do_check_success(rv);
93 return conn.forget();
94 }
95
96 already_AddRefed<mozIStorageConnection>
97 getDatabase()
98 {
99 nsCOMPtr<nsIFile> dbFile;
100 (void)NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
101 getter_AddRefs(dbFile));
102 NS_ASSERTION(dbFile, "The directory doesn't exists?!");
103
104 nsresult rv = dbFile->Append(NS_LITERAL_STRING("storage_test_db.sqlite"));
105 do_check_success(rv);
106
107 nsCOMPtr<mozIStorageService> ss = getService();
108 nsCOMPtr<mozIStorageConnection> conn;
109 rv = ss->OpenDatabase(dbFile, getter_AddRefs(conn));
110 do_check_success(rv);
111 return conn.forget();
112 }
113
114
115 class AsyncStatementSpinner : public mozIStorageStatementCallback
116 , public mozIStorageCompletionCallback
117 {
118 public:
119 NS_DECL_ISUPPORTS
120 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
121 NS_DECL_MOZISTORAGECOMPLETIONCALLBACK
122
123 AsyncStatementSpinner();
124
125 void SpinUntilCompleted();
126
127 uint16_t completionReason;
128
129 protected:
130 virtual ~AsyncStatementSpinner() {}
131 volatile bool mCompleted;
132 };
133
134 NS_IMPL_ISUPPORTS(AsyncStatementSpinner,
135 mozIStorageStatementCallback,
136 mozIStorageCompletionCallback)
137
138 AsyncStatementSpinner::AsyncStatementSpinner()
139 : completionReason(0)
140 , mCompleted(false)
141 {
142 }
143
144 NS_IMETHODIMP
145 AsyncStatementSpinner::HandleResult(mozIStorageResultSet *aResultSet)
146 {
147 return NS_OK;
148 }
149
150 NS_IMETHODIMP
151 AsyncStatementSpinner::HandleError(mozIStorageError *aError)
152 {
153 int32_t result;
154 nsresult rv = aError->GetResult(&result);
155 NS_ENSURE_SUCCESS(rv, rv);
156 nsAutoCString message;
157 rv = aError->GetMessage(message);
158 NS_ENSURE_SUCCESS(rv, rv);
159
160 nsAutoCString warnMsg;
161 warnMsg.Append("An error occurred while executing an async statement: ");
162 warnMsg.AppendInt(result);
163 warnMsg.Append(" ");
164 warnMsg.Append(message);
165 NS_WARNING(warnMsg.get());
166
167 return NS_OK;
168 }
169
170 NS_IMETHODIMP
171 AsyncStatementSpinner::HandleCompletion(uint16_t aReason)
172 {
173 completionReason = aReason;
174 mCompleted = true;
175 return NS_OK;
176 }
177
178 NS_IMETHODIMP
179 AsyncStatementSpinner::Complete(nsresult, nsISupports*)
180 {
181 mCompleted = true;
182 return NS_OK;
183 }
184
185 void AsyncStatementSpinner::SpinUntilCompleted()
186 {
187 nsCOMPtr<nsIThread> thread(::do_GetCurrentThread());
188 nsresult rv = NS_OK;
189 bool processed = true;
190 while (!mCompleted && NS_SUCCEEDED(rv)) {
191 rv = thread->ProcessNextEvent(true, &processed);
192 }
193 }
194
195 #define NS_DECL_ASYNCSTATEMENTSPINNER \
196 NS_IMETHOD HandleResult(mozIStorageResultSet *aResultSet);
197
198 ////////////////////////////////////////////////////////////////////////////////
199 //// Async Helpers
200
201 /**
202 * Execute an async statement, blocking the main thread until we get the
203 * callback completion notification.
204 */
205 void
206 blocking_async_execute(mozIStorageBaseStatement *stmt)
207 {
208 nsRefPtr<AsyncStatementSpinner> spinner(new AsyncStatementSpinner());
209
210 nsCOMPtr<mozIStoragePendingStatement> pendy;
211 (void)stmt->ExecuteAsync(spinner, getter_AddRefs(pendy));
212 spinner->SpinUntilCompleted();
213 }
214
215 /**
216 * Invoke AsyncClose on the given connection, blocking the main thread until we
217 * get the completion notification.
218 */
219 void
220 blocking_async_close(mozIStorageConnection *db)
221 {
222 nsRefPtr<AsyncStatementSpinner> spinner(new AsyncStatementSpinner());
223
224 db->AsyncClose(spinner);
225 spinner->SpinUntilCompleted();
226 }

mercurial