1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2006,2008 Oracle.  All rights reserved.
4#
5# $Id: test120.tcl,v 12.7 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	test120
8# TEST	Test of multi-version concurrency control.
9# TEST
10# TEST	Test basic functionality: a snapshot transaction started
11# TEST	before a regular transaction's put can't see the modification.
12# TEST	A snapshot transaction started after the put can see it.
13
14proc test120 { method {tnum "120"} args } {
15	source ./include.tcl
16
17	# This test needs its own env.
18	set eindex [lsearch -exact $args "-env"]
19	if { $eindex != -1 } {
20		incr eindex
21		set env [lindex $args $eindex]
22		puts "Test$tnum skipping for env $env"
23		return
24	}
25
26	# MVCC is not allowed with queue methods.
27	if { [is_queue $method] == 1 } {
28		puts "Test$tnum skipping for method $method"
29		return
30	}
31
32	puts "\tTest$tnum ($method): MVCC and blocking."
33
34	set args [convert_args $method $args]
35	set omethod [convert_method $method]
36	set encargs ""
37	set args [split_encargs $args encargs]
38	set filename "test.db"
39
40	# Create transactional env.  Specifying -multiversion makes
41	# all databases opened within the env -multiversion.
42	env_cleanup $testdir
43	puts "\tTest$tnum.a: Creating txn env."
44	set env [eval {berkdb_env}\
45	    -create -txn -multiversion $encargs -home $testdir]
46	error_check_good env_open [is_valid_env $env] TRUE
47
48	# Open database.
49	puts "\tTest$tnum.b: Creating -multiversion db."
50	set db [eval {berkdb_open} \
51	    -create -auto_commit -env $env $omethod $args $filename]
52	error_check_good db_open [is_valid_db $db] TRUE
53
54	puts "\tTest$tnum.c: Start transactions."
55	# Start two transactions.  T1 is the writer, so it's a regular
56	# transaction.  T2 is the reader and uses -snapshot.
57	set t1 [$env txn]
58	set txn1 "-txn $t1"
59	set t2 [$env txn -snapshot]
60	set txn2 "-txn $t2"
61
62	# Enter some data using txn1.
63	set key 1
64	set data DATA
65	error_check_good \
66	    t1_put [eval {$db put} $txn1 $key [chop_data $method $data]] 0
67
68	# Txn2 cannot see txn1's put, but it does not block.
69	puts "\tTest$tnum.d: Txn2 can't see txn1's put."
70	set ret [eval {$db get} $txn2 $key]
71	error_check_good txn2_get [llength $ret] 0
72
73	# Commit txn1.  Txn2 get still can't see txn1's put.
74	error_check_good t1_commit [$t1 commit] 0
75	set ret [eval {$db get} $txn2 $key]
76	error_check_good txn2_get [llength $ret] 0
77	error_check_good db_sync [$db sync] 0
78	set ret [eval {$db get} $txn2 $key]
79	error_check_good txn2_get [llength $ret] 0
80
81	# Start a new txn with -snapshot.  It can see the put.
82	puts "\tTest$tnum.e: A new txn can see txn1's put."
83	set t3 [$env txn -snapshot]
84	set txn3 "-txn $t3"
85	set ret [eval {$db get} $txn3 $key]
86	error_check_good \
87	    t3_get $ret [list [list $key [pad_data $method $data]]]
88
89	# Commit txns.
90	error_check_good t2_commit [$t2 commit] 0
91	error_check_good t3_commit [$t3 commit] 0
92
93	# Clean up.
94	error_check_good db_close [$db close] 0
95	error_check_good env_close [$env close] 0
96}
97