Exemple #1
0
 def check_oid_reuse(self):
     # Requires ShelfStorage oid reuse pack semantics
     s1 = ClientStorage(address=self.address)
     s1.oid_pool_size = 1
     c1 = Connection(s1)
     r1 = c1.get_root()
     s2 = ClientStorage(address=self.address)
     s2.oid_pool_size = 1
     c2 = Connection(s2)
     r2 = c2.get_root()
     r1['a'] = PersistentDict()
     r1['b'] = PersistentDict()
     c1.commit()
     c2.abort()
     a_oid = r1['a']._p_oid
     assert 'a' in r1 and 'b' in r1 and len(r1['b']) == 0
     assert 'a' in r2 and 'b' in r2 and len(r2['b']) == 0
     del r2['a'] # remove only reference to a
     c2.commit()
     c2.pack() # force relinquished oid back into availability
     sleep(0.5) # Give time for pack to complete
     c2.abort()
     assert c2.get(a_oid) is None
     c1.abort()
     assert c1.get(a_oid)._p_is_ghost()
     r2['b']['new'] = Persistent()
     r2['b']['new'].bogus = 1
     c2.commit()
     assert c2.get(a_oid) is r2['b']['new']
     c1.abort()
     assert c1.get(a_oid).__class__ == PersistentDict
     r1['b']['new'].bogus
     assert c1.get(a_oid).__class__ == Persistent
     s1.close()
Exemple #2
0
    def check_storage_tools(self):
        connection = Connection(self._get_storage())
        root = connection.get_root()
        root['a'] = Persistent()
        root['b'] = Persistent()
        connection.commit()
        index = get_reference_index(connection.get_storage())
        assert index == {
            int8_to_str(1): [int8_to_str(0)],
            int8_to_str(2): [int8_to_str(0)]
        }
        census = get_census(connection.get_storage())
        assert census == {
            as_bytes('PersistentDict'): 1,
            as_bytes('Persistent'): 2
        }
        references = list(
            gen_referring_oid_record(connection.get_storage(), int8_to_str(1)))
        assert references == [(int8_to_str(0),
                               connection.get_storage().load(int8_to_str(0)))]

        class Fake(object):
            pass

        s = Fake()
        s.__class__ = Storage
        raises(RuntimeError, s.__init__)
        raises(NotImplementedError, s.load, None)
        raises(NotImplementedError, s.begin)
        raises(NotImplementedError, s.store, None, None)
        raises(NotImplementedError, s.end)
        raises(NotImplementedError, s.sync)
        g = s.gen_oid_record()
        raises(NotImplementedError, next, g)
Exemple #3
0
 def call_callback(self):
     d = ObjectDictionary()
     assert len(d) == 0
     key = 'ok'
     x = Persistent()
     x.key = key
     d[key] = x
     d.callback(x)
     assert key in d.dead
     assert key in d.mapping
Exemple #4
0
 def call_callback(self):
     d = ObjectDictionary()
     assert len(d) == 0
     key = 'ok'
     x = Persistent()
     x.key = key
     d[key] = x
     d.callback(x)
     assert key in d.dead
     assert key in d.mapping
Exemple #5
0
 def check_connection(self):
     self.conn=conn=Connection(self._get_storage())
     self.root=root=conn.get_root()
     assert root._p_is_ghost() == False
     assert root is conn.get(int8_to_str(0))
     assert root is conn.get(0)
     assert conn is root._p_connection
     assert conn.get(int8_to_str(1)) == None
     conn.abort()
     conn.commit()
     assert root._p_is_ghost() == False
     root['a'] = Persistent()
     assert root._p_is_unsaved() == True
     assert root['a']._p_is_unsaved() == True
     root['a'].f=2
     assert list(conn.changed.values()) == [root]
     conn.commit()
     assert root._p_is_saved()
     assert list(conn.changed.values()) == []
     root['a'] = Persistent()
     assert list(conn.changed.values()) == [root]
     root['b'] = Persistent()
     root['a'].a = 'a'
     root['b'].b = 'b'
     conn.commit()
     root['a'].a = 'a'
     root['b'].b = 'b'
     conn.abort()
     conn.shrink_cache()
     root['b'].b = 'b'
     del conn
Exemple #6
0
 def check_touch_every_reference(self):
     connection = Connection(self._get_storage())
     root = connection.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     from durus.persistent_list import PersistentList
     root['b'].c = PersistentList()
     connection.commit()
     touch_every_reference(connection, 'PersistentList')
     assert root['b']._p_is_unsaved()
     assert root['b'].c._p_is_unsaved()
     assert not root._p_is_unsaved()
     assert len(list(connection.get_cache())) == 4
Exemple #7
0
 def _scenario(self):
     c1 = Connection(self._get_storage())
     c2 = Connection(self._get_storage())
     c1.get_root()['A'] = Persistent()
     c1.get_root()['B'] = Persistent()
     c1.get_root()['A'].a = 1
     c1.commit()
     c2.abort()
     c1.cache.recent_objects.discard(c1.get_root()['A'])
     # Imagine c1 has been running for a while, and
     # cache management, for example, has caused the
     # cache reference to be weak.
     return c1, c2
Exemple #8
0
 def check_touch_every_reference(self):
     connection = Connection(self._get_storage())
     root = connection.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     from durus.persistent_list import PersistentList
     root['b'].c = PersistentList()
     connection.commit()
     touch_every_reference(connection, 'PersistentList')
     assert root['b']._p_is_unsaved()
     assert root['b'].c._p_is_unsaved()
     assert not root._p_is_unsaved()
     assert len(list(connection.get_cache())) == 4
Exemple #9
0
 def b(self):
     d = ObjectDictionary()
     assert len(d) == 0
     key = 'ok'
     x = Persistent()
     x.key = key
     d[key] = x
     assert d.get(key) is not None
     assert len(d) == 1
     assert list(d) == [key]
     del d[key]
     assert len(d) == 0
     assert d.get(key) is None
     assert list(d) == []
Exemple #10
0
 def b(self):
     d = ObjectDictionary()
     assert len(d) == 0
     key = 'ok'
     x = Persistent()
     x.key = key
     d[key] = x
     assert d.get(key) is not None
     assert len(d) == 1
     assert list(d) == [key]
     del d[key]
     assert len(d) == 0
     assert d.get(key) is None
     assert list(d) == []
Exemple #11
0
 def check_conflict(self):
     b = Connection(self._get_storage())
     c = Connection(self._get_storage())
     rootb = b.get(int8_to_str(0))
     rootb['b'] = Persistent()
     rootc = c.get(int8_to_str(0))
     rootc['c'] = Persistent()
     c.commit()
     raises(ConflictError, b.commit)
     raises(KeyError, rootb.__getitem__, 'c')
     transaction_serial = b.transaction_serial
     b.abort()
     assert b.get_transaction_serial() > transaction_serial
     assert rootb._p_is_ghost()
     rootc['d'] = Persistent()
     c.commit()
     rootb['d']
Exemple #12
0
 def check_object_writer(self):
     class FakeConnection(ConnectionBase):
         def new_oid(self):
             return ROOT_OID
         def note_access(self, obj):
             pass
     connection = FakeConnection()
     self.s=s=ObjectWriter(connection)
     x = Persistent()
     assert x._p_connection == None
     x._p_oid = ROOT_OID
     x._p_connection = connection
     assert s._persistent_id(x) == (ROOT_OID, Persistent)
     x._p_connection = FakeConnection()
     # connection of x no longer matches connection of s.
     raises(ValueError, s._persistent_id, x)
     x.a = Persistent()
     assert s.get_state(x), (
         '\x80\x02cdurus.persistent\nPersistent\nq\x01.\x80\x02}q\x02U'
         '\x01aU\x08\x00\x00\x00\x00\x00\x00\x00\x00q\x03h\x01\x86Qs.',
         '\x00\x00\x00\x00\x00\x00\x00\x00')
     assert list(s.gen_new_objects(x)) == [x, x.a]
     # gen_new_objects() can only be called once.
     raises(RuntimeError, s.gen_new_objects, 3)
     s.close()
Exemple #13
0
 def check_accessors(self):
     p=Persistent()
     p._p_oid
     assert p._p_format_oid() == 'None'
     p._p_oid = 'aaaaaaaa'
     assert p._p_format_oid() == '7016996765293437281'
     p._p_oid = int8_to_str(1)
     assert p._p_format_oid() == '1'
     assert repr(p) == "<Persistent 1>"
Exemple #14
0
 def check_fine_conflict(self):
     c1 = Connection(self._get_storage())
     c2 = Connection(self._get_storage())
     c1.get_root()['A'] = Persistent()
     c1.get_root()['A'].a = 1
     c1.get_root()['B'] = Persistent()
     c1.commit()
     c2.abort()
     # c1 has A loaded.
     assert not c1.get_root()['A']._p_is_ghost()
     c1.get_root()['B'].b = 1
     c2.get_root()['A'].a = 2
     c2.commit()
     # Even though A has been changed by c2,
     # c1 has not accessed an attribute of A since
     # the last c1.commit(), so we don't want a ConflictError.
     c1.commit()
     assert c1.get_root()['A']._p_is_ghost()
     c1.get_root()['A'].a  # accessed!
     c1.get_root()['B'].b = 1
     c2.get_root()['A'].a = 2
     c2.commit()
     raises(WriteConflictError, c1.commit)
Exemple #15
0
 def check_accessors(self):
     p=Persistent()
     p._p_oid
     assert p._p_format_oid() == 'None'
     p._p_oid = 'aaaaaaaa'
     assert p._p_format_oid() == '7016996765293437281'
     p._p_oid = int8_to_str(1)
     assert p._p_format_oid() == '1'
     assert repr(p) == "<Persistent 1>"
Exemple #16
0
 def check_shrink(self):
     storage = self._get_storage()
     self.conn=conn=Connection(storage, cache_size=3)
     self.root=root=conn.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     root['c'] = Persistent()
     assert self.root._p_is_unsaved()
     conn.commit()
     root['a'].a = 1
     conn.commit()
     root['b'].b = 1
     root['c'].c = 1
     root['d'] = Persistent()
     root['e'] = Persistent()
     root['f'] = Persistent()
     conn.commit()
     root['f'].f = 1
     root['g'] = Persistent()
     conn.commit()
     conn.pack()
Exemple #17
0
 def b(self):
     f = File(prefix='shelftest')
     name = f.get_name()
     f.close()
     s = FileStorage(name)
     c = Connection(s)
     r = c.get_root()
     for x in range(10):
         r["a%s" % x] = Persistent()
         c.commit()
     deleted_oid = r['a9']._p_oid
     del r['a9']
     c.commit()
     c.pack()
     c.abort()
     assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 10
     new_oid = s.new_oid()
     assert new_oid == deleted_oid
     new_oid = s.new_oid()
     assert new_oid == int8_to_str(11)
Exemple #18
0
 def a(self):
     f = File(prefix='shelftest')
     name = f.get_name()
     f.close()
     s = FileStorage(name)
     c = Connection(s)
     r = c.get_root()
     for x in range(10):
         r["a%s" % x] = Persistent()
         c.commit()
     deleted_oids = [
         r['a0']._p_oid, r['a2']._p_oid, r['a7']._p_oid, r['a8']._p_oid
     ]
     del r['a0']
     del r['a2']
     del r['a7']
     del r['a8']
     c.commit()
     c.pack()
     c.abort()
     assert c.get(deleted_oids[0])._p_is_ghost()
     assert c.get(deleted_oids[1])._p_is_ghost()
     raises(KeyError, getattr, c.get(deleted_oids[0]), 'a')
     assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 7
     c.commit()
     c.pack()
     new_oid = s.new_oid()
     assert new_oid == deleted_oids[-1], (new_oid, deleted_oids)
     new_oid = s.new_oid()
     assert new_oid == deleted_oids[-2], (new_oid, deleted_oids)
     new_oid = s.new_oid()
     assert new_oid == deleted_oids[-3], (new_oid, deleted_oids)
     new_oid = s.new_oid()
     assert new_oid == deleted_oids[-4], (new_oid, deleted_oids)
     new_oid = s.new_oid()
     assert new_oid == int8_to_str(11), repr(new_oid)
     new_oid = s.new_oid()
     assert new_oid == int8_to_str(12), repr(new_oid)
Exemple #19
0
 def c(self):
     f = File(prefix='shelftest')
     name = f.get_name()
     f.close()
     s = FileStorage(name)
     c = Connection(s)
     r = c.get_root()
     for x in range(10):
         r["a%s" % x] = Persistent()
         c.commit()
     deleted_oid = r['a9']._p_oid
     del r['a9']
     c.commit()
     c.pack()
     c.abort()
     r.clear()
     c.commit()
     c.pack()
     c.abort()
     new_oid = s.new_oid()
     assert new_oid == int8_to_str(1), repr(new_oid)
     new_oid = s.new_oid()
     assert new_oid == int8_to_str(2), repr(new_oid)
Exemple #20
0
 def check_connection(self):
     self.conn = conn = Connection(self._get_storage())
     self.root = root = conn.get_root()
     assert root._p_is_ghost() == False
     assert root is conn.get(int8_to_str(0))
     assert root is conn.get(0)
     assert conn is root._p_connection
     assert conn.get(int8_to_str(1)) == None
     conn.abort()
     conn.commit()
     assert root._p_is_ghost() == False
     root['a'] = Persistent()
     assert root._p_is_unsaved() == True
     assert root['a']._p_is_unsaved() == True
     root['a'].f = 2
     assert list(conn.changed.values()) == [root]
     conn.commit()
     assert root._p_is_saved()
     assert list(conn.changed.values()) == []
     root['a'] = Persistent()
     assert list(conn.changed.values()) == [root]
     root['b'] = Persistent()
     root['a'].a = 'a'
     root['b'].b = 'b'
     conn.commit()
     root['a'].a = 'a'
     root['b'].b = 'b'
     conn.abort()
     conn.shrink_cache()
     root['b'].b = 'b'
     del conn
Exemple #21
0
 def check_shrink(self):
     storage = self._get_storage()
     self.conn = conn = Connection(storage, cache_size=3)
     self.root = root = conn.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     root['c'] = Persistent()
     assert self.root._p_is_unsaved()
     conn.commit()
     root['a'].a = 1
     conn.commit()
     root['b'].b = 1
     root['c'].c = 1
     root['d'] = Persistent()
     root['e'] = Persistent()
     root['f'] = Persistent()
     conn.commit()
     root['f'].f = 1
     root['g'] = Persistent()
     conn.commit()
     conn.pack()
Exemple #22
0
 def check_getstate(self):
     p=Persistent()
     assert p.__getstate__() == {}
     p.a = 1
     assert p.__getstate__() == {'a':1}
Exemple #23
0
 def check_setstate(self):
     p=Persistent()
     p.__setstate__({})
     p.__setstate__({'a':1})
     assert p.a == 1
Exemple #24
0
 def check_setstate(self):
     p=Persistent()
     p.__setstate__({})
     p.__setstate__({'a':1})
     assert p.a == 1
Exemple #25
0
 def check_getstate(self):
     p=Persistent()
     assert p.__getstate__() == {}
     p.a = 1
     assert p.__getstate__() == {'a':1}
Exemple #26
0
 def pickling(self):
     a = Persistent()
     pickle_a = dumps(a, 2)
     b = loads(pickle_a)
     assert isinstance(b, Persistent)