summaryrefslogtreecommitdiff
path: root/odb-tests/common/transaction
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2024-01-25 20:32:06 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2024-01-25 20:32:06 +0300
commit0d49ea1fe08cf1eab41a00149393a291c65a59d7 (patch)
tree0391eb09309ca95282e200516937e64d89f3e1bb /odb-tests/common/transaction
parentfc3fb39c90ab7fe5fccbe3f3bc0eb2645157bb96 (diff)
Turn odb-tests repository into package for muti-package repositoryodb-tests
Diffstat (limited to 'odb-tests/common/transaction')
-rw-r--r--odb-tests/common/transaction/basics/buildfile13
-rw-r--r--odb-tests/common/transaction/basics/driver.cxx151
-rw-r--r--odb-tests/common/transaction/basics/testscript62
-rw-r--r--odb-tests/common/transaction/callback/buildfile13
-rw-r--r--odb-tests/common/transaction/callback/driver.cxx231
-rw-r--r--odb-tests/common/transaction/callback/testscript72
6 files changed, 542 insertions, 0 deletions
diff --git a/odb-tests/common/transaction/basics/buildfile b/odb-tests/common/transaction/basics/buildfile
new file mode 100644
index 0000000..f412235
--- /dev/null
+++ b/odb-tests/common/transaction/basics/buildfile
@@ -0,0 +1,13 @@
+# file : common/transaction/basics/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libs = libodb%lib{odb}
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{*} $libs testscript
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../../alias{database-client}: include = adhoc
diff --git a/odb-tests/common/transaction/basics/driver.cxx b/odb-tests/common/transaction/basics/driver.cxx
new file mode 100644
index 0000000..1833555
--- /dev/null
+++ b/odb-tests/common/transaction/basics/driver.cxx
@@ -0,0 +1,151 @@
+// file : common/transaction/basics/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test basic transaction operations.
+//
+
+#include <string>
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/tracer.hxx>
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+#include <odb/statement.hxx>
+#include <odb/exceptions.hxx>
+
+#include <libcommon/common.hxx>
+#include <libcommon/concrete.hxx>
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+struct transaction_tracer: odb::tracer
+{
+ virtual void
+ execute (connection&, const char* s)
+ {
+ string str (s);
+
+ if (str == "BEGIN")
+ cout << "begin transaction" << endl;
+ else if (str == "COMMIT")
+ cout << "commit transaction" << endl;
+ else if (str == "ROLLBACK")
+ cout << "rollback transaction" << endl;
+ }
+
+ // Override the other version to get rid of a Sun CC warning.
+ //
+ virtual void
+ execute (connection& c, const statement& s)
+ {
+ execute (c, s.text ());
+ }
+};
+
+int
+main (int argc, char* argv[])
+{
+ {
+ transaction_tracer tracer;
+ unique_ptr<database> db (create_database (argc, argv, false));
+ db->tracer (tracer);
+
+ assert (!transaction::has_current ());
+
+ // Current and db accessors.
+ //
+ cout << "test 001" << endl;
+ {
+ transaction t (db->begin ());
+ assert (&t.database () == db.get ());
+ assert (transaction::has_current ());
+ assert (&transaction::current () == &t);
+
+ transaction::reset_current ();
+ assert (!transaction::has_current ());
+
+ transaction t2 (db->begin (), false);
+ assert (!transaction::has_current ());
+
+ transaction::current (t2);
+ assert (&transaction::current () == &t2);
+ }
+
+ // Commit.
+ //
+ cout << "test 002" << endl;
+ {
+ transaction t (db->begin ());
+ t.commit ();
+ }
+
+ // Rollback.
+ //
+ cout << "test 003" << endl;
+ {
+ transaction t (db->begin ());
+ t.rollback ();
+ }
+
+ // Auto rollback.
+ //
+ cout << "test 004" << endl;
+ {
+ transaction t (db->begin ());
+ }
+
+ // Nested transaction.
+ //
+ cout << "test 005" << endl;
+ {
+ transaction t (db->begin ());
+
+ try
+ {
+ transaction n (db->begin ());
+ }
+ catch (const already_in_transaction&)
+ {
+ cout << "already_in_transaction" << endl;
+ }
+ }
+
+ // Concrete transaction type.
+ //
+ cout << "test 006" << endl;
+ {
+ assert (sizeof (odb_db::transaction) == sizeof (transaction));
+
+ odb_db::transaction t (static_cast<odb_db::database&> (*db).begin ());
+ odb_db::transaction& r (odb_db::transaction::current ());
+ assert (&t == &r);
+ }
+
+ // Transaction restart.
+ //
+ cout << "test 007" << endl;
+ {
+ transaction t (db->begin ());
+ t.commit ();
+ t.reset (db->begin ());
+ t.commit ();
+ }
+ }
+
+ // Test early connection release.
+ //
+ {
+ unique_ptr<database> db (create_database (argc, argv, false, 1));
+ transaction t1 (db->begin ());
+ t1.commit ();
+ transaction t2 (db->begin ());
+ t2.rollback ();
+ transaction t3 (db->begin ());
+ t3.commit ();
+ }
+}
diff --git a/odb-tests/common/transaction/basics/testscript b/odb-tests/common/transaction/basics/testscript
new file mode 100644
index 0000000..94c58b6
--- /dev/null
+++ b/odb-tests/common/transaction/basics/testscript
@@ -0,0 +1,62 @@
+# file : common/transaction/basics/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../../database-options.testscript
+
++cat <<EOI >=output
+ test 001
+ begin transaction
+ begin transaction
+ rollback transaction
+ rollback transaction
+ test 002
+ begin transaction
+ commit transaction
+ test 003
+ begin transaction
+ rollback transaction
+ test 004
+ begin transaction
+ rollback transaction
+ test 005
+ begin transaction
+ already_in_transaction
+ rollback transaction
+ test 006
+ begin transaction
+ rollback transaction
+ test 007
+ begin transaction
+ commit transaction
+ begin transaction
+ commit transaction
+ EOI
+
+test.redirects += >>>../output
+
+: mysql
+:
+if $mysql
+{
+ .include ../../../mysql.testscript
+
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../../pgsql.testscript
+
+ $*
+}
diff --git a/odb-tests/common/transaction/callback/buildfile b/odb-tests/common/transaction/callback/buildfile
new file mode 100644
index 0000000..78b1b03
--- /dev/null
+++ b/odb-tests/common/transaction/callback/buildfile
@@ -0,0 +1,13 @@
+# file : common/transaction/callback/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libs = libodb%lib{odb}
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{*} $libs testscript
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../../alias{database-client}: include = adhoc
diff --git a/odb-tests/common/transaction/callback/driver.cxx b/odb-tests/common/transaction/callback/driver.cxx
new file mode 100644
index 0000000..d0af993
--- /dev/null
+++ b/odb-tests/common/transaction/callback/driver.cxx
@@ -0,0 +1,231 @@
+// file : common/transaction/callback/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test transaction callbacks.
+//
+
+#include <memory> // std::unique_ptr
+#include <cstddef> // std::size_t
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+struct callback
+{
+ callback (unsigned short v): v_ (v), t_ (0) {}
+ callback (unsigned short v, transaction& t): v_ (v), t_ (0) {register_ (t);}
+ ~callback () {if (t_ != 0) unregister ();}
+
+ void
+ register_ (transaction& t)
+ {
+ t_ = &t;
+ t.callback_register (&func, this, transaction::event_all, v_, &t_);
+ }
+
+ void
+ unregister ()
+ {
+ cout << " unregister callback " << v_ << endl;
+ t_->callback_unregister (this);
+ t_ = 0;
+ }
+
+ void
+ update (unsigned short v)
+ {
+ v_ = v;
+ t_->callback_update (this, transaction::event_all, v_, &t_);
+ }
+
+private:
+ static void
+ func (unsigned short event, void* key, unsigned long long data)
+ {
+ callback& c (*static_cast<callback*> (key));
+
+ const char* en;
+ switch (event)
+ {
+ case transaction::event_commit:
+ en = "commit";
+ break;
+ case transaction::event_rollback:
+ en = "rollback";
+ break;
+ default:
+ en = "unknown";
+ }
+
+ cout << " callback " << c.v_ << " " << en << endl;
+
+ assert (data == c.v_);
+ assert (c.t_ == 0);
+ }
+
+ unsigned short v_;
+ transaction* t_;
+};
+
+struct failed {};
+
+static void
+throw_func (unsigned short, void*, unsigned long long)
+{
+ throw failed ();
+}
+
+static void
+dummy_func (unsigned short, void* key, unsigned long long data)
+{
+ assert (reinterpret_cast<unsigned long long> (key) == data);
+}
+
+static void
+fill (transaction& t)
+{
+ // 20 is from odb/transaction.hxx.
+ //
+ for (size_t i (0); i < 20; ++i)
+ t.callback_register (&dummy_func,
+ reinterpret_cast<void*> (i),
+ transaction::event_all,
+ i);
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv, false));
+
+ // We want to test both stack and dynamic slots.
+ //
+ for (unsigned short i (1); i < 3; ++i)
+ {
+ // Test basic logic.
+ //
+ cout << "test " << i << "/001" << endl;
+
+ // Commit callback.
+ //
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ t.commit ();
+ }
+
+ // Rollback callback.
+ //
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ t.rollback ();
+ }
+
+ // Rollback via exception callback.
+ //
+ {
+ callback c1 (1);
+
+ try
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ c1.register_ (t);
+ throw failed ();
+ }
+ catch (const failed&)
+ {
+ }
+ }
+
+ // Unregister callback at the end.
+ //
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ c1.unregister ();
+ t.callback_unregister (&c1); // Test unregistering non-registered key.
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ c1.unregister ();
+ callback c2 (2, t);
+ t.commit ();
+ }
+
+ // Unregister callback in the middle.
+ //
+ cout << "test " << i << "/002" << endl;
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ callback c2 (2, t);
+ callback c3 (3, t);
+ c2.unregister ();
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ callback c2 (2, t);
+ callback c3 (3, t);
+ c2.unregister ();
+ callback c4 (4, t); // Using the free slot.
+ t.commit ();
+ }
+
+ // Test a callback in the middle that throws.
+ //
+ cout << "test " << i << "/003" << endl;
+ try
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c1 (1, t);
+ t.callback_register (&throw_func, 0);
+ callback c2 (2, t);
+ t.commit ();
+ }
+ catch (const failed&)
+ {
+ }
+
+ // Test callback_update().
+ //
+ {
+ transaction t (db->begin ());
+ if (i == 2) fill (t);
+ callback c (1, t);
+ c.update (2);
+ t.commit ();
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/transaction/callback/testscript b/odb-tests/common/transaction/callback/testscript
new file mode 100644
index 0000000..7229ecd
--- /dev/null
+++ b/odb-tests/common/transaction/callback/testscript
@@ -0,0 +1,72 @@
+# file : common/transaction/callback/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../../database-options.testscript
+
++cat <<EOI >=output
+ test 1/001
+ callback 1 commit
+ callback 1 rollback
+ callback 1 rollback
+ unregister callback 1
+ unregister callback 1
+ callback 2 commit
+ test 1/002
+ unregister callback 2
+ callback 1 commit
+ callback 3 commit
+ unregister callback 2
+ callback 1 commit
+ callback 4 commit
+ callback 3 commit
+ test 1/003
+ callback 1 commit
+ callback 2 commit
+ test 2/001
+ callback 1 commit
+ callback 1 rollback
+ callback 1 rollback
+ unregister callback 1
+ unregister callback 1
+ callback 2 commit
+ test 2/002
+ unregister callback 2
+ callback 1 commit
+ callback 3 commit
+ unregister callback 2
+ callback 1 commit
+ callback 4 commit
+ callback 3 commit
+ test 2/003
+ callback 1 commit
+ callback 2 commit
+ EOI
+
+test.redirects += >>>../output
+
+: mysql
+:
+if $mysql
+{
+ .include ../../../mysql.testscript
+
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../../pgsql.testscript
+
+ $*
+}