gecko-dev/storage/test/gtest/test_unlock_notify.cpp

265 строки
6.5 KiB
C++
Исходник Обычный вид История

/* -*- 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 15:12:37 +04: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/. */
#include "storage_test_harness.h"
#include "mozilla/ReentrantMonitor.h"
#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 mozilla::Runnable
{
public:
explicit DatabaseLocker(const char* aSQL)
: mozilla::Runnable("DatabaseLocker")
, monitor("DatabaseLocker::monitor")
, mSQL(aSQL)
, mState(STARTING)
{
}
void RunInBackground()
{
(void)NS_NewNamedThread("DatabaseLocker", getter_AddRefs(mThread));
do_check_true(mThread);
do_check_success(mThread->Dispatch(this, NS_DISPATCH_NORMAL));
}
void Shutdown()
{
if (mThread) {
mThread->Shutdown();
}
}
NS_IMETHOD Run() override
{
mozilla::ReentrantMonitorAutoEnter lock(monitor);
nsCOMPtr<mozIStorageConnection> db(getDatabase());
nsCString sql(mSQL);
nsCOMPtr<mozIStorageStatement> stmt;
do_check_success(db->CreateStatement(sql, getter_AddRefs(stmt)));
bool hasResult;
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());
}
mozilla::ReentrantMonitor monitor;
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() override
{
mozilla::ReentrantMonitorAutoEnter lock(monitor);
WaitFor(READ_LOCK);
nsCString sql(mSQL);
nsCOMPtr<mozIStorageStatement> stmt;
do_check_success(mConnection->CreateStatement(sql, getter_AddRefs(stmt)));
bool hasResult;
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);
mConnection = nullptr;
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);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<DatabaseLocker> locker(new DatabaseLocker("SELECT * FROM test"));
do_check_true(locker);
{
mozilla::ReentrantMonitorAutoEnter lock(locker->monitor);
locker->RunInBackground();
// Wait for the locker to notify us that it has locked the database properly.
locker->WaitFor(WRITE_LOCK);
bool hasResult;
rv = stmt->ExecuteStep(&hasResult);
do_check_eq(rv, NS_ERROR_FILE_IS_LOCKED);
locker->Notify(TEST_DONE);
}
locker->Shutdown();
}
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);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<DatabaseTester> tester =
new DatabaseTester(db, "DROP INDEX unique_data");
do_check_true(tester);
{
mozilla::ReentrantMonitorAutoEnter lock(tester->monitor);
tester->RunInBackground();
// Hold a read lock on the database, and then let the tester try to execute.
bool hasResult;
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);
}
tester->Shutdown();
}
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);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<DatabaseTester> tester(new DatabaseTester(db, "DROP TABLE test"));
do_check_true(tester);
{
mozilla::ReentrantMonitorAutoEnter lock(tester->monitor);
tester->RunInBackground();
// Hold a read lock on the database, and then let the tester try to execute.
bool hasResult;
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);
}
tester->Shutdown();
}
Bug 1315138 - gtestify storage/test/*.cpp. r=mak,erahm. This change is mostly straightforward, except for the following. - It removes all the printing from the do_check_* macros because gtest macros do appropriate printing. - test_StatementCache.cpp needs some special gtest magic for the type parameterization. - It merges the four tests in test_unlock_notify.cpp because they rely on being executed in order, and so aren't independent. - storage_test_harness_tail.h is no longer necessary because gtest provides the test looping functionality. - It uses #include and the preprocessor to remove the duplication between test_deadlock_detector.cpp and xpcom/tests/DeadlockDetector.cpp. - It makes the test in test_service_init_background_thread.cpp a death test to force it to be the first storage gtest, because it fails otherwise. - It adds code to undo the SQLite mutex hooking as necessary, so that tests don't interfere with each other. - It de-virtualizes Spinner's destructor (as identified in bug 1318282). --HG-- rename : storage/test/storage_test_harness.h => storage/test/gtest/storage_test_harness.h rename : storage/test/test_AsXXX_helpers.cpp => storage/test/gtest/test_AsXXX_helpers.cpp rename : storage/test/test_StatementCache.cpp => storage/test/gtest/test_StatementCache.cpp rename : storage/test/test_asyncStatementExecution_transaction.cpp => storage/test/gtest/test_asyncStatementExecution_transaction.cpp rename : storage/test/test_async_callbacks_with_spun_event_loops.cpp => storage/test/gtest/test_async_callbacks_with_spun_event_loops.cpp rename : storage/test/test_binding_params.cpp => storage/test/gtest/test_binding_params.cpp rename : storage/test/test_deadlock_detector.cpp => storage/test/gtest/test_deadlock_detector.cpp rename : storage/test/test_file_perms.cpp => storage/test/gtest/test_file_perms.cpp rename : storage/test/test_mutex.cpp => storage/test/gtest/test_mutex.cpp rename : storage/test/test_service_init_background_thread.cpp => storage/test/gtest/test_service_init_background_thread.cpp rename : storage/test/test_statement_scoper.cpp => storage/test/gtest/test_statement_scoper.cpp rename : storage/test/test_transaction_helper.cpp => storage/test/gtest/test_transaction_helper.cpp rename : storage/test/test_true_async.cpp => storage/test/gtest/test_true_async.cpp rename : storage/test/test_unlock_notify.cpp => storage/test/gtest/test_unlock_notify.cpp extra : rebase_source : dbb695c112564efa1945116be1a8435988982e74
2016-11-11 01:59:23 +03:00
TEST(storage_unlock_notify, Test)
{
// These must execute in order.
setup();
test_step_locked_does_not_block_main_thread();
test_drop_index_does_not_loop();
test_drop_table_does_not_loop();
}