@@ -463,10 +463,8 @@ def bad_start(self):
463
463
async def test_trio_run_sync_in_thread_token ():
464
464
# Test that run_sync_in_thread automatically injects the current trio token
465
465
# into a spawned thread
466
-
467
466
def thread_fn ():
468
- current_thread = threading .current_thread ()
469
- callee_token = getattr (current_thread , 'current_trio_token' )
467
+ callee_token = run_sync (_core .current_trio_token )
470
468
return callee_token
471
469
472
470
caller_token = _core .current_trio_token ()
@@ -478,25 +476,28 @@ async def test_trio_from_thread_run_sync():
478
476
# Test that run_sync_in_thread correctly "hands off" the trio token to
479
477
# trio.from_thread.run_sync()
480
478
def thread_fn ():
481
- start = run_sync (_core .current_time )
482
- end = run_sync (_core .current_time )
483
- return end - start
479
+ trio_time = run_sync (_core .current_time )
480
+ return trio_time
484
481
485
- duration = await run_sync_in_thread (thread_fn )
486
- assert duration > 0
482
+ trio_time = await run_sync_in_thread (thread_fn )
483
+ assert isinstance ( trio_time , float )
487
484
488
485
489
486
async def test_trio_from_thread_run ():
490
487
# Test that run_sync_in_thread correctly "hands off" the trio token to
491
488
# trio.from_thread.run()
489
+ record = []
490
+
491
+ async def back_in_trio_fn ():
492
+ _core .current_time () # implicitly checks that we're in trio
493
+ record .append ("back in trio" )
494
+
492
495
def thread_fn ():
493
- start = time .perf_counter ()
494
- run (sleep , 0.05 )
495
- end = time .perf_counter ()
496
- return end - start
496
+ record .append ("in thread" )
497
+ run (back_in_trio_fn )
497
498
498
- duration = await run_sync_in_thread (thread_fn )
499
- assert duration > 0
499
+ await run_sync_in_thread (thread_fn )
500
+ assert record == [ "in thread" , "back in trio" ]
500
501
501
502
502
503
async def test_trio_from_thread_token ():
@@ -523,23 +524,9 @@ def thread_fn(token):
523
524
assert callee_token == caller_token
524
525
525
526
526
- async def test_trio_from_thread_both_run ():
527
- # Test that trio.from_thread.run() and from_thread.run_sync() can run in
528
- # the same thread together
529
-
530
- def thread_fn ():
531
- start = run_sync (_core .current_time )
532
- run (sleep , 0.05 )
533
- end = run_sync (_core .current_time )
534
- return end - start
535
-
536
- duration = await run_sync_in_thread (thread_fn )
537
- assert duration > 0
538
-
539
-
540
- async def test_trio_from_thread_raw_call ():
527
+ async def test_from_thread_no_token ():
541
528
# Test that a "raw call" to trio.from_thread.run() fails because no token
542
529
# has been provided
543
530
544
- with pytest .raises (AttributeError ):
531
+ with pytest .raises (RuntimeError ):
545
532
run_sync (_core .current_time )
0 commit comments