Exemplo n.º 1
0
    def test_basic_local(self):
        l = local.Local()
        l.foo = 0
        values = []

        def value_setter(idx):
            time.sleep(0.01 * idx)
            l.foo = idx
            time.sleep(0.02)
            values.append(l.foo)

        threads = [Thread(target=value_setter, args=(x, )) for x in [1, 2, 3]]
        for thread in threads:
            thread.start()
        time.sleep(0.2)
        self.assertEqual(sorted(values), [1, 2, 3])

        def delfoo():
            del l.foo

        delfoo()
        with self.assertRaises(AttributeError):
            l.foo()
        with self.assertRaises(AttributeError):
            delfoo()

        local.release_local(l)
Exemplo n.º 2
0
    def test_basic_local(self):
        l = local.Local()
        l.foo = 0
        values = []

        def value_setter(idx):
            time.sleep(0.01 * idx)
            l.foo = idx
            time.sleep(0.02)
            values.append(l.foo)

        threads = [Thread(target=value_setter, args=(x,)) for x in [1, 2, 3]]
        for thread in threads:
            thread.start()
        time.sleep(0.2)
        self.assertEqual(sorted(values), [1, 2, 3])

        def delfoo():
            del l.foo

        delfoo()
        with self.assertRaises(AttributeError):
            l.foo()
        with self.assertRaises(AttributeError):
            delfoo()

        local.release_local(l)
Exemplo n.º 3
0
    def test_local_release(self):
        loc = local.Local()
        loc.foo = 42
        local.release_local(loc)
        self.assertFalse(hasattr(loc, 'foo'))

        ls = local.LocalStack()
        ls.push(42)
        local.release_local(ls)
        self.assertIsNone(ls.top)
Exemplo n.º 4
0
    def test_local_release(self):
        loc = local.Local()
        loc.foo = 42
        local.release_local(loc)
        self.assertFalse(hasattr(loc, "foo"))

        ls = local.LocalStack()
        ls.push(42)
        local.release_local(ls)
        self.assertIsNone(ls.top)