Beispiel #1
0
def test_name_doesnt_clash(name):
    stack1 = ContextStack(name)
    stack2 = ContextStack(name + 'xxx')

    stack1.push(a=1)
    stack2.push(a=2)

    assert stack1['a'] == 1
    assert stack2['a'] == 2
Beispiel #2
0
def test_name_doesnt_clash(name):
    stack1 = ContextStack(name)
    stack2 = ContextStack(name + 'xxx')

    stack1.push(a=1)
    stack2.push(a=2)

    assert stack1['a'] == 1
    assert stack2['a'] == 2
Beispiel #3
0
def test_context_clear_resets_stack(name):
    stack = ContextStack(name)
    stack.push(a=1)
    assert stack._stack == [{'a': 1}]
    assert stack.flat == {'a': 1}

    clear()

    assert stack._stack == []
    assert stack.flat == {}
Beispiel #4
0
def test_context_clear_resets_stack(name):
    stack = ContextStack(name)
    stack.push(a=1)
    assert stack._stack == [{'a': 1}]
    assert stack.flat == {'a': 1}

    clear()

    assert stack._stack == []
    assert stack.flat == {}
Beispiel #5
0
def test_does_not_use_or_modify_dict():
    stack = ContextStack()

    d = {'a': 1}
    stack.push(d, b=2)
    assert stack['a'] == 1
    assert stack['b'] == 2
    assert d == {'a': 1}

    d['a'] = 2
    assert stack['a'] == 1
Beispiel #6
0
def test_stack_context_manager():
    stack = ContextStack()

    stack.push(a=1)

    assert list(stack.items()) == [('a', 1)]

    with stack(b=2):
        assert list(stack.items()) == [('b', 2), ('a', 1)]

    assert list(stack.items()) == [('a', 1)]
Beispiel #7
0
def test_stack_context(name):
    stack = ContextStack(name)

    stack.push(a=1)

    assert list(stack.items()) == [('a', 1)]

    with stack(b=2):
        assert list(stack.items()) == [('b', 2), ('a', 1)]

    assert list(stack.items()) == [('a', 1)]
Beispiel #8
0
def test_does_not_use_or_modify_dict(name):
    stack = ContextStack(name)

    d = {'a': 1}
    stack.push(d, b=2)
    assert stack['a'] == 1
    assert stack['b'] == 2
    assert d == {'a': 1}

    d['a'] = 2
    assert stack['a'] == 1
Beispiel #9
0
def test_stack_clear(name):
    stack = ContextStack(name)

    stack.push(a=1)
    stack.push(b=2)
    stack.push(c=3)

    assert list(stack.items()) == [('c', 3), ('b', 2), ('a', 1)]

    stack.clear()

    assert list(stack.items()) == []
Beispiel #10
0
def test_stack_clear(name):
    stack = ContextStack(name)

    stack.push(a=1)
    stack.push(b=2)
    stack.push(c=3)

    assert list(stack.items()) == [('c', 3), ('b', 2), ('a', 1)]

    stack.clear()

    assert list(stack.items()) == []
Beispiel #11
0
def test_stack_unwind():
    stack = ContextStack()

    stack.push(a=1)
    assert stack['a'] == 1

    level = stack.push(a=2)
    assert stack['a'] == 2

    stack.push(a=3)
    stack.push(a=4)
    assert stack['a'] == 4

    stack.unwind(level)
    assert stack['a'] == 1
Beispiel #12
0
def test_stack_unwind(name):
    stack = ContextStack(name)

    stack.push(a=1)
    assert stack['a'] == 1

    level = stack.push(a=2)
    assert stack['a'] == 2

    stack.push(a=3)
    stack.push(a=4)
    assert stack['a'] == 4

    stack.unwind(level)
    assert stack['a'] == 1
Beispiel #13
0
def test_stack_basic():
    stack = ContextStack()

    stack.push(a=1)
    assert stack['a'] == 1
    assert list(stack.items()) == [('a', 1)]

    stack.push(b=2)
    assert stack['b'] == 2
    assert list(stack.items()) == [('b', 2), ('a', 1)]

    stack.push(a=3)
    assert stack['a'] == 3
    assert list(stack.items()) == [('a', 3), ('b', 2)]

    stack.pop()
    assert stack['a'] == 1
    assert list(stack.items()) == [('b', 2), ('a', 1)]

    stack.pop()
    assert stack['a'] == 1
    assert list(stack.items()) == [('a', 1)]

    stack.pop()
    with pytest.raises(KeyError):
        stack['a']
    assert list(stack.items()) == []
Beispiel #14
0
def test_stack_basic(name):
    stack = ContextStack(name)

    stack.push(a=1)
    assert stack['a'] == 1
    assert list(stack.items()) == [('a', 1)]

    stack.push(b=2)
    assert stack['b'] == 2
    assert list(stack.items()) == [('b', 2), ('a', 1)]

    stack.push(a=3)
    assert stack['a'] == 3
    assert list(stack.items()) == [('a', 3), ('b', 2)]

    stack.pop()
    assert stack['a'] == 1
    assert list(stack.items()) == [('b', 2), ('a', 1)]

    stack.pop()
    assert stack['a'] == 1
    assert list(stack.items()) == [('a', 1)]

    stack.pop()
    with pytest.raises(KeyError):
        stack['a']
    assert list(stack.items()) == []
Beispiel #15
0
def test_concurrent(name):
    stack = ContextStack(name)

    result = []

    e1 = threading.Event()
    e2 = threading.Event()

    def worker():
        stack.push(a=2)
        result.append(stack.flat)
        e1.set()
        e2.wait()
        stack.clear()
        result.append(stack.flat)
        e1.set()

    t = threading.Thread(target=worker)

    stack.push(a=1)
    t.start()

    e1.wait()
    e1.clear()

    # we should now have 2 different thread locals
    assert stack.flat == {'a': 1}
    assert result[-1] == {'a': 2}

    e2.set()
    e1.wait()

    assert stack.flat == {'a': 1}
    assert result[-1] == {}

    t.join()
Beispiel #16
0
def test_concurrent(name):
    stack = ContextStack(name)

    result = []

    e1 = threading.Event()
    e2 = threading.Event()

    def worker():
        stack.push(a=2)
        result.append(stack.flat)
        e1.set()
        e2.wait()
        stack.clear()
        result.append(stack.flat)
        e1.set()

    t = threading.Thread(target=worker)

    stack.push(a=1)
    t.start()

    e1.wait()
    e1.clear()

    # we should now have 2 different thread locals
    assert stack.flat == {'a': 1}
    assert result[-1] == {'a': 2}

    e2.set()
    e1.wait()

    assert stack.flat == {'a': 1}
    assert result[-1] == {}

    t.join()