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_durability01.py
30#   Durability: make sure the metadata is stable after exclusive operations
31#   cause files to be closed.
32#
33
34import fnmatch, os, shutil, time
35from helper import copy_wiredtiger_home
36from suite_subprocess import suite_subprocess
37import wttest
38
39class test_durability01(wttest.WiredTigerTestCase, suite_subprocess):
40    uri = 'table:test_durability01'
41    create_params = 'key_format=i,value_format=i'
42
43    def check_crash_restart(self, olddir, newdir):
44        ''' Simulate a crash from olddir and restart in newdir. '''
45        # with the connection still open, copy files to new directory
46        copy_wiredtiger_home(olddir, newdir)
47
48        # Open the new directory
49        conn = self.setUpConnectionOpen(newdir)
50        session = self.setUpSessionOpen(conn)
51        session.verify(self.uri)
52        conn.close()
53
54    def test_durability(self):
55        '''Check for missing metadata checkpoints'''
56
57        # Here's the strategy:
58        #    - update the table
59        #    - verify, which causes the table to be flushed
60        #    - copy the database directory (live, simulating a crash)
61        #    - verify in the copy
62        #    - repeat
63        #
64        # If the metadata isn't flushed, eventually the metadata we copy will
65        # be sufficiently out-of-sync with the data file that it won't verify.
66        self.session.create(self.uri, self.create_params)
67        for i in range(100):
68            c = self.session.open_cursor(self.uri)
69            c[i] = i
70            c.close()
71            if i % 5 == 0:
72                self.session.checkpoint()
73            else:
74                self.session.verify(self.uri)
75            self.check_crash_restart(".", "RESTART")
76
77if __name__ == '__main__':
78    wttest.run()
79