2010-06-21 21:23:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim set:sw=2 ts=2 et lcs=trail\:.,tab\:>~ : */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2010-06-21 21:23:00 +00:00
|
|
|
|
|
|
|
#include "storage_test_harness.h"
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2010-06-21 21:23:00 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file tests that our implementation around sqlite3_unlock_notify works
|
|
|
|
* as expected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Helpers
|
|
|
|
|
|
|
|
enum State {
|
|
|
|
STARTING,
|
|
|
|
WRITE_LOCK,
|
|
|
|
READ_LOCK,
|
|
|
|
TEST_DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
class DatabaseLocker : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-09-03 22:25:57 +00:00
|
|
|
explicit DatabaseLocker(const char* aSQL)
|
2010-06-21 21:23:00 +00:00
|
|
|
: monitor("DatabaseLocker::monitor")
|
|
|
|
, mSQL(aSQL)
|
|
|
|
, mState(STARTING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunInBackground()
|
|
|
|
{
|
|
|
|
(void)NS_NewThread(getter_AddRefs(mThread));
|
|
|
|
do_check_true(mThread);
|
|
|
|
|
|
|
|
do_check_success(mThread->Dispatch(this, NS_DISPATCH_NORMAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitorAutoEnter lock(monitor);
|
2010-06-21 21:23:00 +00:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getDatabase());
|
|
|
|
|
|
|
|
nsCString sql(mSQL);
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
do_check_success(db->CreateStatement(sql, getter_AddRefs(stmt)));
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasResult;
|
2010-06-21 21:23:00 +00:00
|
|
|
do_check_success(stmt->ExecuteStep(&hasResult));
|
|
|
|
|
|
|
|
Notify(WRITE_LOCK);
|
|
|
|
WaitFor(TEST_DONE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitFor(State aState)
|
|
|
|
{
|
|
|
|
monitor.AssertCurrentThreadIn();
|
|
|
|
while (mState != aState) {
|
|
|
|
do_check_success(monitor.Wait());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notify(State aState)
|
|
|
|
{
|
|
|
|
monitor.AssertCurrentThreadIn();
|
|
|
|
mState = aState;
|
|
|
|
do_check_success(monitor.Notify());
|
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitor monitor;
|
2010-06-21 21:23:00 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
const char *const mSQL;
|
|
|
|
State mState;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DatabaseTester : public DatabaseLocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DatabaseTester(mozIStorageConnection *aConnection,
|
|
|
|
const char* aSQL)
|
|
|
|
: DatabaseLocker(aSQL)
|
|
|
|
, mConnection(aConnection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitorAutoEnter lock(monitor);
|
2010-06-21 21:23:00 +00:00
|
|
|
WaitFor(READ_LOCK);
|
|
|
|
|
|
|
|
nsCString sql(mSQL);
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
do_check_success(mConnection->CreateStatement(sql, getter_AddRefs(stmt)));
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasResult;
|
2010-06-21 21:23:00 +00:00
|
|
|
nsresult rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);
|
|
|
|
|
|
|
|
// Finalize our statement and null out our connection before notifying to
|
|
|
|
// ensure that we close on the proper thread.
|
|
|
|
rv = stmt->Finalize();
|
|
|
|
do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);
|
2012-07-30 14:20:58 +00:00
|
|
|
mConnection = nullptr;
|
2010-06-21 21:23:00 +00:00
|
|
|
|
|
|
|
Notify(TEST_DONE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageConnection> mConnection;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Test Functions
|
|
|
|
|
|
|
|
void
|
|
|
|
setup()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getDatabase());
|
|
|
|
|
|
|
|
// Create and populate a dummy table.
|
|
|
|
nsresult rv = db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY, data STRING)"
|
|
|
|
));
|
|
|
|
do_check_success(rv);
|
|
|
|
rv = db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO test (data) VALUES ('foo')"
|
|
|
|
));
|
|
|
|
do_check_success(rv);
|
|
|
|
rv = db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO test (data) VALUES ('bar')"
|
|
|
|
));
|
|
|
|
do_check_success(rv);
|
|
|
|
rv = db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE UNIQUE INDEX unique_data ON test (data)"
|
|
|
|
));
|
|
|
|
do_check_success(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_step_locked_does_not_block_main_thread()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getDatabase());
|
|
|
|
|
|
|
|
// Need to prepare our statement ahead of time so we make sure to only test
|
|
|
|
// step and not prepare.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = db->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO test (data) VALUES ('test1')"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
do_check_success(rv);
|
|
|
|
|
|
|
|
nsRefPtr<DatabaseLocker> locker(new DatabaseLocker("SELECT * FROM test"));
|
|
|
|
do_check_true(locker);
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitorAutoEnter lock(locker->monitor);
|
2010-06-21 21:23:00 +00:00
|
|
|
locker->RunInBackground();
|
|
|
|
|
|
|
|
// Wait for the locker to notify us that it has locked the database properly.
|
|
|
|
locker->WaitFor(WRITE_LOCK);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasResult;
|
2010-06-21 21:23:00 +00:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);
|
|
|
|
|
|
|
|
locker->Notify(TEST_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_drop_index_does_not_loop()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getDatabase());
|
|
|
|
|
|
|
|
// Need to prepare our statement ahead of time so we make sure to only test
|
|
|
|
// step and not prepare.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = db->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT * FROM test"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
do_check_success(rv);
|
|
|
|
|
|
|
|
nsRefPtr<DatabaseTester> tester =
|
|
|
|
new DatabaseTester(db, "DROP INDEX unique_data");
|
|
|
|
do_check_true(tester);
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitorAutoEnter lock(tester->monitor);
|
2010-06-21 21:23:00 +00:00
|
|
|
tester->RunInBackground();
|
|
|
|
|
|
|
|
// Hold a read lock on the database, and then let the tester try to execute.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasResult;
|
2010-06-21 21:23:00 +00:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
do_check_success(rv);
|
|
|
|
do_check_true(hasResult);
|
|
|
|
tester->Notify(READ_LOCK);
|
|
|
|
|
|
|
|
// Make sure the tester finishes its test before we move on.
|
|
|
|
tester->WaitFor(TEST_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_drop_table_does_not_loop()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getDatabase());
|
|
|
|
|
|
|
|
// Need to prepare our statement ahead of time so we make sure to only test
|
|
|
|
// step and not prepare.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = db->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT * FROM test"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
do_check_success(rv);
|
|
|
|
|
|
|
|
nsRefPtr<DatabaseTester> tester(new DatabaseTester(db, "DROP TABLE test"));
|
|
|
|
do_check_true(tester);
|
2011-04-29 19:21:57 +00:00
|
|
|
mozilla::ReentrantMonitorAutoEnter lock(tester->monitor);
|
2010-06-21 21:23:00 +00:00
|
|
|
tester->RunInBackground();
|
|
|
|
|
|
|
|
// Hold a read lock on the database, and then let the tester try to execute.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasResult;
|
2010-06-21 21:23:00 +00:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
do_check_success(rv);
|
|
|
|
do_check_true(hasResult);
|
|
|
|
tester->Notify(READ_LOCK);
|
|
|
|
|
|
|
|
// Make sure the tester finishes its test before we move on.
|
|
|
|
tester->WaitFor(TEST_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void (*gTests[])(void) = {
|
|
|
|
setup,
|
|
|
|
test_step_locked_does_not_block_main_thread,
|
|
|
|
test_drop_index_does_not_loop,
|
|
|
|
test_drop_table_does_not_loop,
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *file = __FILE__;
|
|
|
|
#define TEST_NAME "sqlite3_unlock_notify"
|
|
|
|
#define TEST_FILE file
|
|
|
|
#include "storage_test_harness_tail.h"
|