Skip to content

Commit

Permalink
Fix a test which wasn't guaranteed to work
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Dec 5, 2022
1 parent 8c294f1 commit 95f3c1b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
48 changes: 27 additions & 21 deletions Realm/ObjectServerTests/SwiftObjectServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
func testStreamingDownloadNotifier() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}

var callCount = 0
Expand All @@ -1206,29 +1205,40 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
return

}
let ex = expectation(description: "streaming-downloads-expectation")
var hasBeenFulfilled = false

var ex = expectation(description: "download one")
let token = session.addProgressNotification(for: .download, mode: .reportIndefinitely) { p in
callCount += 1
XCTAssertGreaterThanOrEqual(p.transferredBytes, transferred)
XCTAssertGreaterThanOrEqual(p.transferrableBytes, transferrable)
transferred = p.transferredBytes
transferrable = p.transferrableBytes
if p.transferredBytes > 0 && p.isTransferComplete && !hasBeenFulfilled {
ex.fulfill()
hasBeenFulfilled = true
DispatchQueue.main.async {
callCount += 1
XCTAssertGreaterThanOrEqual(p.transferredBytes, transferred)
XCTAssertGreaterThanOrEqual(p.transferrableBytes, transferrable)
transferred = p.transferredBytes
transferrable = p.transferrableBytes
if p.transferredBytes > 0 && p.isTransferComplete && !hasBeenFulfilled {
ex.fulfill()
hasBeenFulfilled = true
}
}
}
XCTAssertNotNil(token)

// Wait for the child process to upload all the data.
executeChild()
waitForExpectations(timeout: 60.0, handler: nil)
XCTAssertGreaterThanOrEqual(callCount, 1)
XCTAssertGreaterThanOrEqual(transferred, transferrable)
let initialCallCount = callCount

// Run a second time to upload more data and verify that the callback continues to be called
hasBeenFulfilled = false
ex = expectation(description: "download two")
executeChild()
waitForExpectations(timeout: 60.0, handler: nil)
XCTAssertGreaterThanOrEqual(callCount, initialCallCount)
XCTAssertGreaterThanOrEqual(transferred, transferrable)

token!.invalidate()
XCTAssert(callCount > 1)
XCTAssert(transferred >= transferrable)
}

func testStreamingUploadNotifier() throws {
Expand Down Expand Up @@ -1323,8 +1333,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
func testDownloadRealm() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}

// Wait for the child process to upload everything.
Expand Down Expand Up @@ -1358,8 +1367,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
func testDownloadRealmToCustomPath() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}

// Wait for the child process to upload everything.
Expand Down Expand Up @@ -1396,8 +1404,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
func testCancelDownloadRealm() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}

// Wait for the child process to upload everything.
Expand Down Expand Up @@ -1426,8 +1433,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
func testAsyncOpenProgress() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}

// Wait for the child process to upload everything.
Expand Down
23 changes: 9 additions & 14 deletions Realm/ObjectServerTests/SwiftSyncTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,17 @@ open class SwiftSyncTestCase: RLMSyncTestCase {
}

public static let bigObjectCount = 2
public func populateRealm<T: BSON>(user: User? = nil, partitionValue: T) {
do {
let user = try (user ?? logInUser(for: basicCredentials()))
let config = user.configuration(testName: partitionValue)

let realm = try openRealm(configuration: config)
try realm.write {
for _ in 0..<SwiftSyncTestCase.bigObjectCount {
realm.add(SwiftHugeSyncObject.create())
}
public func populateRealm<T: BSON>(user: User? = nil, partitionValue: T) throws {
let user = try (user ?? logInUser(for: basicCredentials()))
let config = user.configuration(testName: partitionValue)

let realm = try openRealm(configuration: config)
try realm.write {
for _ in 0..<SwiftSyncTestCase.bigObjectCount {
realm.add(SwiftHugeSyncObject.create())
}
waitForUploads(for: realm)
checkCount(expected: SwiftSyncTestCase.bigObjectCount, realm, SwiftHugeSyncObject.self)
} catch {
XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
}
waitForUploads(for: realm)
}

// MARK: - Flexible Sync Use Cases
Expand Down
43 changes: 15 additions & 28 deletions Realm/ObjectServerTests/SwiftUIServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
func testAsyncOpenDownloadRealm() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}
executeChild()

Expand Down Expand Up @@ -130,8 +129,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
func testAsyncOpenProgressNotification() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}
executeChild()

Expand All @@ -150,8 +148,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
func testAsyncOpenWithACachedApp() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}
executeChild()

Expand Down Expand Up @@ -184,8 +181,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {

let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: partitionValueA)
return
return try populateRealm(user: user, partitionValue: partitionValueA)
}
executeChild()

Expand Down Expand Up @@ -218,8 +214,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
XCTAssertEqual(syncUser2.id, app.currentUser!.id)

if !isParent {
populateRealm(user: syncUser1, partitionValue: partitionValueA)
return
return try populateRealm(user: syncUser1, partitionValue: partitionValueA)
}
executeChild()

Expand Down Expand Up @@ -252,8 +247,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {

let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: partitionValueB)
return
return try populateRealm(user: user, partitionValue: partitionValueB)
}
executeChild()

Expand Down Expand Up @@ -311,8 +305,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
let user = try logInUser(for: basicCredentials())

if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}
executeChild()

Expand Down Expand Up @@ -351,7 +344,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
try autoreleasepool {
let app = App(id: appId, configuration: appConfig)
let user = try logInUser(for: basicCredentials(app: app), app: app)
populateRealm(user: user, partitionValue: #function)
try populateRealm(user: user, partitionValue: #function)
}
App.resetAppCache()

Expand All @@ -372,7 +365,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
func testAutoOpenProgressNotification() throws {
try autoreleasepool {
let user = try logInUser(for: basicCredentials())
populateRealm(user: user, partitionValue: #function)
try populateRealm(user: user, partitionValue: #function)
}

let user = try logInUser(for: basicCredentials())
Expand All @@ -391,8 +384,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
func testAutoOpenWithACachedApp() throws {
let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: #function)
return
return try populateRealm(user: user, partitionValue: #function)
}
executeChild()

Expand Down Expand Up @@ -429,8 +421,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
XCTAssertEqual(syncUser2.id, app.currentUser!.id)

if !isParent {
populateRealm(user: syncUser1, partitionValue: partitionValueA)
return
return try populateRealm(user: syncUser1, partitionValue: partitionValueA)
}
executeChild()

Expand Down Expand Up @@ -475,8 +466,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {

let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: partitionValueB)
return
return try populateRealm(user: user, partitionValue: partitionValueB)
}
executeChild()

Expand All @@ -496,8 +486,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {

let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: partitionValueA)
return
return try populateRealm(user: user, partitionValue: partitionValueA)
}
executeChild()

Expand Down Expand Up @@ -527,8 +516,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {

let user = try logInUser(for: basicCredentials())
if !isParent {
populateRealm(user: user, partitionValue: partitionValueA)
return
return try populateRealm(user: user, partitionValue: partitionValueA)
}
executeChild()

Expand Down Expand Up @@ -561,8 +549,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
XCTAssertEqual(syncUser2.id, app.currentUser!.id)

if !isParent {
populateRealm(user: syncUser1, partitionValue: partitionValueA)
return
return try populateRealm(user: syncUser1, partitionValue: partitionValueA)
}
executeChild()

Expand Down
2 changes: 1 addition & 1 deletion RealmSwift/Tests/CombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CombinePublisherTestCase: TestCase {

override func setUp() {
super.setUp()
realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "test"))
realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "CombinePublisherTestCase"))
XCTAssertTrue(realm.isEmpty)
}

Expand Down

0 comments on commit 95f3c1b

Please sign in to comment.