Skip to content

Commit

Permalink
test wip async tasks carry over
Browse files Browse the repository at this point in the history
  • Loading branch information
nan-li committed Apr 29, 2024
1 parent 581f159 commit 86b45a2
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,74 +69,74 @@ final class OneSignalUserTests: XCTestCase {
It is possible for two threads to flush concurrently.
However, this test does not crash 100% of the time.
*/
func testOperationRepoFlushingConcurrency() throws {
/* Setup */
OneSignalCoreImpl.setSharedClient(MockOneSignalClient())

/* When */

// 1. Enqueue 10 Deltas to the Operation Repo
for num in 0...9 {
OneSignalUserManagerImpl.sharedInstance.addTag(key: "tag\(num)", value: "value")
}

// 2. Flush the delta queue from 4 multiple threads
for _ in 1...4 {
DispatchQueue.global().async {
print("🧪 flushDeltaQueue on thread \(Thread.current)")
OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
}
}

/* Then */
// There are two places that can crash, as multiple threads are manipulating arrays:
// 1. OpRepo: `deltaQueue.remove(at: index)` index out of bounds
// 2. OSPropertyOperationExecutor: `deltaQueue.append(delta)` EXC_BAD_ACCESS
}
// func testOperationRepoFlushingConcurrency() throws {
// /* Setup */
// OneSignalCoreImpl.setSharedClient(MockOneSignalClient())
//
// /* When */
//
// // 1. Enqueue 10 Deltas to the Operation Repo
// for num in 0...9 {
// OneSignalUserManagerImpl.sharedInstance.addTag(key: "tag\(num)", value: "value")
// }
//
// // 2. Flush the delta queue from 4 multiple threads
// for _ in 1...4 {
// DispatchQueue.global().async {
// print("🧪 flushDeltaQueue on thread \(Thread.current)")
// OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
// }
// }
//
// /* Then */
// // There are two places that can crash, as multiple threads are manipulating arrays:
// // 1. OpRepo: `deltaQueue.remove(at: index)` index out of bounds
// // 2. OSPropertyOperationExecutor: `deltaQueue.append(delta)` EXC_BAD_ACCESS
// }

/**
This test reproduced a crash when the property model is being encoded.
*/
func testEncodingPropertiesModel_withConcurrency_doesNotCrash() throws {
/* Setup */
let propertiesModel = OSPropertiesModel(changeNotifier: OSEventProducer())

/* When */
DispatchQueue.concurrentPerform(iterations: 5_000) { i in
// 1. Add tags
for num in 0...9 {
propertiesModel.addTags(["\(i)tag\(num)": "value"])
}

// 2. Encode the model
OneSignalUserDefaults.initShared().saveCodeableData(forKey: "PropertyModel", withValue: propertiesModel)

// 3. Clear the tags
propertiesModel.clearData()
}
}
// func testEncodingPropertiesModel_withConcurrency_doesNotCrash() throws {
// /* Setup */
// let propertiesModel = OSPropertiesModel(changeNotifier: OSEventProducer())
//
// /* When */
// DispatchQueue.concurrentPerform(iterations: 5_000) { i in
// // 1. Add tags
// for num in 0...9 {
// propertiesModel.addTags(["\(i)tag\(num)": "value"])
// }
//
// // 2. Encode the model
// OneSignalUserDefaults.initShared().saveCodeableData(forKey: "PropertyModel", withValue: propertiesModel)
//
// // 3. Clear the tags
// propertiesModel.clearData()
// }
// }

/**
This test reproduced a crash when the identity model is being encoded.
*/
func testEncodingIdentityModel_withConcurrency_doesNotCrash() throws {
/* Setup */
let identityModel = OSIdentityModel(aliases: nil, changeNotifier: OSEventProducer())

/* When */
DispatchQueue.concurrentPerform(iterations: 5_000) { i in
// 1. Add aliases
for num in 0...9 {
identityModel.addAliases(["\(i)alias\(num)": "value"])
}

// 2. Encode the model
OneSignalUserDefaults.initShared().saveCodeableData(forKey: "IdentityModel", withValue: identityModel)

// 2. Clear the aliases
identityModel.clearData()
}
}
// func testEncodingIdentityModel_withConcurrency_doesNotCrash() throws {
// /* Setup */
// let identityModel = OSIdentityModel(aliases: nil, changeNotifier: OSEventProducer())
//
// /* When */
// DispatchQueue.concurrentPerform(iterations: 5_000) { i in
// // 1. Add aliases
// for num in 0...9 {
// identityModel.addAliases(["\(i)alias\(num)": "value"])
// }
//
// // 2. Encode the model
// OneSignalUserDefaults.initShared().saveCodeableData(forKey: "IdentityModel", withValue: identityModel)
//
// // 2. Clear the aliases
// identityModel.clearData()
// }
// }

func testSwitchUser_sendsCorrectTags() throws {
/* Setup */
Expand Down

0 comments on commit 86b45a2

Please sign in to comment.