11import { expect , test } from 'vitest' ;
22import { createRequestExecutionLocks } from '../request-execution-locks.ts' ;
3+ import type { RequestExecutionLockKey } from '../request-binding.ts' ;
34
45test ( 'a dynamically retained device lock lasts until the owning request completes' , async ( ) => {
56 const locks = new Map < string , Promise < unknown > > ( ) ;
@@ -54,14 +55,57 @@ test('retaining an initial device lock is reentrant within the request', async (
5455 ) . resolves . toBe ( 'done' ) ;
5556} ) ;
5657
57- test ( 'a request holding an app device lock is refused its host lock instead of deadlocking' , async ( ) => {
58+ /**
59+ * Runs two requests that each hold their initial locks, lets both start, then has each retain one
60+ * more device lock. Returns what each retention settled to: `retained` or the refusal.
61+ */
62+ async function crossRetentions (
63+ first : { keys : RequestExecutionLockKey [ ] ; retain : string } ,
64+ second : { keys : RequestExecutionLockKey [ ] ; retain : string } ,
65+ ) : Promise < unknown [ ] > {
66+ const locks = new Map < string , Promise < unknown > > ( ) ;
67+ let started = 0 ;
68+ let markBothStarted : ( ) => void = ( ) => { } ;
69+ const bothStarted = new Promise < void > ( ( resolve ) => {
70+ markBothStarted = resolve ;
71+ } ) ;
72+ const run = async ( request : { keys : RequestExecutionLockKey [ ] ; retain : string } ) => {
73+ const requestLocks = createRequestExecutionLocks ( { locks, initialKeys : request . keys } ) ;
74+ return await requestLocks . run ( async ( ) => {
75+ started += 1 ;
76+ if ( started === 2 ) markBothStarted ( ) ;
77+ await bothStarted ;
78+ return await requestLocks . retainDevice ( request . retain ) . then (
79+ ( ) => 'retained' ,
80+ ( error : unknown ) => error ,
81+ ) ;
82+ } ) ;
83+ } ;
84+ return await Promise . all ( [ run ( first ) , run ( second ) ] ) ;
85+ }
86+
87+ const BUSY = {
88+ code : 'DEVICE_IN_USE' ,
89+ details : { reason : 'DEVICE_EXECUTION_LOCK_BUSY' } ,
90+ } ;
91+
92+ test ( 'replays on two apps of one Mac that each open the other app are refused, not deadlocked' , async ( ) => {
93+ // Each replay runs in its existing app session, closes its app, and opens the other one.
94+ const outcomes = await crossRetentions (
95+ { keys : [ 'session:one' , 'device:mac:app:com.example.one' ] , retain : 'mac:app:com.example.two' } ,
96+ { keys : [ 'session:two' , 'device:mac:app:com.example.two' ] , retain : 'mac:app:com.example.one' } ,
97+ ) ;
98+ expect ( outcomes ) . toHaveLength ( 2 ) ;
99+ for ( const outcome of outcomes ) expect ( outcome ) . toMatchObject ( BUSY ) ;
100+ } ) ;
101+
102+ test ( 'a replay that leaves its app for the whole Mac is refused while another request holds the Mac' , async ( ) => {
58103 const locks = new Map < string , Promise < unknown > > ( ) ;
59- // A replay in an existing app session holds that app device until it finishes.
60104 const replayLocks = createRequestExecutionLocks ( {
61105 locks,
62106 initialKeys : [ 'session:replay' , 'device:mac:app:com.example.one' ] ,
63107 } ) ;
64- // A fresh open of the same app in another session takes the host first, then waits for the app.
108+ // A fresh open of the same app in another session holds the Mac and waits for the app.
65109 const freshOpenLocks = createRequestExecutionLocks ( {
66110 locks,
67111 initialKeys : [ 'session:fresh' , 'device:mac' , 'device:mac:app:com.example.one' ] ,
@@ -79,14 +123,10 @@ test('a request holding an app device lock is refused its host lock instead of d
79123 const replayRun = replayLocks . run ( async ( ) => {
80124 replayHolding ( ) ;
81125 await freshWaiting ;
82- // The replay closes its app session and opens the whole Mac.
83- const refused = await replayLocks . retainDevice ( 'mac' ) . then (
84- ( ) => undefined ,
126+ return await replayLocks . retainDevice ( 'mac' ) . then (
127+ ( ) => 'retained' ,
85128 ( error : unknown ) => error ,
86129 ) ;
87- // Another app device of the Mac keeps the host-then-app order and is retained as before.
88- await replayLocks . retainDevice ( 'mac:app:com.example.two' ) ;
89- return refused ;
90130 } ) ;
91131 await holding ;
92132 const freshRun = freshOpenLocks . run ( async ( ) => {
@@ -96,10 +136,41 @@ test('a request holding an app device lock is refused its host lock instead of d
96136 expect ( freshEntered ) . toBe ( false ) ;
97137 continueReplay ( ) ;
98138
99- expect ( await replayRun ) . toMatchObject ( {
100- code : 'UNSUPPORTED_OPERATION' ,
101- details : { reason : 'host-after-app-device-lock' , deviceId : 'mac' } ,
102- } ) ;
139+ expect ( await replayRun ) . toMatchObject ( { ...BUSY , details : { ...BUSY . details , deviceId : 'mac' } } ) ;
103140 await freshRun ;
104141 expect ( freshEntered ) . toBe ( true ) ;
105142} ) ;
143+
144+ test ( 'a second lock on the same Mac is taken while it is free, and other devices still wait' , async ( ) => {
145+ const locks = new Map < string , Promise < unknown > > ( ) ;
146+ const replayLocks = createRequestExecutionLocks ( {
147+ locks,
148+ initialKeys : [ 'session:replay' , 'device:mac:app:com.example.one' ] ,
149+ } ) ;
150+ await expect (
151+ replayLocks . run ( async ( ) => {
152+ await replayLocks . retainDevice ( 'mac:app:com.example.two' ) ;
153+ await replayLocks . retainDevice ( 'mac' ) ;
154+ return 'done' ;
155+ } ) ,
156+ ) . resolves . toBe ( 'done' ) ;
157+
158+ // A lock on another device is still waited for, as before.
159+ const holder = createRequestExecutionLocks ( { locks, initialKeys : [ 'device:SIM-002' ] } ) ;
160+ let releaseHolder : ( ) => void = ( ) => { } ;
161+ const holderRun = holder . run (
162+ async ( ) =>
163+ await new Promise < void > ( ( resolve ) => {
164+ releaseHolder = resolve ;
165+ } ) ,
166+ ) ;
167+ const simLocks = createRequestExecutionLocks ( { locks, initialKeys : [ 'device:SIM-001' ] } ) ;
168+ const simRun = simLocks . run ( async ( ) => {
169+ await simLocks . retainDevice ( 'SIM-002' ) ;
170+ return 'waited' ;
171+ } ) ;
172+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
173+ releaseHolder ( ) ;
174+ await holderRun ;
175+ await expect ( simRun ) . resolves . toBe ( 'waited' ) ;
176+ } ) ;
0 commit comments