Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1 | """TestCases for checking that it does not segfault when a DBEnv object |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 2 | is closed before its DB objects. |
| 3 | """ |
| 4 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5 | import os |
| 6 | import sys |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 7 | import tempfile |
| 8 | import glob |
| 9 | import unittest |
| 10 | |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame^] | 11 | try: |
| 12 | # For Python 2.3 |
| 13 | from bsddb import db |
| 14 | except ImportError: |
| 15 | # For earlier Pythons w/distutils pybsddb |
| 16 | from bsddb3 import db |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 17 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 18 | from test_all import verbose |
| 19 | |
| 20 | # We're going to get warnings in this module about trying to close the db when |
| 21 | # its env is already closed. Let's just ignore those. |
| 22 | try: |
| 23 | import warnings |
| 24 | except ImportError: |
| 25 | pass |
| 26 | else: |
| 27 | warnings.filterwarnings('ignore', |
| 28 | message='DB could not be closed in', |
| 29 | category=RuntimeWarning) |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | #---------------------------------------------------------------------- |
| 33 | |
| 34 | class DBEnvClosedEarlyCrash(unittest.TestCase): |
| 35 | def setUp(self): |
| 36 | self.homeDir = os.path.join(os.path.dirname(sys.argv[0]), 'db_home') |
| 37 | try: os.mkdir(self.homeDir) |
| 38 | except os.error: pass |
| 39 | tempfile.tempdir = self.homeDir |
| 40 | self.filename = os.path.split(tempfile.mktemp())[1] |
| 41 | tempfile.tempdir = None |
| 42 | |
| 43 | def tearDown(self): |
| 44 | files = glob.glob(os.path.join(self.homeDir, '*')) |
| 45 | for file in files: |
| 46 | os.remove(file) |
| 47 | |
| 48 | |
| 49 | def test01_close_dbenv_before_db(self): |
| 50 | dbenv = db.DBEnv() |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 51 | dbenv.open(self.homeDir, |
| 52 | db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, |
| 53 | 0666) |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 54 | |
| 55 | d = db.DB(dbenv) |
| 56 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) |
| 57 | |
| 58 | try: |
| 59 | dbenv.close() |
| 60 | except db.DBError: |
| 61 | try: |
| 62 | d.close() |
| 63 | except db.DBError: |
| 64 | return |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 65 | assert 0, \ |
| 66 | "DB close did not raise an exception about its "\ |
| 67 | "DBEnv being trashed" |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 68 | |
Barry Warsaw | bc38baf | 2002-12-31 18:21:43 +0000 | [diff] [blame] | 69 | # XXX This may fail when using older versions of BerkeleyDB. |
| 70 | # E.g. 3.2.9 never raised the exception. |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 71 | assert 0, "dbenv did not raise an exception about its DB being open" |
| 72 | |
| 73 | |
| 74 | def test02_close_dbenv_delete_db_success(self): |
| 75 | dbenv = db.DBEnv() |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 76 | dbenv.open(self.homeDir, |
| 77 | db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, |
| 78 | 0666) |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 79 | |
| 80 | d = db.DB(dbenv) |
| 81 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) |
| 82 | |
| 83 | try: |
| 84 | dbenv.close() |
| 85 | except db.DBError: |
| 86 | pass # good, it should raise an exception |
| 87 | |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 88 | del d |
| 89 | try: |
| 90 | import gc |
| 91 | except ImportError: |
| 92 | gc = None |
| 93 | if gc: |
| 94 | # force d.__del__ [DB_dealloc] to be called |
| 95 | gc.collect() |
| 96 | |
| 97 | |
| 98 | #---------------------------------------------------------------------- |
| 99 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 100 | def test_suite(): |
| 101 | suite = unittest.TestSuite() |
| 102 | suite.addTest(unittest.makeSuite(DBEnvClosedEarlyCrash)) |
| 103 | return suite |
Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | if __name__ == '__main__': |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 107 | unittest.main(defaultTest='test_suite') |