Skip to content

Commit 39e32a7

Browse files
author
DominicGBauer
committed
fix: failing test
1 parent 1bfe15e commit 39e32a7

File tree

1 file changed

+54
-35
lines changed

1 file changed

+54
-35
lines changed

Tests/PowerSyncSwiftTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,41 +95,60 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
9595
}
9696

9797
func testWatchTableChanges() async throws {
98-
let expectation = XCTestExpectation(description: "Watch changes")
99-
var results: [[String]] = []
100-
101-
let stream = database.watch(
102-
sql: "SELECT name FROM users ORDER BY id",
103-
parameters: nil
104-
) { cursor in
105-
cursor.getString(index: 0)!
106-
}
107-
108-
let watchTask = Task {
109-
for await names in stream {
110-
results.append(names)
111-
if results.count == 2 {
112-
expectation.fulfill()
113-
}
114-
}
115-
}
116-
117-
_ = try await database.execute(
118-
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
119-
parameters: ["1", "User 1", "user1@example.com"]
120-
)
121-
122-
_ = try await database.execute(
123-
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
124-
parameters: ["2", "User 2", "user2@example.com"]
125-
)
126-
127-
await fulfillment(of: [expectation], timeout: 5)
128-
watchTask.cancel()
129-
130-
XCTAssertEqual(results.count, 2)
131-
XCTAssertEqual(results[1], ["User 1", "User 2"])
132-
}
98+
let expectation = XCTestExpectation(description: "Watch changes")
99+
100+
// Create an actor to handle concurrent mutations
101+
actor ResultsStore {
102+
private var results: [[String]] = []
103+
104+
func append(_ names: [String]) {
105+
results.append(names)
106+
}
107+
108+
func getResults() -> [[String]] {
109+
results
110+
}
111+
112+
func count() -> Int {
113+
results.count
114+
}
115+
}
116+
117+
let resultsStore = ResultsStore()
118+
119+
let stream = database.watch(
120+
sql: "SELECT name FROM users ORDER BY id",
121+
parameters: nil
122+
) { cursor in
123+
cursor.getString(index: 0)!
124+
}
125+
126+
let watchTask = Task {
127+
for await names in stream {
128+
await resultsStore.append(names)
129+
if await resultsStore.count() == 2 {
130+
expectation.fulfill()
131+
}
132+
}
133+
}
134+
135+
_ = try await database.execute(
136+
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
137+
parameters: ["1", "User 1", "user1@example.com"]
138+
)
139+
140+
_ = try await database.execute(
141+
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
142+
parameters: ["2", "User 2", "user2@example.com"]
143+
)
144+
145+
await fulfillment(of: [expectation], timeout: 5)
146+
watchTask.cancel()
147+
148+
let finalResults = await resultsStore.getResults()
149+
XCTAssertEqual(finalResults.count, 2)
150+
XCTAssertEqual(finalResults[1], ["User 1", "User 2"])
151+
}
133152

134153
func testWriteTransaction() async throws {
135154
try await database.writeTransaction { transaction in

0 commit comments

Comments
 (0)