示例#1
0
def test_name():
    name_list = []
    NameManager.current = NameManager()
    NameManager.current.get(None, "main_thread")
    name_list.append(NameManager.current)

    def f():
        NameManager.current = NameManager()
        NameManager.current.get(None, "spawned_thread")
        name_list.append(NameManager.current)

    thread = threading.Thread(target=f)
    thread.start()
    thread.join()
    assert "main_thread" in name_list[
        0]._counter, "cannot find the string `main thread` in name_list[0]._counter"
    assert "spawned_thread" in name_list[
        1]._counter, "cannot find the string `spawned thread` in name_list[1]._counter"

    event = threading.Event()
    status = [False]

    def g():
        with NameManager():
            if "main_thread" not in NameManager.current._counter:
                status[0] = True

    thread = threading.Thread(target=g)
    thread.start()
    NameManager.current = NameManager()
    NameManager.current.get(None, "main_thread")
    event.set()
    thread.join()
    event.clear()
    assert status[0], "Spawned thread isn't using thread local NameManager"
示例#2
0
 def g():
     with NameManager():
         if "main_thread" not in NameManager.current._counter:
             status[0] = True
示例#3
0
 def f():
     NameManager.current = NameManager()
     NameManager.current.get(None, "spawned_thread")
     name_list.append(NameManager.current)