Exemplo n.º 1
0
    def testConfigContext(self):
        with self.assertRaises(AttributeError):
            _ = options.a.b.c

        options.register_option('c.d.e', 'a', is_string)

        try:
            with option_context() as ctx:
                ctx.register_option('a.b.c', 1, validator=is_integer)
                self.assertEqual(ctx.a.b.c, 1)

                ctx.a.b.c = 2
                self.assertEqual(ctx.a.b.c, 2)

                with self.assertRaises(ValueError):
                    ctx.a.b.c = 'a'

                self.assertEqual(ctx.c.d.e, 'a')

                ctx.c.d.e = 'b'

            self.assertEqual(options.c.d.e, 'a')

            options.c.d.e = 'c'

            self.assertEqual(options.c.d.e, 'c')

            with self.assertRaises(AttributeError):
                _ = options.a.b.c  # noqa: F841
        finally:
            options.unregister_option('c.d.e')
Exemplo n.º 2
0
    def testMultiThreadConfig(self):
        options.register_option('a.b.c', 1)
        assert_equal = self.assertEqual

        class T(threading.Thread):
            def __init__(self, is_first, condition):
                super(T, self).__init__()
                self.is_first = is_first
                self.condition = condition

            def run(self):
                self.condition.acquire()
                if self.is_first:
                    options.a.b.c = 2
                    self.condition.notify()
                else:
                    self.condition.wait()
                    assert_equal(options.a.b.c, 1)
                self.condition.release()

        try:
            cond = threading.Condition()
            a = T(True, cond)
            b = T(False, cond)
            b.start()
            a.start()
            a.join()
            b.join()
        finally:
            options.unregister_option('a.b.c')
Exemplo n.º 3
0
def test_config_context():
    with pytest.raises(AttributeError):
        _ = options.a.b.c

    options.register_option('c.d.e', 'a', is_string)
    assert 'c' in dir(options)
    assert 'd' in dir(options.c)

    try:
        with option_context() as ctx:
            ctx.register_option('a.b.c', 1, validator=is_integer)
            assert ctx.a.b.c == 1

            ctx.a.b.c = 2
            assert ctx.a.b.c == 2

            with pytest.raises(ValueError):
                ctx.a.b.c = 'a'

            assert ctx.c.d.e == 'a'

            ctx.c.d.e = 'b'

        assert options.c.d.e == 'a'

        options.c.d.e = 'c'

        assert options.c.d.e == 'c'

        with pytest.raises(AttributeError):
            _ = options.a.b.c  # noqa: F841
    finally:
        options.unregister_option('c.d.e')