Adding transactions further - settled on MVCC
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
index f5f567d..3915a00 100644 (file)
@@ -1,25 +1,45 @@
 use strict;
-use Test::More tests => 4;
+use Test::More tests => 13;
 use Test::Exception;
 use t::common qw( new_fh );
 
 use_ok( 'DBM::Deep' );
 
 my ($fh, $filename) = new_fh();
-my $db = DBM::Deep->new(
+my $db1 = DBM::Deep->new(
     file => $filename,
+    locking => 1,
 );
 
-$db->{x} = 'y';
-is( $db->{x}, 'y' );
-$db->begin_work;
-$db->{x} = 'z';
-is( $db->{x}, 'z' );
-$db->rollback;
-TODO: {
-    local $TODO = "Haven't written transaction code yet";
-    is( $db->{x}, 'y' );
-}
+my $db2 = DBM::Deep->new(
+    file => $filename,
+    locking => 1,
+);
+
+$db1->{x} = 'y';
+is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
+is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
+
+$db1->begin_work;
+
+is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
+is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
+
+$db1->{x} = 'z';
+is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
+is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
+
+$db2->{other_x} = 'foo';
+is( $db2->{other_x}, 'foo', "Set other_x within DB1's transaction, so DB2 can see it" );
+is( $db1->{other_x}, undef, "Since other_x was added after the transaction began, DB1 doesn't see it." );
+
+$db1->rollback;
+
+is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
+is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
+
+is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
+is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
 
 # Add a commit test (using fork) - we don't have to use fork initially. Since
 # the transaction is in the Engine object and each new() provides a new Engine
@@ -28,6 +48,12 @@ TODO: {
 # Should the transaction be in the Root and not the Engine? How would that
 # work?
 
+# What about the following:
+#   $db->{foo} = {};
+#   $db2 = $db->{foo};
+#   $db2->begin_work;
+#   $db->{foo} = 3;
+
 __END__
 
 Plan for transactions: