Skip to content

Commit e871264

Browse files
committed
[trio.from_thread]
Migrate all uses of `BlockingTrioPortal` in `test_thread.py` to use `trio.from_thread.*` API instead.
1 parent e1da9d2 commit e871264

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

trio/tests/test_threads.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
async def test_do_in_trio_thread():
1818
trio_thread = threading.current_thread()
1919

20-
async def check_case(do_in_trio_thread, fn, expected):
20+
async def check_case(do_in_trio_thread, fn, expected, trio_token=None):
2121
record = []
2222

2323
def threadfn():
2424
try:
2525
record.append(("start", threading.current_thread()))
26-
x = do_in_trio_thread(fn, record)
26+
x = do_in_trio_thread(fn, record, trio_token=trio_token)
2727
record.append(("got", x))
2828
except BaseException as exc:
2929
print(exc)
@@ -38,63 +38,48 @@ def threadfn():
3838
("start", child_thread), ("f", trio_thread), expected
3939
]
4040

41-
portal = BlockingTrioPortal()
41+
token = _core.current_trio_token()
4242

4343
def f(record):
4444
assert not _core.currently_ki_protected()
4545
record.append(("f", threading.current_thread()))
4646
return 2
4747

48-
await check_case(portal.run_sync, f, ("got", 2))
48+
await check_case(run_sync, f, ("got", 2), trio_token=token)
4949

5050
def f(record):
5151
assert not _core.currently_ki_protected()
5252
record.append(("f", threading.current_thread()))
5353
raise ValueError
5454

55-
await check_case(portal.run_sync, f, ("error", ValueError))
55+
await check_case(run_sync, f, ("error", ValueError), trio_token=token)
5656

5757
async def f(record):
5858
assert not _core.currently_ki_protected()
5959
await _core.checkpoint()
6060
record.append(("f", threading.current_thread()))
6161
return 3
6262

63-
await check_case(portal.run, f, ("got", 3))
63+
await check_case(run, f, ("got", 3), trio_token=token)
6464

6565
async def f(record):
6666
assert not _core.currently_ki_protected()
6767
await _core.checkpoint()
6868
record.append(("f", threading.current_thread()))
6969
raise KeyError
7070

71-
await check_case(portal.run, f, ("error", KeyError))
71+
await check_case(run, f, ("error", KeyError), trio_token=token)
7272

7373

7474
async def test_do_in_trio_thread_from_trio_thread():
75-
portal = BlockingTrioPortal()
76-
7775
with pytest.raises(RuntimeError):
78-
portal.run_sync(lambda: None) # pragma: no branch
76+
run_sync(lambda: None) # pragma: no branch
7977

8078
async def foo(): # pragma: no cover
8179
pass
8280

8381
with pytest.raises(RuntimeError):
84-
portal.run(foo)
85-
86-
87-
async def test_BlockingTrioPortal_with_explicit_TrioToken():
88-
token = _core.current_trio_token()
89-
90-
def worker_thread(token):
91-
with pytest.raises(RuntimeError):
92-
BlockingTrioPortal()
93-
portal = BlockingTrioPortal(token)
94-
return portal.run_sync(threading.current_thread)
95-
96-
t = await run_sync_in_thread(worker_thread, token)
97-
assert t == threading.current_thread()
82+
run(foo)
9883

9984

10085
def test_run_in_trio_thread_ki():
@@ -103,7 +88,7 @@ def test_run_in_trio_thread_ki():
10388
record = set()
10489

10590
async def check_run_in_trio_thread():
106-
portal = BlockingTrioPortal()
91+
token = _core.current_trio_token()
10792

10893
def trio_thread_fn():
10994
print("in Trio thread")
@@ -121,12 +106,12 @@ async def trio_thread_afn():
121106
def external_thread_fn():
122107
try:
123108
print("running")
124-
portal.run_sync(trio_thread_fn)
109+
run_sync(trio_thread_fn, trio_token=token)
125110
except KeyboardInterrupt:
126111
print("ok1")
127112
record.add("ok1")
128113
try:
129-
portal.run(trio_thread_afn)
114+
run(trio_thread_afn, trio_token=token)
130115
except KeyboardInterrupt:
131116
print("ok2")
132117
record.add("ok2")
@@ -153,15 +138,15 @@ async def trio_fn():
153138
ev.set()
154139
await _core.wait_task_rescheduled(lambda _: _core.Abort.SUCCEEDED)
155140

156-
def thread_fn(portal):
141+
def thread_fn(token):
157142
try:
158-
portal.run(trio_fn)
143+
run(trio_fn, trio_token=token)
159144
except _core.Cancelled:
160145
record.append("cancelled")
161146

162147
async def main():
163-
portal = BlockingTrioPortal()
164-
thread = threading.Thread(target=thread_fn, args=(portal,))
148+
token = _core.current_trio_token()
149+
thread = threading.Thread(target=thread_fn, args=(token,))
165150
thread.start()
166151
await ev.wait()
167152
assert record == ["sleeping"]
@@ -323,11 +308,11 @@ class state:
323308
state.running = 0
324309
state.parked = 0
325310

326-
portal = BlockingTrioPortal()
311+
token = _core.current_trio_token()
327312

328313
def thread_fn(cancel_scope):
329314
print("thread_fn start")
330-
portal.run_sync(cancel_scope.cancel)
315+
run_sync(cancel_scope.cancel, trio_token=token)
331316
with lock:
332317
state.ran += 1
333318
state.running += 1

0 commit comments

Comments
 (0)