test_spinningSynchronousClose.cpp (2775B)
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 #include "prinrval.h" 9 10 using namespace mozilla; 11 12 /** 13 * Helper to verify that the event loop was spun. As long as this is dispatched 14 * prior to a call to Close()/SpinningSynchronousClose() we are guaranteed this 15 * will be run if the event loop is spun to perform a close. This is because 16 * SpinningSynchronousClose must spin the event loop to realize the close 17 * completed and our runnable will already be enqueued and therefore run before 18 * the AsyncCloseConnection's callback. Note that this invariant may be 19 * violated if our runnables end up in different queues thanks to Quantum 20 * changes, so this test may need to be updated if the close dispatch changes. 21 */ 22 class CompletionRunnable final : public Runnable { 23 public: 24 explicit CompletionRunnable() 25 : Runnable("CompletionRunnable"), mDone(false) {} 26 27 NS_IMETHOD Run() override { 28 mDone = true; 29 return NS_OK; 30 } 31 32 bool mDone; 33 }; 34 35 // Can only run in optimized builds, or it would assert. 36 #ifndef DEBUG 37 TEST(storage_spinningSynchronousClose, CloseOnAsync) 38 { 39 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 40 // Run an async statement. 41 nsCOMPtr<mozIStorageAsyncStatement> stmt; 42 do_check_success(db->CreateAsyncStatement( 43 "CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns, getter_AddRefs(stmt))); 44 nsCOMPtr<mozIStoragePendingStatement> p; 45 do_check_success(stmt->ExecuteAsync(nullptr, getter_AddRefs(p))); 46 do_check_success(stmt->Finalize()); 47 48 // Wrongly use Close() instead of AsyncClose(). 49 RefPtr<CompletionRunnable> event = new CompletionRunnable(); 50 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); 51 do_check_false(NS_SUCCEEDED(db->Close())); 52 do_check_true(event->mDone); 53 } 54 #endif 55 56 TEST(storage_spinningSynchronousClose, spinningSynchronousCloseOnAsync) 57 { 58 nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase()); 59 // Run an async statement. 60 nsCOMPtr<mozIStorageAsyncStatement> stmt; 61 do_check_success(db->CreateAsyncStatement( 62 "CREATE TABLE test (id INTEGER PRIMARY KEY)"_ns, getter_AddRefs(stmt))); 63 nsCOMPtr<mozIStoragePendingStatement> p; 64 do_check_success(stmt->ExecuteAsync(nullptr, getter_AddRefs(p))); 65 do_check_success(stmt->Finalize()); 66 67 // Use the spinning close API. 68 RefPtr<CompletionRunnable> event = new CompletionRunnable(); 69 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); 70 do_check_success(db->SpinningSynchronousClose()); 71 do_check_true(event->mDone); 72 }