コード例 #1
0
    def test_can_get_value_if_material_girl_not_run(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material("test", lambda: "woot")

        value = girl.get("test")
        expect(value).to_equal("woot")
コード例 #2
0
    def test_can_add_material_with_expiration_and_graceperiod(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material("test", lambda: "woot", expiration=2, grace_period=4)

        girl.run()

        expect(storage.items).to_include("test")
        expect(storage.items["test"]).to_equal("woot")
コード例 #3
0
    def test_can_add_material(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material("test", lambda: "woot")

        girl.run()

        expect(storage.items).to_include("test")
        expect(storage.items["test"]).to_equal("woot")
コード例 #4
0
    def test_can_lock_key_with_timeout(self):
        storage = Mock(store=Mock(), acquire_lock=Mock())

        girl = Materializer(storage=storage)
        girl.add_material("test1", lambda: "woot", lock_timeout=1)
        girl.add_material("test2", lambda: "woot", lock_timeout=2)

        girl.run()

        expect(storage.store.call_count).to_equal(2)
        expect(storage.acquire_lock.call_count).to_equal(2)
        storage.acquire_lock.assert_has_calls([call("test1", timeout=1), call("test2", timeout=2)])
コード例 #5
0
    def test_raises_if_key_not_found(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        try:
            value = girl.get('test')
        except ValueError:
            err = sys.exc_info()[1]
            expect(err).to_have_an_error_message_of(
                'Key test not found in materials. Maybe you forgot to call "add_material" for this key?'
            )
        else:
            assert False, "Should not have gotten this far"
コード例 #6
0
    def test_can_add_material(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material(
            'test',
            lambda: 'woot'
        )

        girl.run()

        expect(storage.items).to_include('test')
        expect(storage.items['test']).to_equal('woot')
コード例 #7
0
    def test_can_skip_locked_key(self, logging_info_mock):
        storage = Mock(store=Mock(), acquire_lock=Mock(return_value=None), release_lock=Mock())

        girl = Materializer(storage=storage)

        girl.add_material("test1", lambda: "woot1")
        girl.add_material("test2", lambda: "woot2")

        girl.run()

        expect(storage.store.call_count).to_equal(0)
        expect(storage.acquire_lock.call_count).to_equal(2)
        expect(storage.release_lock.call_count).to_equal(0)
        expect(logging_info_mock.call_count).to_equal(4)
コード例 #8
0
    def configure_material_girl(self):
        from holmes.material import configure_materials

        host = self.config.get('MATERIAL_GIRL_REDISHOST')
        port = self.config.get('MATERIAL_GIRL_REDISPORT')

        self.redis_material = redis.StrictRedis(host=host, port=port, db=0)

        self.application.girl = Materializer(
            storage=RedisStorage(redis=self.redis_material),
            load_on_cachemiss=self.config.get(
                'MATERIAL_GIRL_LOAD_ON_CACHEMISS', True))

        configure_materials(self.application.girl, self.application.db,
                            self.config)
コード例 #9
0
    def test_can_expire_materials(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material("test", lambda: "woot")

        girl.run()

        expect(storage.items).to_length(1)
        expect(storage.items["test"]).to_equal("woot")

        girl.expire("test")

        expect(girl.is_expired("test")).to_be_true()

        expect(storage.items).to_length(1)
        expect(storage.items.get("test")).to_be_null()
        expect(storage.items["_expired_test"]).to_equal("woot")
コード例 #10
0
    def test_can_miss_the_cache(self):
        storage = Mock(retrieve=Mock(return_value=None))

        girl = Materializer(storage=storage, load_on_cachemiss=False)
        girl.add_material("test", lambda: "woot")

        girl.run()
        value = girl.get("test")

        expect(value).to_be_null()
        expect(storage.acquire_lock.call_count).to_equal(1)
        storage.store.assert_called_once_with("test", "woot", expiration=10, grace_period=0)
コード例 #11
0
    def test_does_not_add_not_expired_materials(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material(
            'test',
            lambda: 'woot'
        )

        girl.run()

        storage.items = {}

        girl.run()

        expect(storage.items).to_be_empty()
コード例 #12
0
    def test_can_get_value_after_material_girl_run(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        girl.add_material(
            'test',
            lambda: 'woot'
        )

        girl.run()

        value = girl.get('test')
        expect(value).to_equal('woot')
コード例 #13
0
    def test_dont_update_not_expired_materials(self):
        storage = InMemoryStorage()
        girl = Materializer(storage=storage)

        woots = self.woots_generator()

        girl.add_material("test", lambda: next(woots))

        girl.run()

        expect(storage.items).to_length(1)
        expect(storage.items["test"]).to_equal("woot1")

        girl.run()

        expect(storage.items).to_length(1)
        expect(storage.items["test"]).to_equal("woot1")
コード例 #14
0
    def configure_material_girl(self):
        from holmes.material import configure_materials
        self.girl = Materializer(storage=RedisStorage(
            redis=self.redis_material))

        configure_materials(self.girl, self.db, self.config)