Skip to content

Commit d9f9144

Browse files
try switching to not trigger Task inside callback
this will preserve order
1 parent a5f6729 commit d9f9144

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

Tests/AblyLiveObjectsTests/JS Integration Tests/ObjectsIntegrationTests.swift

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,8 @@ private struct ObjectsIntegrationTests {
32503250
let counter = try #require(ctx.root.get(key: ctx.sampleCounterKey)?.liveCounterValue)
32513251
let expectedCounterIncrements = [100.0, -100.0, Double(Int.max), Double(-Int.max)]
32523252

3253-
actor UpdateIndexTracker {
3253+
@MainActor
3254+
class UpdateIndexTracker {
32543255
private var index = 0
32553256

32563257
func getAndIncrement() -> Int {
@@ -3265,8 +3266,8 @@ private struct ObjectsIntegrationTests {
32653266
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
32663267
do {
32673268
try counter.subscribe { update, _ in
3268-
Task {
3269-
let currentUpdateIndex = await tracker.getAndIncrement()
3269+
MainActor.assumeIsolated {
3270+
let currentUpdateIndex = tracker.getAndIncrement()
32703271
let expectedInc = expectedCounterIncrements[currentUpdateIndex]
32713272
#expect(update.amount == expectedInc, "Check counter subscription callback is called with an expected update object for \(currentUpdateIndex + 1) times")
32723273

@@ -3369,7 +3370,8 @@ private struct ObjectsIntegrationTests {
33693370
["bar": .removed],
33703371
]
33713372

3372-
actor UpdateIndexTracker {
3373+
@MainActor
3374+
class UpdateIndexTracker {
33733375
private var index = 0
33743376

33753377
func getAndIncrement() -> Int {
@@ -3384,8 +3386,8 @@ private struct ObjectsIntegrationTests {
33843386
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
33853387
do {
33863388
try map.subscribe { update, _ in
3387-
Task {
3388-
let currentUpdateIndex = await tracker.getAndIncrement()
3389+
MainActor.assumeIsolated {
3390+
let currentUpdateIndex = tracker.getAndIncrement()
33893391
let expectedUpdate = expectedMapUpdates[currentUpdateIndex]
33903392
#expect(update.update == expectedUpdate, "Check map subscription callback is called with an expected update object for \(currentUpdateIndex + 1) times")
33913393

@@ -3452,7 +3454,8 @@ private struct ObjectsIntegrationTests {
34523454
action: { ctx in
34533455
let counter = try #require(ctx.root.get(key: ctx.sampleCounterKey)?.liveCounterValue)
34543456

3455-
actor CallbackTracker {
3457+
@MainActor
3458+
class CallbackTracker {
34563459
private var callbackCalled = 0
34573460

34583461
func increment() {
@@ -3469,10 +3472,10 @@ private struct ObjectsIntegrationTests {
34693472
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
34703473
do {
34713474
try counter.subscribe { _, subscriptionResponse in
3472-
// unsubscribe from future updates after the first call - do this synchronously
3473-
subscriptionResponse.unsubscribe()
3474-
Task {
3475-
await tracker.increment()
3475+
MainActor.assumeIsolated {
3476+
// unsubscribe from future updates after the first call
3477+
subscriptionResponse.unsubscribe()
3478+
tracker.increment()
34763479
continuation.resume()
34773480
}
34783481
}
@@ -3508,7 +3511,8 @@ private struct ObjectsIntegrationTests {
35083511
let counter = try #require(ctx.root.get(key: ctx.sampleCounterKey)?.liveCounterValue)
35093512
let callbacks = 3
35103513

3511-
actor CallbackTracker {
3514+
@MainActor
3515+
class CallbackTracker {
35123516
private var callbacksCalled: [Int]
35133517
private var completedCallbacks = 0
35143518

@@ -3536,15 +3540,15 @@ private struct ObjectsIntegrationTests {
35363540
}
35373541
}
35383542

3539-
let tracker = CallbackTracker(count: callbacks)
3543+
let tracker = await CallbackTracker(count: callbacks)
35403544

35413545
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
35423546
// Create multiple subscriptions
35433547
for i in 0 ..< callbacks {
35443548
do {
35453549
try counter.subscribe { _, _ in
3546-
Task {
3547-
let allCompleted = await tracker.increment(at: i)
3550+
MainActor.assumeIsolated {
3551+
let allCompleted = tracker.increment(at: i)
35483552
if allCompleted {
35493553
continuation.resume()
35503554
}
@@ -3588,7 +3592,8 @@ private struct ObjectsIntegrationTests {
35883592
action: { ctx in
35893593
let map = try #require(ctx.root.get(key: ctx.sampleMapKey)?.liveMapValue)
35903594

3591-
actor CallbackTracker {
3595+
@MainActor
3596+
class CallbackTracker {
35923597
private var callbackCalled = 0
35933598

35943599
func increment() {
@@ -3605,10 +3610,10 @@ private struct ObjectsIntegrationTests {
36053610
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
36063611
do {
36073612
try map.subscribe { _, subscriptionResponse in
3608-
// unsubscribe from future updates after the first call - do this synchronously
3609-
subscriptionResponse.unsubscribe()
3610-
Task {
3611-
await tracker.increment()
3613+
MainActor.assumeIsolated {
3614+
// unsubscribe from future updates after the first call
3615+
subscriptionResponse.unsubscribe()
3616+
tracker.increment()
36123617
continuation.resume()
36133618
}
36143619
}
@@ -3651,7 +3656,8 @@ private struct ObjectsIntegrationTests {
36513656
let map = try #require(ctx.root.get(key: ctx.sampleMapKey)?.liveMapValue)
36523657
let callbacks = 3
36533658

3654-
actor CallbackTracker {
3659+
@MainActor
3660+
class CallbackTracker {
36553661
private var callbacksCalled: [Int]
36563662
private var completedCallbacks = 0
36573663

@@ -3679,15 +3685,15 @@ private struct ObjectsIntegrationTests {
36793685
}
36803686
}
36813687

3682-
let tracker = CallbackTracker(count: callbacks)
3688+
let tracker = await CallbackTracker(count: callbacks)
36833689

36843690
async let subscriptionPromise: Void = withCheckedThrowingContinuation { continuation in
36853691
// Create multiple subscriptions
36863692
for i in 0 ..< callbacks {
36873693
do {
36883694
try map.subscribe { _, _ in
3689-
Task {
3690-
let allCompleted = await tracker.increment(at: i)
3695+
MainActor.assumeIsolated {
3696+
let allCompleted = tracker.increment(at: i)
36913697
if allCompleted {
36923698
continuation.resume()
36933699
}

0 commit comments

Comments
 (0)