Ejemplo n.º 1
0
    def test01_basic_replication(self):
        master_port = test_support.find_unused_port()
        self.dbenvMaster.repmgr_set_local_site("127.0.0.1", master_port)
        client_port = test_support.find_unused_port()
        self.dbenvClient.repmgr_set_local_site("127.0.0.1", client_port)
        self.dbenvMaster.repmgr_add_remote_site("127.0.0.1", client_port)
        self.dbenvClient.repmgr_add_remote_site("127.0.0.1", master_port)
        self.dbenvMaster.rep_set_nsites(2)
        self.dbenvClient.rep_set_nsites(2)
        self.dbenvMaster.rep_set_priority(10)
        self.dbenvClient.rep_set_priority(0)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100123)
        self.dbenvClient.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100321)
        self.assertEquals(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100123)
        self.assertEquals(
            self.dbenvClient.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100321)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100234)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100432)
        self.assertEquals(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100234)
        self.assertEquals(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100432)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100345)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100543)
        self.assertEquals(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100345)
        self.assertEquals(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100543)

        self.dbenvMaster.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvClient.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)

        self.dbenvMaster.repmgr_start(1, db.DB_REP_MASTER)
        self.dbenvClient.repmgr_start(1, db.DB_REP_CLIENT)

        self.assertEquals(self.dbenvMaster.rep_get_nsites(), 2)
        self.assertEquals(self.dbenvClient.rep_get_nsites(), 2)
        self.assertEquals(self.dbenvMaster.rep_get_priority(), 10)
        self.assertEquals(self.dbenvClient.rep_get_priority(), 0)
        self.assertEquals(self.dbenvMaster.repmgr_get_ack_policy(),
                          db.DB_REPMGR_ACKS_ALL)
        self.assertEquals(self.dbenvClient.repmgr_get_ack_policy(),
                          db.DB_REPMGR_ACKS_ALL)

        # The timeout is necessary in BDB 4.5, since DB_EVENT_REP_STARTUPDONE
        # is not generated if the master has no new transactions.
        # This is solved in BDB 4.6 (#15542).
        import time
        timeout = time.time() + 60
        while (time.time() < timeout) and not (self.confirmed_master
                                               and self.client_startupdone):
            time.sleep(0.02)
        # this fails on Windows as self.client_startupdone never gets set
        # to True - see bug 3892.  BUT - even though this assertion
        # fails on Windows the rest of the test passes - so to prove
        # that we let the rest of the test run.  Sadly we can't
        # make use of raising TestSkipped() here (unittest still
        # reports it as an error), so we yell to stderr.
        import sys
        if sys.platform == "win32":
            print >> sys.stderr, \
                "XXX - windows bsddb replication fails on windows and is skipped"
            print >> sys.stderr, "XXX - Please see issue #3892"
        # It also fails irregularly on other platforms, and again the
        # rest of the tests pass.  Since bsddb support is unmaintained, and
        # is gone in py3k, we just emit a warning instead of a test failure
        # so as to improve buildbot stability.
        elif time.time() > timeout:
            print >> sys.stderr, \
                "XXX - timeout before startup confirmed, see issue #3892."

        d = self.dbenvMaster.repmgr_site_list()
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0][0], "127.0.0.1")
        self.assertEquals(d[0][1], client_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        d = self.dbenvClient.repmgr_site_list()
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0][0], "127.0.0.1")
        self.assertEquals(d[0][1], master_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        if db.version() >= (4, 6):
            d = self.dbenvMaster.repmgr_stat(flags=db.DB_STAT_CLEAR)
            self.assertTrue("msgs_queued" in d)

        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open("test", db.DB_HASH, db.DB_CREATE, 0666, txn=txn)
        txn.commit()

        import time, os.path
        timeout = time.time() + 10
        while (time.time()<timeout) and \
          not (os.path.exists(os.path.join(self.homeDirClient,"test"))) :
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open("test",
                                   db.DB_HASH,
                                   flags=db.DB_RDONLY,
                                   mode=0666,
                                   txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put("ABC", "123", txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while (time.time() < timeout) and (v == None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v == None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEquals("123", v)

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete("ABC", txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while (time.time() < timeout) and (v != None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v == None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEquals(None, v)
Ejemplo n.º 2
0
    def test02_WithSource(self):
        """
        A Recno file that is given a "backing source file" is essentially a
        simple ASCII file.  Normally each record is delimited by \n and so is
        just a line in the file, but you can set a different record delimiter
        if needed.
        """
        homeDir = get_new_environment_path()
        self.homeDir = homeDir
        source = os.path.join(homeDir, 'test_recno.txt')
        if not os.path.isdir(homeDir):
            os.mkdir(homeDir)
        f = open(source, 'w') # create the file
        f.close()

        d = db.DB()
        # This is the default value, just checking if both int
        d.set_re_delim(0x0A)
        d.set_re_delim('\n')  # and char can be used...
        d.set_re_source(source)
        d.open(self.filename, db.DB_RECNO, db.DB_CREATE)

        data = "The quick brown fox jumped over the lazy dog".split()
        for datum in data:
            d.append(datum)
        d.sync()
        d.close()

        # get the text from the backing source
        f = open(source, 'r')
        text = f.read()
        f.close()
        text = text.strip()
        if verbose:
            print text
            print data
            print text.split('\n')

        self.assertEqual(text.split('\n'), data)

        # open as a DB again
        d = db.DB()
        d.set_re_source(source)
        d.open(self.filename, db.DB_RECNO)

        d[3] = 'reddish-brown'
        d[8] = 'comatose'

        d.sync()
        d.close()

        f = open(source, 'r')
        text = f.read()
        f.close()
        text = text.strip()
        if verbose:
            print text
            print text.split('\n')

        self.assertEqual(text.split('\n'),
           "The quick reddish-brown fox jumped over the comatose dog".split())
Ejemplo n.º 3
0
 def setUp(self):
     self.path = get_new_database_path()
     self.db = db.DB()
Ejemplo n.º 4
0
 def test07_verify(self):
     self.d.close()
     d = db.DB(self.env)
     d.verify(self.filename)
Ejemplo n.º 5
0
 def test_set_cachesize_dbenv_db(self):
     # You can not configure the cachesize using
     # the database handle, if you are using an environment.
     d = db.DB(self.env)
     self.assertRaises(db.DBInvalidArgError, d.set_cachesize, 0,
                       1024 * 1024, 1)
Ejemplo n.º 6
0
 def setUp(self) :
     self.obj = db.DB()
 def test_set_cachesize_dbenv_db(self):
     d = db.DB(self.env)
     self.assertRaises(db.DBInvalidArgError, d.set_cachesize, 0,
                       1048576, 1)
Ejemplo n.º 8
0
 def setUp(self):
     DBEnv.setUp(self)
     self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL | db.DB_INIT_LOG)
     self.db = db.DB(self.env)
     self.db.open("test", db.DB_HASH, db.DB_CREATE, 0660)
Ejemplo n.º 9
0
    def test01_basic_replication(self):
        self.basic_rep_threading()
        import time
        timeout = time.time() + 60
        while time.time() < timeout and not (self.confirmed_master and self.client_startupdone):
            time.sleep(0.02)

        self.assertTrue(time.time() < timeout)
        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open('test', db.DB_HASH, db.DB_CREATE, 438, txn=txn)
        txn.commit()
        import time, os.path
        timeout = time.time() + 10
        while time.time() < timeout and not os.path.exists(os.path.join(self.homeDirClient, 'test')):
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open('test', db.DB_HASH, flags=db.DB_RDONLY, mode=438, txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        d = self.dbenvMaster.rep_stat(flags=db.DB_STAT_CLEAR)
        self.assertTrue('master_changes' in d)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put('ABC', '123', txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while time.time() < timeout and v is None:
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get('ABC', txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)

        self.assertTrue(time.time() < timeout)
        self.assertEqual('123', v)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete('ABC', txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while time.time() < timeout and v is not None:
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get('ABC', txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)

        self.assertTrue(time.time() < timeout)
        self.assertEqual(None, v)
        return
Ejemplo n.º 10
0
    def test01_basic_replication(self):
        master_port = test_support.find_unused_port()
        client_port = test_support.find_unused_port()
        if db.version() >= (5, 2):
            self.site = self.dbenvMaster.repmgr_site('127.0.0.1', master_port)
            self.site.set_config(db.DB_GROUP_CREATOR, True)
            self.site.set_config(db.DB_LOCAL_SITE, True)
            self.site2 = self.dbenvMaster.repmgr_site('127.0.0.1', client_port)
            self.site3 = self.dbenvClient.repmgr_site('127.0.0.1', master_port)
            self.site3.set_config(db.DB_BOOTSTRAP_HELPER, True)
            self.site4 = self.dbenvClient.repmgr_site('127.0.0.1', client_port)
            self.site4.set_config(db.DB_LOCAL_SITE, True)
            d = {db.DB_BOOTSTRAP_HELPER: [False,
                                      False,
                                      True,
                                      False],
             db.DB_GROUP_CREATOR: [True,
                                   False,
                                   False,
                                   False],
             db.DB_LEGACY: [False,
                            False,
                            False,
                            False],
             db.DB_LOCAL_SITE: [True,
                                False,
                                False,
                                True],
             db.DB_REPMGR_PEER: [False,
                                 False,
                                 False,
                                 False]}
            for i, j in d.items():
                for k, v in zip([self.site,
                 self.site2,
                 self.site3,
                 self.site4], j):
                    if v:
                        self.assertTrue(k.get_config(i))
                    else:
                        self.assertFalse(k.get_config(i))

            self.assertNotEqual(self.site.get_eid(), self.site2.get_eid())
            self.assertNotEqual(self.site3.get_eid(), self.site4.get_eid())
            for i, j in zip([self.site,
             self.site2,
             self.site3,
             self.site4], [master_port,
             client_port,
             master_port,
             client_port]):
                addr = i.get_address()
                self.assertEqual(addr, ('127.0.0.1', j))

            for i in [self.site, self.site2]:
                self.assertEqual(i.get_address(), self.dbenvMaster.repmgr_site_by_eid(i.get_eid()).get_address())

            for i in [self.site3, self.site4]:
                self.assertEqual(i.get_address(), self.dbenvClient.repmgr_site_by_eid(i.get_eid()).get_address())

        else:
            self.dbenvMaster.repmgr_set_local_site('127.0.0.1', master_port)
            self.dbenvClient.repmgr_set_local_site('127.0.0.1', client_port)
            self.dbenvMaster.repmgr_add_remote_site('127.0.0.1', client_port)
            self.dbenvClient.repmgr_add_remote_site('127.0.0.1', master_port)
            self.dbenvMaster.rep_set_nsites(2)
            self.dbenvClient.rep_set_nsites(2)
        self.dbenvMaster.rep_set_priority(10)
        self.dbenvClient.rep_set_priority(0)
        self.dbenvMaster.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100123)
        self.dbenvClient.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100321)
        self.assertEqual(self.dbenvMaster.rep_get_timeout(db.DB_REP_CONNECTION_RETRY), 100123)
        self.assertEqual(self.dbenvClient.rep_get_timeout(db.DB_REP_CONNECTION_RETRY), 100321)
        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100234)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100432)
        self.assertEqual(self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT), 100234)
        self.assertEqual(self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT), 100432)
        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100345)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100543)
        self.assertEqual(self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100345)
        self.assertEqual(self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100543)
        self.dbenvMaster.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvClient.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvMaster.repmgr_start(1, db.DB_REP_MASTER)
        self.dbenvClient.repmgr_start(1, db.DB_REP_CLIENT)
        self.assertEqual(self.dbenvMaster.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvClient.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvMaster.rep_get_priority(), 10)
        self.assertEqual(self.dbenvClient.rep_get_priority(), 0)
        self.assertEqual(self.dbenvMaster.repmgr_get_ack_policy(), db.DB_REPMGR_ACKS_ALL)
        self.assertEqual(self.dbenvClient.repmgr_get_ack_policy(), db.DB_REPMGR_ACKS_ALL)
        import time
        timeout = time.time() + 60
        while time.time() < timeout and not (self.confirmed_master and self.client_startupdone):
            time.sleep(0.02)

        if time.time() > timeout:
            import sys
            print >> sys.stderr, 'XXX: timeout happened beforestartup was confirmed - see issue 3892'
            startup_timeout = True
        d = self.dbenvMaster.repmgr_site_list()
        self.assertEqual(len(d), 1)
        d = d.values()[0]
        self.assertEqual(d[0], '127.0.0.1')
        self.assertEqual(d[1], client_port)
        self.assertTrue(d[2] == db.DB_REPMGR_CONNECTED or d[2] == db.DB_REPMGR_DISCONNECTED)
        d = self.dbenvClient.repmgr_site_list()
        self.assertEqual(len(d), 1)
        d = d.values()[0]
        self.assertEqual(d[0], '127.0.0.1')
        self.assertEqual(d[1], master_port)
        self.assertTrue(d[2] == db.DB_REPMGR_CONNECTED or d[2] == db.DB_REPMGR_DISCONNECTED)
        if db.version() >= (4, 6):
            d = self.dbenvMaster.repmgr_stat(flags=db.DB_STAT_CLEAR)
            self.assertTrue('msgs_queued' in d)
        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open('test', db.DB_HASH, db.DB_CREATE, 438, txn=txn)
        txn.commit()
        import time, os.path
        timeout = time.time() + 10
        while time.time() < timeout and not os.path.exists(os.path.join(self.homeDirClient, 'test')):
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open('test', db.DB_HASH, flags=db.DB_RDONLY, mode=438, txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put('ABC', '123', txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while time.time() < timeout and v is None:
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get('ABC', txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)

        if time.time() >= timeout and startup_timeout:
            self.skipTest('replication test skipped due to random failure, see issue 3892')
        self.assertTrue(time.time() < timeout)
        self.assertEqual('123', v)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete('ABC', txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while time.time() < timeout and v is not None:
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get('ABC', txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)

        self.assertTrue(time.time() < timeout)
        self.assertEqual(None, v)
        return
Ejemplo n.º 11
0
    def test01_basic_replication(self) :
        master_port = test_support.find_unused_port()
        self.dbenvMaster.repmgr_set_local_site("127.0.0.1", master_port)
        client_port = test_support.find_unused_port()
        self.dbenvClient.repmgr_set_local_site("127.0.0.1", client_port)
        self.dbenvMaster.repmgr_add_remote_site("127.0.0.1", client_port)
        self.dbenvClient.repmgr_add_remote_site("127.0.0.1", master_port)
        self.dbenvMaster.rep_set_nsites(2)
        self.dbenvClient.rep_set_nsites(2)
        self.dbenvMaster.rep_set_priority(10)
        self.dbenvClient.rep_set_priority(0)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_CONNECTION_RETRY,100123)
        self.dbenvClient.rep_set_timeout(db.DB_REP_CONNECTION_RETRY,100321)
        self.assertEquals(self.dbenvMaster.rep_get_timeout(
            db.DB_REP_CONNECTION_RETRY), 100123)
        self.assertEquals(self.dbenvClient.rep_get_timeout(
            db.DB_REP_CONNECTION_RETRY), 100321)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100234)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100432)
        self.assertEquals(self.dbenvMaster.rep_get_timeout(
            db.DB_REP_ELECTION_TIMEOUT), 100234)
        self.assertEquals(self.dbenvClient.rep_get_timeout(
            db.DB_REP_ELECTION_TIMEOUT), 100432)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100345)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100543)
        self.assertEquals(self.dbenvMaster.rep_get_timeout(
            db.DB_REP_ELECTION_RETRY), 100345)
        self.assertEquals(self.dbenvClient.rep_get_timeout(
            db.DB_REP_ELECTION_RETRY), 100543)

        self.dbenvMaster.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvClient.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)

        self.dbenvMaster.repmgr_start(1, db.DB_REP_MASTER);
        self.dbenvClient.repmgr_start(1, db.DB_REP_CLIENT);

        self.assertEquals(self.dbenvMaster.rep_get_nsites(),2)
        self.assertEquals(self.dbenvClient.rep_get_nsites(),2)
        self.assertEquals(self.dbenvMaster.rep_get_priority(),10)
        self.assertEquals(self.dbenvClient.rep_get_priority(),0)
        self.assertEquals(self.dbenvMaster.repmgr_get_ack_policy(),
                db.DB_REPMGR_ACKS_ALL)
        self.assertEquals(self.dbenvClient.repmgr_get_ack_policy(),
                db.DB_REPMGR_ACKS_ALL)

        # The timeout is necessary in BDB 4.5, since DB_EVENT_REP_STARTUPDONE
        # is not generated if the master has no new transactions.
        # This is solved in BDB 4.6 (#15542).
        import time
        timeout = time.time()+10
        while (time.time()<timeout) and not (self.confirmed_master and self.client_startupdone) :
            time.sleep(0.02)
        self.assertTrue(time.time()<timeout)

        d = self.dbenvMaster.repmgr_site_list()
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0][0], "127.0.0.1")
        self.assertEquals(d[0][1], client_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        d = self.dbenvClient.repmgr_site_list()
        self.assertEquals(len(d), 1)
        self.assertEquals(d[0][0], "127.0.0.1")
        self.assertEquals(d[0][1], master_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        if db.version() >= (4,6) :
            d = self.dbenvMaster.repmgr_stat(flags=db.DB_STAT_CLEAR);
            self.assertTrue("msgs_queued" in d)

        self.dbMaster=db.DB(self.dbenvMaster)
        txn=self.dbenvMaster.txn_begin()
        self.dbMaster.open("test", db.DB_HASH, db.DB_CREATE, 0666, txn=txn)
        txn.commit()

        import time,os.path
        timeout=time.time()+10
        while (time.time()<timeout) and \
          not (os.path.exists(os.path.join(self.homeDirClient,"test"))) :
            time.sleep(0.01)

        self.dbClient=db.DB(self.dbenvClient)
        while True :
            txn=self.dbenvClient.txn_begin()
            try :
                self.dbClient.open("test", db.DB_HASH, flags=db.DB_RDONLY,
                        mode=0666, txn=txn)
            except db.DBRepHandleDeadError :
                txn.abort()
                self.dbClient.close()
                self.dbClient=db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        txn=self.dbenvMaster.txn_begin()
        self.dbMaster.put("ABC", "123", txn=txn)
        txn.commit()
        import time
        timeout=time.time()+10
        v=None
        while (time.time()<timeout) and (v==None) :
            txn=self.dbenvClient.txn_begin()
            v=self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v==None :
                time.sleep(0.02)
        self.assertTrue(time.time()<timeout)
        self.assertEquals("123", v)

        txn=self.dbenvMaster.txn_begin()
        self.dbMaster.delete("ABC", txn=txn)
        txn.commit()
        timeout=time.time()+10
        while (time.time()<timeout) and (v!=None) :
            txn=self.dbenvClient.txn_begin()
            v=self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v==None :
                time.sleep(0.02)
        self.assertTrue(time.time()<timeout)
        self.assertEquals(None, v)
Ejemplo n.º 12
0
 def test03_dbobj_type_before_open(self):
     self.assertRaises(db.DBInvalidArgError, db.DB().type)
    def test02_basicPost32(self):
        # Basic Queue tests using the new DB.consume method in DB 3.2+
        # (No cursor needed)

        if verbose:
            print '\n', '-=' * 30
            print "Running %s.test02_basicPost32..." % self.__class__.__name__

        if db.version() < (3, 2, 0):
            if verbose:
                print "Test not run, DB not new enough..."
            return

        d = db.DB()
        d.set_re_len(40)  # Queues must be fixed length
        d.open(self.filename, db.DB_QUEUE, db.DB_CREATE)

        if verbose:
            print "before appends" + '-' * 30
            pprint(d.stat())

        for x in string.letters:
            d.append(x * 40)

        self.assertEqual(len(d), len(string.letters))

        d.put(100, "some more data")
        d.put(101, "and some more ")
        d.put(75, "out of order")
        d.put(1, "replacement data")

        self.assertEqual(len(d), len(string.letters) + 3)

        if verbose:
            print "before close" + '-' * 30
            pprint(d.stat())

        d.close()
        del d
        d = db.DB()
        d.open(self.filename)
        #d.set_get_returns_none(true)

        if verbose:
            print "after open" + '-' * 30
            pprint(d.stat())

        d.append("one more")

        if verbose:
            print "after append" + '-' * 30
            pprint(d.stat())

        rec = d.consume()
        while rec:
            if verbose:
                print rec
            rec = d.consume()

        if verbose:
            print "after consume loop" + '-' * 30
            pprint(d.stat())

        d.close()
Ejemplo n.º 14
0
 def test03_dbobj_type_before_open(self):
     # Ensure this doesn't cause a segfault.
     self.assertRaises(db.DBInvalidArgError, db.DB().type)
Ejemplo n.º 15
0
    def test01_basic(self):
        # Basic Queue tests using the deprecated DBCursor.consume method.

        if verbose:
            print '\n', '-=' * 30
            print "Running %s.test01_basic..." % self.__class__.__name__

        d = db.DB()
        d.set_re_len(40)  # Queues must be fixed length
        d.open(self.filename, db.DB_QUEUE, db.DB_CREATE)

        if verbose:
            print "before appends" + '-' * 30
            pprint(d.stat())

        for x in string.ascii_letters:
            d.append(x * 40)

        self.assertEqual(len(d), len(string.ascii_letters))

        d.put(100, "some more data")
        d.put(101, "and some more ")
        d.put(75, "out of order")
        d.put(1, "replacement data")

        self.assertEqual(len(d), len(string.ascii_letters) + 3)

        if verbose:
            print "before close" + '-' * 30
            pprint(d.stat())

        d.close()
        del d
        d = db.DB()
        d.open(self.filename)

        if verbose:
            print "after open" + '-' * 30
            pprint(d.stat())

        # Test "txn" as a positional parameter
        d.append("one more", None)
        # Test "txn" as a keyword parameter
        d.append("another one", txn=None)

        c = d.cursor()

        if verbose:
            print "after append" + '-' * 30
            pprint(d.stat())

        rec = c.consume()
        while rec:
            if verbose:
                print rec
            rec = c.consume()
        c.close()

        if verbose:
            print "after consume loop" + '-' * 30
            pprint(d.stat())

        self.assertEqual(len(d), 0, \
               "if you see this message then you need to rebuild " \
               "Berkeley DB 3.1.17 with the patch in patches/qam_stat.diff")

        d.close()
Ejemplo n.º 16
0
 def createDB(self, key_comparator):
     self.db = db.DB(self.env)
     self.setupDB(key_comparator)
     self.db.open(self.filename, "test", db.DB_BTREE, db.DB_CREATE)
Ejemplo n.º 17
0
    def test01_basic_replication(self):
        master_port = test_support.find_unused_port()
        self.dbenvMaster.repmgr_set_local_site("127.0.0.1", master_port)
        client_port = test_support.find_unused_port()
        self.dbenvClient.repmgr_set_local_site("127.0.0.1", client_port)
        self.dbenvMaster.repmgr_add_remote_site("127.0.0.1", client_port)
        self.dbenvClient.repmgr_add_remote_site("127.0.0.1", master_port)
        self.dbenvMaster.rep_set_nsites(2)
        self.dbenvClient.rep_set_nsites(2)
        self.dbenvMaster.rep_set_priority(10)
        self.dbenvClient.rep_set_priority(0)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100123)
        self.dbenvClient.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100321)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100123)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100321)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100234)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100432)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100234)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100432)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100345)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100543)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100345)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100543)

        self.dbenvMaster.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvClient.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)

        self.dbenvMaster.repmgr_start(1, db.DB_REP_MASTER)
        self.dbenvClient.repmgr_start(1, db.DB_REP_CLIENT)

        self.assertEqual(self.dbenvMaster.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvClient.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvMaster.rep_get_priority(), 10)
        self.assertEqual(self.dbenvClient.rep_get_priority(), 0)
        self.assertEqual(self.dbenvMaster.repmgr_get_ack_policy(),
                         db.DB_REPMGR_ACKS_ALL)
        self.assertEqual(self.dbenvClient.repmgr_get_ack_policy(),
                         db.DB_REPMGR_ACKS_ALL)

        # The timeout is necessary in BDB 4.5, since DB_EVENT_REP_STARTUPDONE
        # is not generated if the master has no new transactions.
        # This is solved in BDB 4.6 (#15542).
        import time
        timeout = time.time() + 60
        while (time.time() < timeout) and not (self.confirmed_master
                                               and self.client_startupdone):
            time.sleep(0.02)
        # self.client_startupdone does not always get set to True within
        # the timeout.  On windows this may be a deep issue, on other
        # platforms it is likely just a timing issue, especially on slow
        # virthost buildbots (see issue 3892 for more).  Even though
        # the timeout triggers, the rest of this test method usually passes
        # (but not all of it always, see below).  So we just note the
        # timeout on stderr and keep soldering on.
        if time.time() > timeout:
            import sys
            print >> sys.stderr, ("XXX: timeout happened before"
                                  "startup was confirmed - see issue 3892")
            startup_timeout = True

        d = self.dbenvMaster.repmgr_site_list()
        self.assertEqual(len(d), 1)
        self.assertEqual(d[0][0], "127.0.0.1")
        self.assertEqual(d[0][1], client_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        d = self.dbenvClient.repmgr_site_list()
        self.assertEqual(len(d), 1)
        self.assertEqual(d[0][0], "127.0.0.1")
        self.assertEqual(d[0][1], master_port)
        self.assertTrue((d[0][2]==db.DB_REPMGR_CONNECTED) or \
                (d[0][2]==db.DB_REPMGR_DISCONNECTED))

        if db.version() >= (4, 6):
            d = self.dbenvMaster.repmgr_stat(flags=db.DB_STAT_CLEAR)
            self.assertTrue("msgs_queued" in d)

        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open("test", db.DB_HASH, db.DB_CREATE, 0666, txn=txn)
        txn.commit()

        import time, os.path
        timeout = time.time() + 10
        while (time.time()<timeout) and \
          not (os.path.exists(os.path.join(self.homeDirClient,"test"))) :
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open("test",
                                   db.DB_HASH,
                                   flags=db.DB_RDONLY,
                                   mode=0666,
                                   txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put("ABC", "123", txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while (time.time() < timeout) and (v is None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        # If startup did not happen before the timeout above, then this test
        # sometimes fails.  This happens randomly, which causes buildbot
        # instability, but all the other bsddb tests pass.  Since bsddb3 in the
        # stdlib is currently not getting active maintenance, and is gone in
        # py3k, we just skip the end of the test in that case.
        if time.time() >= timeout and startup_timeout:
            self.skipTest("replication test skipped due to random failure, "
                          "see issue 3892")
        self.assertTrue(time.time() < timeout)
        self.assertEqual("123", v)

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete("ABC", txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while (time.time() < timeout) and (v is not None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEqual(None, v)
 def createDB(self, dup_comparator):
     self.db = db.DB(self.env)
     self.setupDB(dup_comparator)
     self.db.open(self.filename, 'test', db.DB_BTREE, db.DB_CREATE)
Ejemplo n.º 19
0
    def test01_basic_replication(self):
        master_port = test_support.find_unused_port()
        client_port = test_support.find_unused_port()
        if db.version() >= (5, 2):
            self.site = self.dbenvMaster.repmgr_site("127.0.0.1", master_port)
            self.site.set_config(db.DB_GROUP_CREATOR, True)
            self.site.set_config(db.DB_LOCAL_SITE, True)
            self.site2 = self.dbenvMaster.repmgr_site("127.0.0.1", client_port)

            self.site3 = self.dbenvClient.repmgr_site("127.0.0.1", master_port)
            self.site3.set_config(db.DB_BOOTSTRAP_HELPER, True)
            self.site4 = self.dbenvClient.repmgr_site("127.0.0.1", client_port)
            self.site4.set_config(db.DB_LOCAL_SITE, True)

            d = {
                db.DB_BOOTSTRAP_HELPER: [False, False, True, False],
                db.DB_GROUP_CREATOR: [True, False, False, False],
                db.DB_LEGACY: [False, False, False, False],
                db.DB_LOCAL_SITE: [True, False, False, True],
                db.DB_REPMGR_PEER: [False, False, False, False],
            }

            for i, j in d.items():
                for k, v in \
                        zip([self.site, self.site2, self.site3, self.site4], j) :
                    if v:
                        self.assertTrue(k.get_config(i))
                    else:
                        self.assertFalse(k.get_config(i))

            self.assertNotEqual(self.site.get_eid(), self.site2.get_eid())
            self.assertNotEqual(self.site3.get_eid(), self.site4.get_eid())

            for i, j in zip([self.site, self.site2, self.site3, self.site4], \
                    [master_port, client_port, master_port, client_port]) :
                addr = i.get_address()
                self.assertEqual(addr, ("127.0.0.1", j))

            for i in [self.site, self.site2]:
                self.assertEqual(
                    i.get_address(),
                    self.dbenvMaster.repmgr_site_by_eid(
                        i.get_eid()).get_address())
            for i in [self.site3, self.site4]:
                self.assertEqual(
                    i.get_address(),
                    self.dbenvClient.repmgr_site_by_eid(
                        i.get_eid()).get_address())
        else:
            self.dbenvMaster.repmgr_set_local_site("127.0.0.1", master_port)
            self.dbenvClient.repmgr_set_local_site("127.0.0.1", client_port)
            self.dbenvMaster.repmgr_add_remote_site("127.0.0.1", client_port)
            self.dbenvClient.repmgr_add_remote_site("127.0.0.1", master_port)

            self.dbenvMaster.rep_set_nsites(2)
            self.dbenvClient.rep_set_nsites(2)

        self.dbenvMaster.rep_set_priority(10)
        self.dbenvClient.rep_set_priority(0)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100123)
        self.dbenvClient.rep_set_timeout(db.DB_REP_CONNECTION_RETRY, 100321)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100123)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_CONNECTION_RETRY),
            100321)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100234)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_TIMEOUT, 100432)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100234)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_TIMEOUT),
            100432)

        self.dbenvMaster.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100345)
        self.dbenvClient.rep_set_timeout(db.DB_REP_ELECTION_RETRY, 100543)
        self.assertEqual(
            self.dbenvMaster.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100345)
        self.assertEqual(
            self.dbenvClient.rep_get_timeout(db.DB_REP_ELECTION_RETRY), 100543)

        self.dbenvMaster.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)
        self.dbenvClient.repmgr_set_ack_policy(db.DB_REPMGR_ACKS_ALL)

        self.dbenvMaster.repmgr_start(1, db.DB_REP_MASTER)
        self.dbenvClient.repmgr_start(1, db.DB_REP_CLIENT)

        self.assertEqual(self.dbenvMaster.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvClient.rep_get_nsites(), 2)
        self.assertEqual(self.dbenvMaster.rep_get_priority(), 10)
        self.assertEqual(self.dbenvClient.rep_get_priority(), 0)
        self.assertEqual(self.dbenvMaster.repmgr_get_ack_policy(),
                         db.DB_REPMGR_ACKS_ALL)
        self.assertEqual(self.dbenvClient.repmgr_get_ack_policy(),
                         db.DB_REPMGR_ACKS_ALL)

        # The timeout is necessary in BDB 4.5, since DB_EVENT_REP_STARTUPDONE
        # is not generated if the master has no new transactions.
        # This is solved in BDB 4.6 (#15542).
        import time
        timeout = time.time() + 10
        while (time.time() < timeout) and not (self.confirmed_master
                                               and self.client_startupdone):
            time.sleep(0.02)
        self.assertTrue(time.time() < timeout)

        d = self.dbenvMaster.repmgr_site_list()
        self.assertEqual(len(d), 1)
        d = d.values()[0]  # There is only one
        self.assertEqual(d[0], "127.0.0.1")
        self.assertEqual(d[1], client_port)
        self.assertTrue((d[2]==db.DB_REPMGR_CONNECTED) or \
                (d[2]==db.DB_REPMGR_DISCONNECTED))

        d = self.dbenvClient.repmgr_site_list()
        self.assertEqual(len(d), 1)
        d = d.values()[0]  # There is only one
        self.assertEqual(d[0], "127.0.0.1")
        self.assertEqual(d[1], master_port)
        self.assertTrue((d[2]==db.DB_REPMGR_CONNECTED) or \
                (d[2]==db.DB_REPMGR_DISCONNECTED))

        d = self.dbenvMaster.repmgr_stat(flags=db.DB_STAT_CLEAR)
        self.assertTrue("msgs_queued" in d)

        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open("test", db.DB_HASH, db.DB_CREATE, 0666, txn=txn)
        txn.commit()

        import time, os.path
        timeout = time.time() + 10
        while (time.time()<timeout) and \
          not (os.path.exists(os.path.join(self.homeDirClient,"test"))) :
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open("test",
                                   db.DB_HASH,
                                   flags=db.DB_RDONLY,
                                   mode=0666,
                                   txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put("ABC", "123", txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while (time.time() < timeout) and (v is None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEqual("123", v)

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete("ABC", txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while (time.time() < timeout) and (v is not None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEqual(None, v)
Ejemplo n.º 20
0
                self.assertEqual(val.args[0], db.DB_KEYEXIST)
            if verbose: print val
        else:
            self.fail("expected exception")

        if self.dbsetflags & db.DB_DUP:
            self.assertEqual(d.get('abcd'), 'a new record')
        else:
            self.assertEqual(d.get('abcd'), 'same key')


        d.sync()
        d.close()
        del d

        self.d = db.DB(self.env)
        if self.dbname:
            self.d.open(self.filename, self.dbname)
        else:
            self.d.open(self.filename)
        d = self.d

        self.assertEqual(d.get('0321'), '0321-0321-0321-0321-0321')
        if self.dbsetflags & db.DB_DUP:
            self.assertEqual(d.get('abcd'), 'a new record')
        else:
            self.assertEqual(d.get('abcd'), 'same key')

        rec = d.get_both('0555', '0555-0555-0555-0555-0555')
        if verbose:
            print rec
Ejemplo n.º 21
0
    def test01_basic_replication(self):
        self.basic_rep_threading()

        # The timeout is necessary in BDB 4.5, since DB_EVENT_REP_STARTUPDONE
        # is not generated if the master has no new transactions.
        # This is solved in BDB 4.6 (#15542).
        import time
        timeout = time.time() + 10
        while (time.time() < timeout) and not (self.confirmed_master
                                               and self.client_startupdone):
            time.sleep(0.02)
        self.assertTrue(time.time() < timeout)

        self.dbMaster = db.DB(self.dbenvMaster)
        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.open("test", db.DB_HASH, db.DB_CREATE, 0666, txn=txn)
        txn.commit()

        import time, os.path
        timeout = time.time() + 10
        while (time.time()<timeout) and \
          not (os.path.exists(os.path.join(self.homeDirClient,"test"))) :
            time.sleep(0.01)

        self.dbClient = db.DB(self.dbenvClient)
        while True:
            txn = self.dbenvClient.txn_begin()
            try:
                self.dbClient.open("test",
                                   db.DB_HASH,
                                   flags=db.DB_RDONLY,
                                   mode=0666,
                                   txn=txn)
            except db.DBRepHandleDeadError:
                txn.abort()
                self.dbClient.close()
                self.dbClient = db.DB(self.dbenvClient)
                continue

            txn.commit()
            break

        d = self.dbenvMaster.rep_stat(flags=db.DB_STAT_CLEAR)
        self.assertTrue("master_changes" in d)

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.put("ABC", "123", txn=txn)
        txn.commit()
        import time
        timeout = time.time() + 10
        v = None
        while (time.time() < timeout) and (v is None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEqual("123", v)

        txn = self.dbenvMaster.txn_begin()
        self.dbMaster.delete("ABC", txn=txn)
        txn.commit()
        timeout = time.time() + 10
        while (time.time() < timeout) and (v is not None):
            txn = self.dbenvClient.txn_begin()
            v = self.dbClient.get("ABC", txn=txn)
            txn.commit()
            if v is None:
                time.sleep(0.02)
        self.assertTrue(time.time() < timeout)
        self.assertEqual(None, v)
Ejemplo n.º 22
0
 def test07_verify(self):
     # Verify bug solved in 4.7.3pre8
     self.d.close()
     d = db.DB(self.env)
     d.verify(self.filename)
Ejemplo n.º 23
0
 def setUp(self) :
     self.homeDir = get_new_environment_path()
     self.env = db.DBEnv()
     self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL |
             db.DB_INIT_LOG | db.DB_INIT_TXN)
     self.db = db.DB(self.env)
Ejemplo n.º 24
0
    def test01_GetsAndPuts(self):
        d = self.d
        if verbose:
            print '\n', '-=' * 30
            print 'Running %s.test01_GetsAndPuts...' % self.__class__.__name__
        for key in ['0001',
         '0100',
         '0400',
         '0700',
         '0999']:
            data = d.get(key)
            if verbose:
                print data

        self.assertEqual(d.get('0321'), '0321-0321-0321-0321-0321')
        self.assertEqual(d.get('abcd'), None)
        try:
            d.delete('abcd')
        except db.DBNotFoundError as val:
            if sys.version_info < (2, 6):
                self.assertEqual(val[0], db.DB_NOTFOUND)
            else:
                self.assertEqual(val.args[0], db.DB_NOTFOUND)
            if verbose:
                print val
        else:
            self.fail('expected exception')

        d.put('abcd', 'a new record')
        self.assertEqual(d.get('abcd'), 'a new record')
        d.put('abcd', 'same key')
        if self.dbsetflags & db.DB_DUP:
            self.assertEqual(d.get('abcd'), 'a new record')
        else:
            self.assertEqual(d.get('abcd'), 'same key')
        try:
            d.put('abcd', 'this should fail', flags=db.DB_NOOVERWRITE)
        except db.DBKeyExistError as val:
            if sys.version_info < (2, 6):
                self.assertEqual(val[0], db.DB_KEYEXIST)
            else:
                self.assertEqual(val.args[0], db.DB_KEYEXIST)
            if verbose:
                print val
        else:
            self.fail('expected exception')

        if self.dbsetflags & db.DB_DUP:
            self.assertEqual(d.get('abcd'), 'a new record')
        else:
            self.assertEqual(d.get('abcd'), 'same key')
        d.sync()
        d.close()
        del d
        self.d = db.DB(self.env)
        if self.dbname:
            self.d.open(self.filename, self.dbname)
        else:
            self.d.open(self.filename)
        d = self.d
        self.assertEqual(d.get('0321'), '0321-0321-0321-0321-0321')
        if self.dbsetflags & db.DB_DUP:
            self.assertEqual(d.get('abcd'), 'a new record')
        else:
            self.assertEqual(d.get('abcd'), 'same key')
        rec = d.get_both('0555', '0555-0555-0555-0555-0555')
        if verbose:
            print rec
        self.assertEqual(d.get_both('0555', 'bad data'), None)
        data = d.get('bad key', 'bad data')
        self.assertEqual(data, 'bad data')
        data = d.get('bad key', self)
        self.assertEqual(data, self)
        s = d.stat()
        self.assertEqual(type(s), type({}))
        if verbose:
            print 'd.stat() returned this dictionary:'
            pprint(s)
        return
Ejemplo n.º 25
0
        rec = c.set_range(30)
        if verbose:
            print rec

        # test that non-existent key lookups work (and that
        # DBC_set_range doesn't have a memleak under valgrind)
        rec = c.set_range(999999)
        self.assertEqual(rec, None)
        if verbose:
            print rec

        c.close()
        d.close()

        d = db.DB()
        d.open(self.filename)
        c = d.cursor()

        # put a record beyond the consecutive end of the recno's
        d[100] = "way out there"
        self.assertEqual(d[100], "way out there")

        try:
            data = d[99]
        except KeyError:
            pass
        else:
            self.fail("expected exception")

        try:
Ejemplo n.º 26
0
    def test11_MultiDB(self):
        d1 = self.d
        if verbose:
            print '\n', '-=' * 30
            print 'Running %s.test11_MultiDB...' % self.__class__.__name__
        d2 = db.DB(self.env)
        d2.open(self.filename, 'second', self.dbtype, self.dbopenflags | db.DB_CREATE)
        d3 = db.DB(self.env)
        d3.open(self.filename, 'third', self.otherType(), self.dbopenflags | db.DB_CREATE)
        for x in 'The quick brown fox jumped over the lazy dog'.split():
            d2.put(x, self.makeData(x))

        for x in string.letters:
            d3.put(x, x * 70)

        d1.sync()
        d2.sync()
        d3.sync()
        d1.close()
        d2.close()
        d3.close()
        self.d = d1 = d2 = d3 = None
        self.d = d1 = db.DB(self.env)
        d1.open(self.filename, self.dbname, flags=self.dbopenflags)
        d2 = db.DB(self.env)
        d2.open(self.filename, 'second', flags=self.dbopenflags)
        d3 = db.DB(self.env)
        d3.open(self.filename, 'third', flags=self.dbopenflags)
        c1 = d1.cursor()
        c2 = d2.cursor()
        c3 = d3.cursor()
        count = 0
        rec = c1.first()
        while rec is not None:
            count = count + 1
            if verbose and count % 50 == 0:
                print rec
            rec = c1.next()

        self.assertEqual(count, self._numKeys)
        count = 0
        rec = c2.first()
        while rec is not None:
            count = count + 1
            if verbose:
                print rec
            rec = c2.next()

        self.assertEqual(count, 9)
        count = 0
        rec = c3.first()
        while rec is not None:
            count = count + 1
            if verbose:
                print rec
            rec = c3.next()

        self.assertEqual(count, len(string.letters))
        c1.close()
        c2.close()
        c3.close()
        d2.close()
        d3.close()
        return
Ejemplo n.º 27
0
    def test01_basic(self):
        d = db.DB()
        get_returns_none = d.set_get_returns_none(2)
        d.set_get_returns_none(get_returns_none)
        d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
        for x in letters:
            recno = d.append(x * 60)
            self.assertIsInstance(recno, int)
            self.assertGreaterEqual(recno, 1)
            if verbose:
                print recno,

        if verbose:
            print
        stat = d.stat()
        if verbose:
            pprint(stat)
        for recno in range(1, len(d) + 1):
            data = d[recno]
            if verbose:
                print data
            self.assertIsInstance(data, str)
            self.assertEqual(data, d.get(recno))

        try:
            data = d[0]
        except db.DBInvalidArgError as val:
            if sys.version_info < (2, 6):
                self.assertEqual(val[0], db.EINVAL)
            else:
                self.assertEqual(val.args[0], db.EINVAL)
            if verbose:
                print val
        else:
            self.fail('expected exception')

        try:
            d.has_key(0)
        except db.DBError as val:
            pass
        else:
            self.fail('has_key did not raise a proper exception')

        try:
            data = d[100]
        except KeyError:
            pass
        else:
            self.fail('expected exception')

        try:
            data = d.get(100)
        except db.DBNotFoundError as val:
            if get_returns_none:
                self.fail('unexpected exception')
        else:
            self.assertEqual(data, None)

        keys = d.keys()
        if verbose:
            print keys
        self.assertIsInstance(keys, list)
        self.assertIsInstance(keys[0], int)
        self.assertEqual(len(keys), len(d))
        items = d.items()
        if verbose:
            pprint(items)
        self.assertIsInstance(items, list)
        self.assertIsInstance(items[0], tuple)
        self.assertEqual(len(items[0]), 2)
        self.assertIsInstance(items[0][0], int)
        self.assertIsInstance(items[0][1], str)
        self.assertEqual(len(items), len(d))
        self.assertTrue(d.has_key(25))
        del d[25]
        self.assertFalse(d.has_key(25))
        d.delete(13)
        self.assertFalse(d.has_key(13))
        data = d.get_both(26, 'z' * 60)
        self.assertEqual(data, 'z' * 60, 'was %r' % data)
        if verbose:
            print data
        fd = d.fd()
        if verbose:
            print fd
        c = d.cursor()
        rec = c.first()
        while rec:
            if verbose:
                print rec
            rec = c.next()

        c.set(50)
        rec = c.current()
        if verbose:
            print rec
        c.put(-1, 'a replacement record', db.DB_CURRENT)
        c.set(50)
        rec = c.current()
        self.assertEqual(rec, (50, 'a replacement record'))
        if verbose:
            print rec
        rec = c.set_range(30)
        if verbose:
            print rec
        rec = c.set_range(999999)
        self.assertEqual(rec, None)
        if verbose:
            print rec
        c.close()
        d.close()
        d = db.DB()
        d.open(self.filename)
        c = d.cursor()
        d[100] = 'way out there'
        self.assertEqual(d[100], 'way out there')
        try:
            data = d[99]
        except KeyError:
            pass
        else:
            self.fail('expected exception')

        try:
            d.get(99)
        except db.DBKeyEmptyError as val:
            if get_returns_none:
                self.fail('unexpected DBKeyEmptyError exception')
            else:
                if sys.version_info < (2, 6):
                    self.assertEqual(val[0], db.DB_KEYEMPTY)
                else:
                    self.assertEqual(val.args[0], db.DB_KEYEMPTY)
                if verbose:
                    print val
        else:
            if not get_returns_none:
                self.fail('expected exception')

        rec = c.set(40)
        while rec:
            if verbose:
                print rec
            rec = c.next()

        c.close()
        d.close()
        return