-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <signal.h> | ||
#include <sys/mman.h> | ||
#include <unistd.h> | ||
|
||
#include "graph_test/graph_test.h" | ||
#include "main/kuzu.h" | ||
|
||
#ifdef __MVS__ | ||
#define MAP_ANONYMOUS 0x0 | ||
#endif | ||
|
||
using namespace kuzu::testing; | ||
using namespace kuzu::common; | ||
using namespace kuzu::main; | ||
|
||
namespace kuzu { | ||
namespace testing { | ||
|
||
class LockingTest : public EmptyDBTest { | ||
public: | ||
void SetUp() override { EmptyDBTest::SetUp(); } | ||
}; | ||
|
||
TEST_F(LockingTest, testReadLock) { | ||
uint64_t* count = (uint64_t*)mmap( | ||
NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, 0, 0); | ||
*count = 0; | ||
// create db first | ||
pid_t create_pid = fork(); | ||
if (create_pid == 0) { | ||
createDBAndConn(); | ||
ASSERT_TRUE( | ||
conn->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));") | ||
->isSuccess()); | ||
ASSERT_TRUE(conn->query("CREATE (:Person {name: 'Alice', age: 25});")->isSuccess()); | ||
exit(0); | ||
} | ||
waitpid(create_pid, NULL, 0); | ||
|
||
// test read write db | ||
pid_t pid = fork(); | ||
if (pid == 0) { | ||
std::unique_ptr<Database> db; | ||
systemConfig->access_mode = AccessMode::READ_ONLY; | ||
EXPECT_NO_THROW(createDBAndConn()); | ||
(*count)++; | ||
ASSERT_TRUE(conn->query("MATCH (:Person) RETURN COUNT(*)")->isSuccess()); | ||
while (true) { | ||
usleep(100); | ||
ASSERT_TRUE(conn->query("MATCH (:Person) RETURN COUNT(*)")->isSuccess()); | ||
} | ||
} else if (pid > 0) { | ||
std::unique_ptr<Database> db; | ||
while (*count == 0) { | ||
usleep(100); | ||
} | ||
systemConfig->access_mode = AccessMode::READ_WRITE; | ||
// try to open db for writing, this should fail | ||
EXPECT_ANY_THROW(createDBAndConn()); | ||
// but opening db for reading should work | ||
systemConfig->access_mode = AccessMode::READ_ONLY; | ||
EXPECT_NO_THROW(createDBAndConn()); | ||
ASSERT_TRUE(conn->query("MATCH (:Person) RETURN COUNT(*)")->isSuccess()); | ||
// kill the child | ||
if (kill(pid, SIGKILL) != 0) { | ||
FAIL(); | ||
} | ||
} | ||
} | ||
|
||
TEST_F(LockingTest, testWriteLock) { | ||
uint64_t* count = (uint64_t*)mmap( | ||
NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, 0, 0); | ||
*count = 0; | ||
// test write lock | ||
// fork away a child | ||
pid_t pid = fork(); | ||
if (pid == 0) { | ||
// child process | ||
// open db for writing | ||
systemConfig->access_mode = AccessMode::READ_WRITE; | ||
EXPECT_NO_THROW(createDBAndConn()); | ||
// opened db for writing | ||
// insert some values | ||
(*count)++; | ||
ASSERT_TRUE( | ||
conn->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));") | ||
->isSuccess()); | ||
ASSERT_TRUE(conn->query("CREATE (:Person {name: 'Alice', age: 25});")->isSuccess()); | ||
while (true) { | ||
ASSERT_TRUE(conn->query("MATCH (:Person) RETURN COUNT(*)")->isSuccess()); | ||
usleep(100); | ||
} | ||
} else if (pid > 0) { | ||
// parent process | ||
// sleep a bit to wait for child process | ||
while (*count == 0) { | ||
usleep(100); | ||
} | ||
// try to open db for writing, this should fail | ||
systemConfig->access_mode = AccessMode::READ_WRITE; | ||
EXPECT_ANY_THROW(createDBAndConn()); | ||
// kill the child | ||
if (kill(pid, SIGKILL) != 0) { | ||
FAIL(); | ||
} | ||
} | ||
} | ||
|
||
} // namespace testing | ||
} // namespace kuzu |