2009-05-13 20:53:57 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: 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/. */
|
2009-05-13 20:53:57 +04:00
|
|
|
|
|
|
|
#include "storage_test_harness.h"
|
|
|
|
|
|
|
|
#include "mozStorageHelper.h"
|
2014-09-10 14:46:14 +04:00
|
|
|
#include "mozStorageConnection.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::storage;
|
|
|
|
|
|
|
|
bool has_transaction(mozIStorageConnection* aDB) {
|
|
|
|
return !(static_cast<Connection *>(aDB)->getAutocommit());
|
|
|
|
}
|
2009-05-13 20:53:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This file test our Transaction helper in mozStorageHelper.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
test_Commit()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
|
|
|
|
|
|
|
|
// Create a table in a transaction, call Commit, and make sure that it does
|
|
|
|
// exists after the transaction falls out of scope.
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mozStorageTransaction transaction(db, false);
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_true(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY)"
|
|
|
|
));
|
|
|
|
(void)transaction.Commit();
|
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_false(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists = false;
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
|
|
|
|
do_check_true(exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_Rollback()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
|
|
|
|
|
|
|
|
// Create a table in a transaction, call Rollback, and make sure that it does
|
|
|
|
// not exists after the transaction falls out of scope.
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mozStorageTransaction transaction(db, true);
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_true(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY)"
|
|
|
|
));
|
|
|
|
(void)transaction.Rollback();
|
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_false(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists = true;
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
|
|
|
|
do_check_false(exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_AutoCommit()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
|
|
|
|
|
|
|
|
// Create a table in a transaction, and make sure that it exists after the
|
|
|
|
// transaction falls out of scope. This means the Commit was successful.
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mozStorageTransaction transaction(db, true);
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_true(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY)"
|
|
|
|
));
|
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_false(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists = false;
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
|
|
|
|
do_check_true(exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_AutoRollback()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
|
|
|
|
|
|
|
|
// Create a table in a transaction, and make sure that it does not exists
|
|
|
|
// after the transaction falls out of scope. This means the Rollback was
|
|
|
|
// successful.
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mozStorageTransaction transaction(db, false);
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_true(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY)"
|
|
|
|
));
|
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_false(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists = true;
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
|
|
|
|
do_check_false(exists);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-10 14:46:14 +04:00
|
|
|
test_null_database_connection()
|
|
|
|
{
|
|
|
|
// We permit the use of the Transaction helper when passing a null database
|
|
|
|
// in, so we need to make sure this still works without crashing.
|
|
|
|
mozStorageTransaction transaction(nullptr, false);
|
|
|
|
do_check_true(NS_SUCCEEDED(transaction.Commit()));
|
|
|
|
do_check_true(NS_SUCCEEDED(transaction.Rollback()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
test_async_Commit()
|
2009-05-13 20:53:57 +04:00
|
|
|
{
|
2014-09-10 14:46:14 +04:00
|
|
|
// note this will be active for any following test.
|
|
|
|
hook_sqlite_mutex();
|
|
|
|
|
2009-05-13 20:53:57 +04:00
|
|
|
nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
|
|
|
|
|
2014-09-10 14:46:14 +04:00
|
|
|
// -- wedge the thread
|
|
|
|
nsCOMPtr<nsIThread> target(get_conn_async_thread(db));
|
|
|
|
do_check_true(target);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ThreadWedger> wedger (new ThreadWedger(target));
|
2009-05-13 20:53:57 +04:00
|
|
|
|
|
|
|
{
|
2014-09-10 14:46:14 +04:00
|
|
|
mozStorageTransaction transaction(db, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_DEFERRED,
|
|
|
|
true);
|
|
|
|
do_check_true(has_transaction(db));
|
2009-05-13 20:53:57 +04:00
|
|
|
(void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2014-09-10 14:46:14 +04:00
|
|
|
"CREATE TABLE test (id INTEGER PRIMARY KEY)"
|
2009-05-13 20:53:57 +04:00
|
|
|
));
|
2014-09-10 14:46:14 +04:00
|
|
|
(void)transaction.Commit();
|
2009-05-13 20:53:57 +04:00
|
|
|
}
|
2014-09-10 14:46:14 +04:00
|
|
|
do_check_true(has_transaction(db));
|
|
|
|
|
|
|
|
// -- unwedge the async thread
|
|
|
|
wedger->unwedge();
|
|
|
|
|
|
|
|
// Ensure the transaction has done its job by enqueueing an async execution.
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> stmt;
|
|
|
|
(void)db->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT NULL"
|
|
|
|
), getter_AddRefs(stmt));
|
|
|
|
blocking_async_execute(stmt);
|
|
|
|
stmt->Finalize();
|
|
|
|
do_check_false(has_transaction(db));
|
|
|
|
bool exists = false;
|
|
|
|
(void)db->TableExists(NS_LITERAL_CSTRING("test"), &exists);
|
2009-05-13 20:53:57 +04:00
|
|
|
do_check_true(exists);
|
|
|
|
|
2014-09-10 14:46:14 +04:00
|
|
|
blocking_async_close(db);
|
2009-05-13 20:53:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void (*gTests[])(void) = {
|
|
|
|
test_Commit,
|
|
|
|
test_Rollback,
|
|
|
|
test_AutoCommit,
|
|
|
|
test_AutoRollback,
|
|
|
|
test_null_database_connection,
|
2014-09-10 14:46:14 +04:00
|
|
|
test_async_Commit,
|
2009-05-13 20:53:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *file = __FILE__;
|
|
|
|
#define TEST_NAME "transaction helper"
|
|
|
|
#define TEST_FILE file
|
|
|
|
#include "storage_test_harness_tail.h"
|