1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim set: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 "nsThreadUtils.h"
10
11 /**
12 * This file tests that the storage service cannot be initialized off the main
13 * thread.
14 */
15
16 ////////////////////////////////////////////////////////////////////////////////
17 //// Helpers
18
19 class ServiceInitializer : public mozilla::Runnable
20 {
21 public:
ServiceInitializer()22 ServiceInitializer() : mozilla::Runnable("ServiceInitializer") {}
Run()23 NS_IMETHOD Run() override
24 {
25 // Use an explicit do_GetService instead of getService so that the check in
26 // getService doesn't blow up.
27 nsCOMPtr<mozIStorageService> service = do_GetService("@mozilla.org/storage/service;1");
28 do_check_false(service);
29 return NS_OK;
30 }
31 };
32
33 ////////////////////////////////////////////////////////////////////////////////
34 //// Test Functions
35
36 // HACK ALERT: this test fails if it runs after any of the other storage tests
37 // because the storage service will have been initialized and the
38 // do_GetService() call in ServiceInitializer::Run will succeed. So we need a
39 // way to force it to run before all the other storage gtests. As it happens,
40 // gtest has a concept of "death tests" that, among other things, run before
41 // all non-death tests. By merely using a "DeathTest" suffix here this becomes
42 // a death test -- even though it doesn't use any of the normal death test
43 // features -- which ensures this is the first storage test to run.
TEST(storage_service_init_background_thread_DeathTest,Test)44 TEST(storage_service_init_background_thread_DeathTest, Test)
45 {
46 nsCOMPtr<nsIRunnable> event = new ServiceInitializer();
47 do_check_true(event);
48
49 nsCOMPtr<nsIThread> thread;
50 do_check_success(NS_NewNamedThread("StorageService", getter_AddRefs(thread)));
51
52 do_check_success(thread->Dispatch(event, NS_DISPATCH_NORMAL));
53
54 // Shutting down the thread will spin the event loop until all work in its
55 // event queue is completed. This will act as our thread synchronization.
56 do_check_success(thread->Shutdown());
57 }
58