Created
November 5, 2022 18:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
import CoreData | |
@testable import iosApp | |
class iosAppTests: XCTestCase { | |
func testCoreDataMergeConflict() { | |
let expectation = XCTestExpectation(description: "") | |
expectation.expectedFulfillmentCount = 2 | |
Task { | |
await CoreDataInventory.instance.perform { (context) in | |
let newChartState = ChartState(context: context) | |
newChartState.chartLen = 100 | |
newChartState.seed = 1 | |
} | |
} | |
Task { | |
await CoreDataInventory.instance.perform { (context) in | |
sleep(1) | |
self.modifyChartStateSeed(context, 2) | |
sleep(1) // Wait 1 seconds before save for the next background task to read the same version of entity | |
} | |
expectation.fulfill() | |
} | |
Task { | |
await CoreDataInventory.instance.perform { (context) in | |
sleep(1) | |
self.modifyChartStateSeed(context, 3) | |
sleep(2) | |
} | |
expectation.fulfill() | |
} | |
wait(for: [expectation], timeout: 8) | |
} | |
private func modifyChartStateSeed(_ context: NSManagedObjectContext, _ seed: Int32) { | |
let fetchRequest: NSFetchRequest<ChartState> = ChartState.fetchRequest() | |
fetchRequest.predicate = NSPredicate(format: "chartLen = %i", 100) | |
let chartStateEntity = try? context.fetch(fetchRequest).first | |
chartStateEntity?.seed = seed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment