Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/storage/test/gtest/test_mutex.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
9
#include "SQLiteMutex.h"
10
11
using namespace mozilla;
12
using namespace mozilla::storage;
13
14
/**
15
 * This file test our sqlite3_mutex wrapper in SQLiteMutex.h.
16
 */
17
18
TEST(storage_mutex, AutoLock)
19
0
{
20
0
  int lockTypes[] = {
21
0
    SQLITE_MUTEX_FAST,
22
0
    SQLITE_MUTEX_RECURSIVE,
23
0
  };
24
0
  for (int lockType : lockTypes) {
25
0
    // Get our test mutex (we have to allocate a SQLite mutex of the right type
26
0
    // too!).
27
0
    SQLiteMutex mutex("TestMutex");
28
0
    sqlite3_mutex *inner = sqlite3_mutex_alloc(lockType);
29
0
    do_check_true(inner);
30
0
    mutex.initWithMutex(inner);
31
0
32
0
    // And test that our automatic locking wrapper works as expected.
33
0
    mutex.assertNotCurrentThreadOwns();
34
0
    {
35
0
      SQLiteMutexAutoLock lockedScope(mutex);
36
0
      mutex.assertCurrentThreadOwns();
37
0
    }
38
0
    mutex.assertNotCurrentThreadOwns();
39
0
40
0
    // Free the wrapped mutex - we don't need it anymore.
41
0
    sqlite3_mutex_free(inner);
42
0
  }
43
0
}
44
45
TEST(storage_mutex, AutoUnlock)
46
0
{
47
0
  int lockTypes[] = {
48
0
    SQLITE_MUTEX_FAST,
49
0
    SQLITE_MUTEX_RECURSIVE,
50
0
  };
51
0
  for (int lockType : lockTypes) {
52
0
    // Get our test mutex (we have to allocate a SQLite mutex of the right type
53
0
    // too!).
54
0
    SQLiteMutex mutex("TestMutex");
55
0
    sqlite3_mutex *inner = sqlite3_mutex_alloc(lockType);
56
0
    do_check_true(inner);
57
0
    mutex.initWithMutex(inner);
58
0
59
0
    // And test that our automatic unlocking wrapper works as expected.
60
0
    {
61
0
      SQLiteMutexAutoLock lockedScope(mutex);
62
0
63
0
      {
64
0
        SQLiteMutexAutoUnlock unlockedScope(mutex);
65
0
        mutex.assertNotCurrentThreadOwns();
66
0
      }
67
0
      mutex.assertCurrentThreadOwns();
68
0
    }
69
0
70
0
    // Free the wrapped mutex - we don't need it anymore.
71
0
    sqlite3_mutex_free(inner);
72
0
  }
73
0
}
74