-
Notifications
You must be signed in to change notification settings - Fork 198
/
SQLiteBuildDBTest.cpp
125 lines (100 loc) · 4.32 KB
/
SQLiteBuildDBTest.cpp
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
//===- unittests/Core/SQLiteBuildDBTest.cpp -----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Core/BuildDB.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "gtest/gtest.h"
#include <sqlite3.h>
using namespace llbuild;
using namespace llbuild::core;
TEST(SQLiteBuildDBTest, ErrorHandling) {
// Create a temporary file.
llvm::SmallString<256> dbPath;
auto ec = llvm::sys::fs::createTemporaryFile("build", "db", dbPath);
EXPECT_EQ(bool(ec), false);
const char* path = dbPath.c_str();
fprintf(stderr, "using db: %s\n", path);
std::string error;
std::unique_ptr<BuildDB> buildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_TRUE(buildDB != nullptr);
EXPECT_EQ(error, "");
sqlite3 *db = nullptr;
sqlite3_open(path, &db);
sqlite3_exec(db, "PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE;", nullptr, nullptr, nullptr);
buildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_FALSE(buildDB == nullptr);
// The database is opened lazily, thus run an operation that will cause it
// to be opened and verify that it fails as expected.
bool result = true;
buildDB->getCurrentEpoch(&result, &error);
EXPECT_FALSE(result);
EXPECT_TRUE(error.find("database is locked") != std::string::npos);
// Clean up database connections before unlinking
sqlite3_exec(db, "END;", nullptr, nullptr, nullptr);
sqlite3_close(db);
buildDB = nullptr;
ec = llvm::sys::fs::remove(dbPath.str());
EXPECT_EQ(bool(ec), false);
}
TEST(SQLiteBuildDBTest, LockedWhileBuilding) {
// Create a temporary file.
llvm::SmallString<256> dbPath;
auto ec = llvm::sys::fs::createTemporaryFile("build", "db", dbPath);
EXPECT_EQ(bool(ec), false);
const char* path = dbPath.c_str();
fprintf(stderr, "using db: %s\n", path);
std::string error;
std::unique_ptr<BuildDB> buildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_TRUE(buildDB != nullptr);
EXPECT_EQ(error, "");
std::unique_ptr<BuildDB> secondBuildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_TRUE(buildDB != nullptr);
EXPECT_EQ(error, "");
bool result = buildDB->buildStarted(&error);
EXPECT_TRUE(result);
EXPECT_EQ(error, "");
// Tests that we cannot start a second build with an existing connection
result = secondBuildDB->buildStarted(&error);
EXPECT_FALSE(result);
EXPECT_TRUE(error.find("database is locked") != std::string::npos);
// Tests that we cannot create new connections while a build is running
std::unique_ptr<BuildDB> otherBuildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_FALSE(otherBuildDB == nullptr);
// The database is opened lazily, thus run an operation that will cause it
// to be opened and verify that it fails as expected.
bool success = true;
otherBuildDB->getCurrentEpoch(&success, &error);
EXPECT_FALSE(success);
EXPECT_TRUE(error.find("database is locked") != std::string::npos);
// Clean up database connections before unlinking
buildDB->buildComplete();
buildDB = nullptr;
secondBuildDB = nullptr;
otherBuildDB = nullptr;
ec = llvm::sys::fs::remove(dbPath.str());
EXPECT_EQ(bool(ec), false);
}
TEST(SQLiteBuildDBTest, CloseDBConnectionAfterCloseCall) {
// Create a temporary file.
llvm::SmallString<256> dbPath;
auto ec = llvm::sys::fs::createTemporaryFile("build", "db", dbPath);
EXPECT_EQ(bool(ec), false);
const char* path = dbPath.c_str();
fprintf(stderr, "using db: %s\n", path);
std::string error;
std::unique_ptr<BuildDB> buildDB = createSQLiteBuildDB(dbPath, 1, /* recreateUnmatchedVersion = */ true, &error);
EXPECT_TRUE(buildDB != nullptr);
EXPECT_EQ(error, "");
buildDB->buildStarted(&error);
EXPECT_EQ(error, "");
buildDB->buildComplete();
}