summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/gtest/TestSimpleFileInfo.cpp
blob: 8971ee54fbedad6c83e5d28f5ccbf45550626ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FileInfo.h"
#include "FileInfoImpl.h"
#include "FileInfoManager.h"

#include "gtest/gtest.h"

#include "mozilla/ArrayAlgorithm.h"
#include "mozilla/StaticMutex.h"
#include "nsTArray.h"

#include <array>

using namespace mozilla;
using namespace mozilla::dom::indexedDB;

class SimpleFileManager;

using SimpleFileInfo = FileInfo<SimpleFileManager>;

struct SimpleFileManagerStats final {
  // XXX We don't keep track of the specific aFileId parameters here, should we?

  size_t mAsyncDeleteFileCalls = 0;
  size_t mSyncDeleteFileCalls = 0;
};

class SimpleFileManager final : public FileInfoManager<SimpleFileManager>,
                                public AtomicSafeRefCounted<SimpleFileManager> {
 public:
  using FileInfoManager<SimpleFileManager>::MutexType;

  MOZ_DECLARE_REFCOUNTED_TYPENAME(SimpleFileManager)

  // SimpleFileManager functions that are used by SimpleFileInfo

  [[nodiscard]] nsresult AsyncDeleteFile(const int64_t aFileId) {
    MOZ_RELEASE_ASSERT(!mFileInfos.Contains(aFileId));

    if (mStats) {
      ++mStats->mAsyncDeleteFileCalls;
    }

    return NS_OK;
  }

  [[nodiscard]] nsresult SyncDeleteFile(const int64_t aFileId) {
    MOZ_RELEASE_ASSERT(!mFileInfos.Contains(aFileId));

    if (mStats) {
      ++mStats->mSyncDeleteFileCalls;
    }
    return NS_OK;
  }

  // Test-specific functions
  explicit SimpleFileManager(SimpleFileManagerStats* aStats = nullptr)
      : mStats{aStats} {}

  void CreateDBOnlyFileInfos() {
    for (const auto id : kDBOnlyFileInfoIds) {
      // Copied from within DatabaseFileManager::Init.

      mFileInfos.InsertOrUpdate(
          id, MakeNotNull<SimpleFileInfo*>(FileInfoManagerGuard{},
                                           SafeRefPtrFromThis(), id,
                                           static_cast<nsrefcnt>(1)));

      mLastFileId = std::max(id, mLastFileId);
    }
  }

  static MutexType& Mutex() { return sMutex; }

  static constexpr auto kDBOnlyFileInfoIds =
      std::array<int64_t, 3>{{10, 20, 30}};

 private:
  inline static MutexType sMutex;

  SimpleFileManagerStats* const mStats;
};

// These tests test the SimpleFileManager itself, to ensure the SimpleFileInfo
// tests below are valid.

TEST(DOM_IndexedDB_SimpleFileManager, Invalidate)
{
  const auto fileManager = MakeSafeRefPtr<SimpleFileManager>();

  fileManager->Invalidate();

  ASSERT_TRUE(fileManager->Invalidated());
}

// These tests mainly test SimpleFileInfo, which is a simplified version of
// DatabaseFileInfo (SimpleFileInfo doesn't work with real files stored on
// disk). The actual objects, DatabaseFileInfo and DatabaseFileManager are not
// tested here.

TEST(DOM_IndexedDB_SimpleFileInfo, Create)
{
  auto stats = SimpleFileManagerStats{};

  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);
    auto fileInfo = fileManager->CreateFileInfo();

    int32_t memRefCnt, dbRefCnt;
    fileInfo->GetReferences(&memRefCnt, &dbRefCnt);

    ASSERT_EQ(fileManager, &fileInfo->Manager());

    ASSERT_EQ(1, memRefCnt);
    ASSERT_EQ(0, dbRefCnt);
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  ASSERT_EQ(1u, stats.mAsyncDeleteFileCalls);
}

TEST(DOM_IndexedDB_SimpleFileInfo, CreateWithInitialDBRefCnt)
{
  auto stats = SimpleFileManagerStats{};

  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);
    fileManager->CreateDBOnlyFileInfos();

    for (const auto id : SimpleFileManager::kDBOnlyFileInfoIds) {
      const auto fileInfo = fileManager->GetFileInfo(id);
      ASSERT_NE(nullptr, fileInfo);

      int32_t memRefCnt, dbRefCnt;
      fileInfo->GetReferences(&memRefCnt, &dbRefCnt);

      ASSERT_EQ(fileManager, &fileInfo->Manager());

      ASSERT_EQ(1, memRefCnt);  // we hold one in fileInfo ourselves
      ASSERT_EQ(1, dbRefCnt);
    }
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  // Since the files have still non-zero dbRefCnt, nothing must be deleted.
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}

TEST(DOM_IndexedDB_SimpleFileInfo, CreateWithInitialDBRefCnt_Invalidate)
{
  auto stats = SimpleFileManagerStats{};

  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);
    fileManager->CreateDBOnlyFileInfos();

    const auto fileInfos = TransformIntoNewArray(
        SimpleFileManager::kDBOnlyFileInfoIds,
        [&fileManager](const auto id) { return fileManager->GetFileInfo(id); });

    fileManager->Invalidate();

    for (const auto& fileInfo : fileInfos) {
      int32_t memRefCnt, dbRefCnt;
      fileInfo->GetReferences(&memRefCnt, &dbRefCnt);

      ASSERT_EQ(1, memRefCnt);  // we hold one in fileInfo ourselves
      ASSERT_EQ(0, dbRefCnt);   // dbRefCnt was cleared by Invalidate
    }
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  // Since the files have still non-zero dbRefCnt, nothing must be deleted.
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}

TEST(DOM_IndexedDB_SimpleFileInfo, CreateWithInitialDBRefCnt_UpdateDBRefsToZero)
{
  auto stats = SimpleFileManagerStats{};

  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);
    fileManager->CreateDBOnlyFileInfos();

    const auto fileInfo =
        fileManager->GetFileInfo(SimpleFileManager::kDBOnlyFileInfoIds[0]);
    fileInfo->UpdateDBRefs(-1);

    int32_t memRefCnt, dbRefCnt;
    fileInfo->GetReferences(&memRefCnt, &dbRefCnt);

    ASSERT_EQ(1, memRefCnt);  // we hold one in fileInfo ourselves
    ASSERT_EQ(0, dbRefCnt);
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  ASSERT_EQ(1u, stats.mAsyncDeleteFileCalls);
}

TEST(DOM_IndexedDB_SimpleFileInfo, ReleaseWithFileManagerCleanup)
{
  auto stats = SimpleFileManagerStats{};
  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);
    fileManager->CreateDBOnlyFileInfos();

    auto* fileInfo = fileManager->CreateFileInfo().forget().take();
    fileInfo->Release(/* aSyncDeleteFile */ true);

    // This was the only reference and SimpleFileManager was not invalidated,
    // so SimpleFileManager::Cleanup should have been called.
    ASSERT_EQ(1u, stats.mSyncDeleteFileCalls);
  }
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}

#ifndef DEBUG
// These tests cause assertion failures in DEBUG builds.

TEST(DOM_IndexedDB_SimpleFileInfo, Invalidate_CreateFileInfo)
{
  auto stats = SimpleFileManagerStats{};
  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);

    fileManager->Invalidate();

    const auto fileInfo = fileManager->CreateFileInfo();
    Unused << fileInfo;

    ASSERT_EQ(nullptr, fileInfo);
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}
#endif

TEST(DOM_IndexedDB_SimpleFileInfo, Invalidate_Release)
{
  auto stats = SimpleFileManagerStats{};
  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);

    const auto fileInfo = fileManager->CreateFileInfo();
    Unused << fileInfo;

    fileManager->Invalidate();

    // SimpleFileManager was invalidated, so Release does not do any cleanup.
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}

TEST(DOM_IndexedDB_SimpleFileInfo, Invalidate_ReleaseWithFileManagerCleanup)
{
  auto stats = SimpleFileManagerStats{};
  {
    const auto fileManager = MakeSafeRefPtr<SimpleFileManager>(&stats);

    auto* fileInfo = fileManager->CreateFileInfo().forget().take();

    fileManager->Invalidate();

    // SimpleFileManager was invalidated, so Release does not do any cleanup.
    fileInfo->Release(/* aSyncDeleteFile */ true);
  }

  ASSERT_EQ(0u, stats.mSyncDeleteFileCalls);
  ASSERT_EQ(0u, stats.mAsyncDeleteFileCalls);
}

// XXX Add test for GetFileForFileInfo