Skip to content

Commit eee53aa

Browse files
committed
harden the startSynchronously.swift test a bit
1 parent bb62e96 commit eee53aa

File tree

1 file changed

+32
-68
lines changed

1 file changed

+32
-68
lines changed

test/Concurrency/Runtime/startSynchronously.swift

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ syncOnNonTaskThread(synchronousTask: behavior)
294294
// CHECK: after startSynchronously, outside; cancel (wakeup) the synchronous task! [thread:[[CALLING_THREAD3]]]
295295

296296
print("\n\n==== ------------------------------------------------------------------")
297-
print("callActorFromStartSynchronousTask()")
297+
print("callActorFromStartSynchronousTask() - not on specific queue")
298298
callActorFromStartSynchronousTask(recipient: .recipient(Recipient()))
299299

300300
// CHECK: callActorFromStartSynchronousTask()
@@ -308,11 +308,6 @@ callActorFromStartSynchronousTask(recipient: .recipient(Recipient()))
308308
// CHECK-NOT: ERROR!
309309
// CHECK: inside startSynchronously, call rec.sync() done
310310

311-
// CHECK-NOT: ERROR!
312-
// CHECK: inside startSynchronously, call rec.async()
313-
// CHECK-NOT: ERROR!
314-
// CHECK: inside startSynchronously, call rec.async() done
315-
316311
// CHECK-NOT: ERROR!
317312
// CHECK: inside startSynchronously, done
318313

@@ -324,35 +319,20 @@ enum TargetActorToCall {
324319
}
325320

326321
protocol RecipientProtocol where Self: Actor {
327-
func sync(syncTaskThreadID: ThreadID) async
328-
func async(syncTaskThreadID: ThreadID) async
322+
func callAndSuspend(syncTaskThreadID: ThreadID) async
329323
}
330324

331325
// default actor, must not declare an 'unownedExecutor'
332-
actor Recipient {
333-
func sync(syncTaskThreadID: ThreadID) {
326+
actor Recipient: RecipientProtocol {
327+
func callAndSuspend(syncTaskThreadID: ThreadID) async {
334328
self.preconditionIsolated()
335329

336330
print("\(Recipient.self)/\(#function) Current actor thread id = \(getCurrentThreadID()) @ :\(#line)")
337331
if compareThreadIDs(syncTaskThreadID, .equal, getCurrentThreadID()) {
338332
print("NOTICE: Actor must not run on the synchronous task's thread :\(#line)")
339333
}
340-
}
341-
342-
func async(syncTaskThreadID: ThreadID) async {
343-
self.preconditionIsolated()
344334

345-
// Dispatch may end up reusing the thread used to service the queue so we
346-
// cannot truly assert exact thread identity in such tests.
347-
// Usually this will be on a different thread by now though.
348-
print("\(Recipient.self)/\(#function) Current actor thread id = \(getCurrentThreadID()) @ :\(#line)")
349-
if compareThreadIDs(syncTaskThreadID, .equal, getCurrentThreadID()) {
350-
print("NOTICE: Actor must not run on the synchronous task's thread :\(#line)")
351-
}
352-
353-
await Task {
354-
self.preconditionIsolated()
355-
}.value
335+
try? await Task.sleep(for: .milliseconds(100))
356336
}
357337
}
358338

@@ -379,8 +359,8 @@ func callActorFromStartSynchronousTask(recipient rec: TargetActorToCall) {
379359

380360
print("inside startSynchronously, call rec.sync() [thread:\(getCurrentThreadID())] @ :\(#line)")
381361
switch rec {
382-
case .recipient(let recipient): await recipient.sync(syncTaskThreadID: innerTID)
383-
case .recipientOnQueue(let recipient): await recipient.sync(syncTaskThreadID: innerTID)
362+
case .recipient(let recipient): await recipient.callAndSuspend(syncTaskThreadID: innerTID)
363+
case .recipientOnQueue(let recipient): await recipient.callAndSuspend(syncTaskThreadID: innerTID)
384364
}
385365
print("inside startSynchronously, call rec.sync() done [thread:\(getCurrentThreadID())] @ :\(#line)")
386366

@@ -395,22 +375,6 @@ func callActorFromStartSynchronousTask(recipient rec: TargetActorToCall) {
395375
print("NOTICE: Task resumed on same thread as it entered the synchronous task!")
396376
}
397377

398-
print("inside startSynchronously, call rec.async() [thread:\(getCurrentThreadID())] @ :\(#line)")
399-
switch rec {
400-
case .recipient(let recipient): await recipient.async(syncTaskThreadID: innerTID)
401-
case .recipientOnQueue(let recipient): await recipient.async(syncTaskThreadID: innerTID)
402-
}
403-
print("inside startSynchronously, call rec.async() done [thread:\(getCurrentThreadID())] @ :\(#line)")
404-
405-
print("Inner thread id = \(innerTID)")
406-
print("Current thread id = \(getCurrentThreadID())")
407-
// Dispatch may end up reusing the thread used to service the queue so we
408-
// cannot truly assert exact thread identity in such tests.
409-
// Usually this will be on a different thread by now though.
410-
if compareThreadIDs(innerTID, .equal, getCurrentThreadID()) {
411-
print("NOTICE: Task resumed on same thread as it entered the synchronous task!")
412-
}
413-
414378
print("inside startSynchronously, done [thread:\(getCurrentThreadID())] @ :\(#line)")
415379
sem1.signal()
416380
}
@@ -428,6 +392,28 @@ print("callActorFromStartSynchronousTask() - actor in custom executor with its o
428392
let actorQueue = DispatchQueue(label: "recipient-actor-queue")
429393
callActorFromStartSynchronousTask(recipient: .recipientOnQueue(RecipientOnQueue(queue: actorQueue)))
430394

395+
396+
// 50: callActorFromStartSynchronousTask()
397+
// 51: before startSynchronously [thread:0x00007000054f5000] @ :366
398+
// 52: inside startSynchronously [thread:0x00007000054f5000] @ :372
399+
// 53: inside startSynchronously, call rec.sync() [thread:0x00007000054f5000] @ :380
400+
// 54: Recipient/sync(syncTaskThreadID:) Current actor thread id = 0x000070000567e000 @ :336
401+
// 55: inside startSynchronously, call rec.sync() done [thread:0x000070000567e000] @ :385
402+
// 56: Inner thread id = 0x00007000054f5000
403+
// 57: Current thread id = 0x000070000567e000
404+
// 60: after startSynchronously [thread:0x00007000054f5000] @ :418
405+
// 61: - async work on queue
406+
// 62: - async work on queue
407+
// 63: - async work on queue
408+
// 64: - async work on queue
409+
// 65: - async work on queue
410+
// 67: - async work on queue
411+
// 68: - async work on queue
412+
// 69: - async work on queue
413+
// 71: Inner thread id = 0x00007000054f5000
414+
// 72: Current thread id = 0x000070000567e000
415+
// 73: inside startSynchronously, done [thread:0x000070000567e000] @ :414
416+
431417
// CHECK-LABEL: callActorFromStartSynchronousTask() - actor in custom executor with its own queue
432418
// No interleaving allowed between "before" and "inside":
433419
// CHECK: before startSynchronously [thread:[[CALLING_THREAD4:.*]]]
@@ -438,21 +424,14 @@ callActorFromStartSynchronousTask(recipient: .recipientOnQueue(RecipientOnQueue(
438424
// allowing the 'after startSynchronously' to run.
439425
//
440426
// CHECK-NEXT: inside startSynchronously, call rec.sync() [thread:[[CALLING_THREAD4]]]
441-
// CHECK: NaiveQueueExecutor(recipient-actor-queue) enqueue
442427
// CHECK: after startSynchronously
443428
// CHECK-NOT: ERROR!
444429
// CHECK: inside startSynchronously, call rec.sync() done
445430

446-
// CHECK-NOT: ERROR!
447-
// CHECK: inside startSynchronously, call rec.async()
448-
// CHECK: NaiveQueueExecutor(recipient-actor-queue) enqueue
449-
// CHECK-NOT: ERROR!
450-
// CHECK: inside startSynchronously, call rec.async() done
451-
452431
// CHECK-NOT: ERROR!
453432
// CHECK: inside startSynchronously, done
454433

455-
actor RecipientOnQueue {
434+
actor RecipientOnQueue: RecipientProtocol {
456435
let executor: NaiveQueueExecutor
457436
nonisolated let unownedExecutor: UnownedSerialExecutor
458437

@@ -461,30 +440,15 @@ actor RecipientOnQueue {
461440
self.unownedExecutor = executor.asUnownedSerialExecutor()
462441
}
463442

464-
func sync(syncTaskThreadID: ThreadID) {
465-
self.preconditionIsolated()
466-
dispatchPrecondition(condition: .onQueue(self.executor.queue))
467-
468-
print("\(Recipient.self)/\(#function) Current actor thread id = \(getCurrentThreadID()) @ :\(#line)")
469-
if compareThreadIDs(syncTaskThreadID, .equal, getCurrentThreadID()) {
470-
print("NOTICE: Actor must not run on the synchronous task's thread :\(#line)")
471-
}
472-
}
473-
474-
func async(syncTaskThreadID: ThreadID) async {
443+
func callAndSuspend(syncTaskThreadID: ThreadID) async {
475444
self.preconditionIsolated()
476445
dispatchPrecondition(condition: .onQueue(self.executor.queue))
477446

478-
// Dispatch may end up reusing the thread used to service the queue so we
479-
// cannot truly assert exact thread identity in such tests.
480-
// Usually this will be on a different thread by now though.
481447
print("\(Recipient.self)/\(#function) Current actor thread id = \(getCurrentThreadID()) @ :\(#line)")
482448
if compareThreadIDs(syncTaskThreadID, .equal, getCurrentThreadID()) {
483449
print("NOTICE: Actor must not run on the synchronous task's thread :\(#line)")
484450
}
485451

486-
await Task {
487-
self.preconditionIsolated()
488-
}.value
452+
try? await Task.sleep(for: .milliseconds(100))
489453
}
490454
}

0 commit comments

Comments
 (0)