 }
 
 actor class A {
-  func onCorrectQueue() -> Bool {
+  func onCorrectQueue(_ count : Int) -> Int {
     if checkIfMainQueue(expectedAnswer: false) {
       print("on actor instance's queue")
-      return true
+      return count + 1
     }
     print("ERROR: not on actor instance's queue")
-    return false
+    return -10
   }
 }
 
-@MainActor func exitTest(success: Bool) -> Never {
-  if !success {
-    exit(EXIT_FAILURE)
-  }
-
+@MainActor func checkAnotherFn(_ count : Int) -> Int {
   if checkIfMainQueue(expectedAnswer: true) {
     print("on main queue again!")
+    return count + 1
   } else {
     print("ERROR: left the main queue?")
+    return -10
   }
-
-  exit(EXIT_SUCCESS)
 }
 
-@MainActor func enterMainActor() async -> Never {
-  var ok = checkIfMainQueue(expectedAnswer: true)
-  if ok {
+@MainActor func enterMainActor(_ initialCount : Int) async -> Int {
+  if checkIfMainQueue(expectedAnswer: true) {
     print("hello from main actor!")
   } else {
     print("ERROR: not on correct queue!")
   }
 
   // try calling a function on another actor.
-  let someActor = A()
-  let successfulActorSwitch = await someActor.onCorrectQueue()
-  ok = ok && successfulActorSwitch
+  let count = await A().onCorrectQueue(initialCount)
 
-  exitTest(success: ok)
+  guard checkIfMainQueue(expectedAnswer: true) else {
+    print("ERROR: did not switch back to main actor!")
+    return -10
+  }
+
+  return checkAnotherFn(count) + 1
 }
 
-@concurrent func someFunc() async {
-  guard checkIfMainQueue(expectedAnswer: false) else {
-    print("ERROR: did not expect detatched task to run on main queue!")
-    exit(EXIT_FAILURE)
-  }
-  await enterMainActor()
+@concurrent func someFunc() async -> Int {
+  // NOTE: the "return" counter is just to make sure we're properly returning values.
+  // the expected number should be equal to the number of "plus-one" expressions.
+  // since there are no loops or duplicate function calls
+  return await enterMainActor(0) + 1
 }
 
 

// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency

// REQUIRES: OS=macosx || OS=ios
// FIXME: should not require Darwin to run this test once we have async main!

// for exit(:Int)
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#endif

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
  import Dispatch

// CHECK: on actor instance's queue
// CHECK-NOT: ERROR
// CHECK: on main queue again!
// CHECK-NOT: ERROR
// CHECK: finished with return counter = 4

@main struct RunIt {
  static func main() async {
    print("starting")
    let result = await someFunc()
    print("finished with return counter = \(result)")
  }
}
