Coverage Report

Created: 2018-09-25 14:53

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