17
17
async def test_do_in_trio_thread ():
18
18
trio_thread = threading .current_thread ()
19
19
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 ):
21
21
record = []
22
22
23
23
def threadfn ():
24
24
try :
25
25
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 )
27
27
record .append (("got" , x ))
28
28
except BaseException as exc :
29
29
print (exc )
@@ -38,63 +38,48 @@ def threadfn():
38
38
("start" , child_thread ), ("f" , trio_thread ), expected
39
39
]
40
40
41
- portal = BlockingTrioPortal ()
41
+ token = _core . current_trio_token ()
42
42
43
43
def f (record ):
44
44
assert not _core .currently_ki_protected ()
45
45
record .append (("f" , threading .current_thread ()))
46
46
return 2
47
47
48
- await check_case (portal . run_sync , f , ("got" , 2 ))
48
+ await check_case (run_sync , f , ("got" , 2 ), trio_token = token )
49
49
50
50
def f (record ):
51
51
assert not _core .currently_ki_protected ()
52
52
record .append (("f" , threading .current_thread ()))
53
53
raise ValueError
54
54
55
- await check_case (portal . run_sync , f , ("error" , ValueError ))
55
+ await check_case (run_sync , f , ("error" , ValueError ), trio_token = token )
56
56
57
57
async def f (record ):
58
58
assert not _core .currently_ki_protected ()
59
59
await _core .checkpoint ()
60
60
record .append (("f" , threading .current_thread ()))
61
61
return 3
62
62
63
- await check_case (portal . run , f , ("got" , 3 ))
63
+ await check_case (run , f , ("got" , 3 ), trio_token = token )
64
64
65
65
async def f (record ):
66
66
assert not _core .currently_ki_protected ()
67
67
await _core .checkpoint ()
68
68
record .append (("f" , threading .current_thread ()))
69
69
raise KeyError
70
70
71
- await check_case (portal . run , f , ("error" , KeyError ))
71
+ await check_case (run , f , ("error" , KeyError ), trio_token = token )
72
72
73
73
74
74
async def test_do_in_trio_thread_from_trio_thread ():
75
- portal = BlockingTrioPortal ()
76
-
77
75
with pytest .raises (RuntimeError ):
78
- portal . run_sync (lambda : None ) # pragma: no branch
76
+ run_sync (lambda : None ) # pragma: no branch
79
77
80
78
async def foo (): # pragma: no cover
81
79
pass
82
80
83
81
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 )
98
83
99
84
100
85
def test_run_in_trio_thread_ki ():
@@ -103,7 +88,7 @@ def test_run_in_trio_thread_ki():
103
88
record = set ()
104
89
105
90
async def check_run_in_trio_thread ():
106
- portal = BlockingTrioPortal ()
91
+ token = _core . current_trio_token ()
107
92
108
93
def trio_thread_fn ():
109
94
print ("in Trio thread" )
@@ -121,12 +106,12 @@ async def trio_thread_afn():
121
106
def external_thread_fn ():
122
107
try :
123
108
print ("running" )
124
- portal . run_sync (trio_thread_fn )
109
+ run_sync (trio_thread_fn , trio_token = token )
125
110
except KeyboardInterrupt :
126
111
print ("ok1" )
127
112
record .add ("ok1" )
128
113
try :
129
- portal . run (trio_thread_afn )
114
+ run (trio_thread_afn , trio_token = token )
130
115
except KeyboardInterrupt :
131
116
print ("ok2" )
132
117
record .add ("ok2" )
@@ -153,15 +138,15 @@ async def trio_fn():
153
138
ev .set ()
154
139
await _core .wait_task_rescheduled (lambda _ : _core .Abort .SUCCEEDED )
155
140
156
- def thread_fn (portal ):
141
+ def thread_fn (token ):
157
142
try :
158
- portal . run (trio_fn )
143
+ run (trio_fn , trio_token = token )
159
144
except _core .Cancelled :
160
145
record .append ("cancelled" )
161
146
162
147
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 ,))
165
150
thread .start ()
166
151
await ev .wait ()
167
152
assert record == ["sleeping" ]
@@ -323,11 +308,11 @@ class state:
323
308
state .running = 0
324
309
state .parked = 0
325
310
326
- portal = BlockingTrioPortal ()
311
+ token = _core . current_trio_token ()
327
312
328
313
def thread_fn (cancel_scope ):
329
314
print ("thread_fn start" )
330
- portal . run_sync (cancel_scope .cancel )
315
+ run_sync (cancel_scope .cancel , trio_token = token )
331
316
with lock :
332
317
state .ran += 1
333
318
state .running += 1
0 commit comments