Martin v. Löwis | 1c6b1a2 | 2002-11-19 17:47:07 +0000 | [diff] [blame^] | 1 | """ |
| 2 | TestCases for checking that it does not segfault when a DBEnv object |
| 3 | is closed before its DB objects. |
| 4 | """ |
| 5 | |
| 6 | import sys, os, string |
| 7 | from pprint import pprint |
| 8 | import tempfile |
| 9 | import glob |
| 10 | import unittest |
| 11 | |
| 12 | from bsddb import db |
| 13 | |
| 14 | from test.test_support import verbose |
| 15 | |
| 16 | |
| 17 | #---------------------------------------------------------------------- |
| 18 | |
| 19 | class DBEnvClosedEarlyCrash(unittest.TestCase): |
| 20 | def setUp(self): |
| 21 | self.homeDir = os.path.join(os.path.dirname(sys.argv[0]), 'db_home') |
| 22 | try: os.mkdir(self.homeDir) |
| 23 | except os.error: pass |
| 24 | tempfile.tempdir = self.homeDir |
| 25 | self.filename = os.path.split(tempfile.mktemp())[1] |
| 26 | tempfile.tempdir = None |
| 27 | |
| 28 | def tearDown(self): |
| 29 | files = glob.glob(os.path.join(self.homeDir, '*')) |
| 30 | for file in files: |
| 31 | os.remove(file) |
| 32 | |
| 33 | |
| 34 | def test01_close_dbenv_before_db(self): |
| 35 | dbenv = db.DBEnv() |
| 36 | dbenv.open(self.homeDir,db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, 0666) |
| 37 | |
| 38 | d = db.DB(dbenv) |
| 39 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) |
| 40 | |
| 41 | try: |
| 42 | dbenv.close() |
| 43 | except db.DBError: |
| 44 | try: |
| 45 | d.close() |
| 46 | except db.DBError: |
| 47 | return |
| 48 | assert 0, "DB close did not raise an exception about its DBEnv being trashed" |
| 49 | |
| 50 | assert 0, "dbenv did not raise an exception about its DB being open" |
| 51 | |
| 52 | |
| 53 | def test02_close_dbenv_delete_db_success(self): |
| 54 | dbenv = db.DBEnv() |
| 55 | dbenv.open(self.homeDir,db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, 0666) |
| 56 | |
| 57 | d = db.DB(dbenv) |
| 58 | d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) |
| 59 | |
| 60 | try: |
| 61 | dbenv.close() |
| 62 | except db.DBError: |
| 63 | pass # good, it should raise an exception |
| 64 | |
| 65 | # this should not raise an exception, it should silently skip |
| 66 | # the db->close() call as it can't be done safely. |
| 67 | del d |
| 68 | try: |
| 69 | import gc |
| 70 | except ImportError: |
| 71 | gc = None |
| 72 | if gc: |
| 73 | # force d.__del__ [DB_dealloc] to be called |
| 74 | gc.collect() |
| 75 | |
| 76 | |
| 77 | #---------------------------------------------------------------------- |
| 78 | |
| 79 | def suite(): |
| 80 | return unittest.makeSuite(DBEnvClosedEarlyCrash) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | unittest.main( defaultTest='suite' ) |
| 85 | |
| 86 | |