def test_consume(self, fudge_time):

        fudge_time.is_callable()
        fudge_time.returns(0)
        bucket = PersistentTokenBucket(2)

        assert_that(bucket, validly_provides(interfaces.ITokenBucket))

        # at time 0, the bucket has two tokens in it
        assert_that(bucket.consume(), is_true())
        assert_that(bucket.consume(), is_true())

        # Which are now gone
        assert_that(bucket.consume(), is_false())

        # If we strobe the clock forward, we can get another token, since
        # we are refilling at one per second
        fudge_time.returns(1)

        assert_that(bucket.consume(), is_true())
        assert_that(bucket.consume(), is_false())

        # skip forward two seconds, and we can consume both tokens again
        fudge_time.returns(3)
        assert_that(bucket.consume(2), is_true())
        assert_that(bucket.consume(), is_false())

        # cover
        assert_that(repr(bucket), is_('PersistentTokenBucket(2.0,1.0)'))
Beispiel #2
0
    def test_isBroken_no_activate(self):
        class O(object):
            pass

        o = O()
        assert_that(isBroken(o), is_false())

        interface.alsoProvides(o, IBroken)
        assert_that(isBroken(o), is_true())
Beispiel #3
0
    def test_pickle_setstate_swap_class_zodb(self):
        storage = DemoStorage()

        self._store_base_subs_in_zodb(storage)

        db = DB(storage)
        conn = db.open()
        new_base = conn.root()['base']
        new_base._p_activate()
        new_sub = conn.root()['sub']

        # Still in place
        assert_that(new_sub.adapters.__bases__, is_((new_base.adapters,)))
        assert_that(new_sub.utilities.__bases__, is_((new_base.utilities,)))

        # And they changed type
        assert_that(new_sub.adapters.__bases__[0], is_(BTreeLocalAdapterRegistry))
        assert_that(new_sub.utilities.__bases__[0], is_(BTreeLocalAdapterRegistry))

        # But p_changed wasn't set.
        assert_that(new_sub.adapters.__bases__[0],
                    has_property('_p_changed', is_false()))

        transaction.commit()
        conn.close()
        db.close()

        db = DB(storage)
        conn = db.open()
        new_sub = conn.root()['sub']
        # Now, we didn't rewrite the reference so the type stayed the same
        assert_that(type(new_sub.adapters.__bases__[0]), _LocalAdapterRegistry)
        assert_that(new_sub.adapters.__bases__[0],
                    has_property('_p_changed', is_false()))

        # Loading the base changes the types
        new_base = conn.root()['base']
        assert_that(new_base.adapters, is_(BTreeLocalAdapterRegistry))
        assert_that(new_base.adapters,
                    same_instance(new_sub.adapters.__bases__[0]))
        assert_that(new_sub.adapters.__bases__[0], is_(BTreeLocalAdapterRegistry))

        assert_that(new_sub.adapters.__bases__[0],
                    has_property('_p_changed', is_false()))
Beispiel #4
0
    def test_ext_accept_external_id_no_tagged_value(self):
        # If there is no id field, the value is false

        class I(interface.Interface):
            id = interface.Attribute("An id")

        @interface.implementer(I)
        class O(object):
            pass

        ext_self = O()
        inst = self._makeOne(ext_self, iface_upper_bound=I)

        assert_that(inst._ext_accept_external_id(ext_self, None), is_false())
Beispiel #5
0
    def test_activate_errors(self):
        from ZODB.POSException import POSKeyError
        class O(object):
            raises = TypeError

            def _p_activate(self):
                raise self.raises()

        o = O()

        # Raising a TypeError doesn't do anything
        assert_that(isBroken(o), is_false())

        # POSKeyError does
        o.raises = POSKeyError
        assert_that(isBroken(o), is_true())


        # other exceptions are passed
        o.raises = KeyError
        self.assertRaises(KeyError, isBroken, o)
    def test_wait(self, fudge_time, fudge_sleep):

        fudge_time.is_callable()
        fudge_time.returns(0)
        bucket = PersistentTokenBucket(2)

        # at time 0, the bucket has two tokens in it
        assert_that(bucket.wait_for_token(), is_true())
        assert_that(bucket.wait_for_token(), is_true())

        # Which are now gone
        assert_that(bucket.consume(), is_false())

        fudge_sleep.is_callable()

        def _sleep(how_long):
            # If we strobe the clock forward, we can get another token, since
            # we are refilling at one per second
            assert_that(how_long, is_(1.0))
            fudge_time.returns(1)

        fudge_sleep._callable.call_replacement = _sleep
        # Sleep gets called, strobes the clock, and we move forward
        assert_that(bucket.wait_for_token(), is_true())
    def test_Tag(self):
        t = Tag()

        assert_that(t.fromUnicode(u"HI"), is_(u'hi'))
        assert_that(t.constraint(u"oh hi"), is_false())
Beispiel #8
0
 def test_is_valid_ntiid_string(self):
     assert_that(is_valid_ntiid_string(None), is_false())
 def test_isSyntheticKey(self):
     assert_that(isSyntheticKey('OID'), is_true())
     assert_that(isSyntheticKey('key'), is_false())
Beispiel #10
0
 def test_ext_accept_external_id_false(self):
     inst = self._makeOne()
     assert_that(inst._ext_accept_external_id(self, self), is_false())
Beispiel #11
0
 def test_isBroken_no_obj(self):
     assert_that(isBroken(None, None), is_false())
     # WTF
     assert_that(isBroken(None, 'foo'), is_true())