1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28#
29# test_txn03.py
30#   Transactions: using multiple cursor and session handles
31#
32
33import wiredtiger, wttest
34from wtscenario import make_scenarios
35
36class test_txn03(wttest.WiredTigerTestCase):
37    tablename = 'test_txn03'
38    uri1 = 'table:' + tablename + "_1"
39    uri2 = 'table:' + tablename + "_2"
40    key_str = "TEST_KEY1"
41    data_str1 = "VAL"
42    data_str2 = "TEST_VAL1"
43
44    nentries = 1000
45    scenarios = make_scenarios([
46        ('var', dict(create_params = "key_format=S,value_format=S")),
47    ])
48
49    def test_ops(self):
50        self.session.create(self.uri1, self.create_params)
51        self.session.create(self.uri2, self.create_params)
52        # Set up the table with entries for 1 and 10
53        # We use the overwrite config so insert can update as needed.
54        c = self.session.open_cursor(self.uri1, None, 'overwrite')
55        c[self.key_str] = self.data_str1
56        c.close()
57        c = self.session.open_cursor(self.uri2, None, 'overwrite')
58        c[self.key_str] = self.data_str1
59        c.close()
60
61        # Update the first table - this update should be visible in the
62        # new session.
63        self.session.begin_transaction()
64        c = self.session.open_cursor(self.uri1, None, 'overwrite')
65        c[self.key_str] = self.data_str2
66        self.session.commit_transaction()
67        c.close()
68
69        # Open another session and some transactional cursors.
70        self.session2 = self.conn.open_session()
71        self.session2.begin_transaction("isolation=snapshot")
72        t1c = self.session2.open_cursor(self.uri1, None, 'overwrite')
73        t2c = self.session2.open_cursor(self.uri2, None, 'overwrite')
74
75        # Make an update in the first session.
76        self.session.begin_transaction()
77        c = self.session.open_cursor(self.uri2, None, 'overwrite')
78        c[self.key_str] = self.data_str2
79        self.session.commit_transaction()
80        c.close()
81
82        t1c.set_key(self.key_str)
83        t1c.search()
84        t2c.set_key(self.key_str)
85        t2c.search()
86        self.assertEqual(t1c.get_value(), self.data_str2)
87        self.assertEqual(t2c.get_value(), self.data_str1)
88
89        # Clean up
90        t1c.close()
91        t2c.close()
92        self.session2.rollback_transaction()
93
94if __name__ == '__main__':
95    wttest.run()
96