Skip to content

Commit

Permalink
Merge pull request #8573 from realm/tg/app-baseurl
Browse files Browse the repository at this point in the history
Fix -[RLMApp baseURL] always being nil
  • Loading branch information
tgoyne authored May 21, 2024
2 parents 8cf26e0 + 2ab87f4 commit 83f6804
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 174 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ x.y.z Release notes (yyyy-MM-dd)
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?)
* `@AutoOpen` and `@AsyncOpen` failed to use the `initialSubscriptions` set in
the configuration passed to them ([PR #8572](https://github.com/realm/realm-swift/pull/8572), since v10.50.0).
* `App.baseURL` was always `nil` ([PR #8573](https://github.com/realm/realm-swift/pull/8573), since it was introduced in v10.50.0).

<!-- ### Breaking Changes - ONLY INCLUDE FOR NEW MAJOR version -->

Expand Down
17 changes: 9 additions & 8 deletions Realm/ObjectServerTests/AsyncSyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ class AsyncAwaitSyncTests: SwiftSyncTestCase {

func testUpdateBaseUrl() async throws {
let app = App(id: appId)
XCTAssertNotNil(app.baseURL)
XCTAssertEqual(app.baseURL, "http://localhost:9090")
XCTAssertEqual(app.baseURL, "https://services.cloud.mongodb.com")

try await app.updateBaseUrl(to: "http://localhost:8080")
XCTAssertEqual(app.baseURL, "http://localhost:8080")
try await app.updateBaseUrl(to: "http://localhost:9090")
XCTAssertEqual(app.baseURL, "http://localhost:9090")

try await app.updateBaseUrl(to: "http://localhost:7070/")
XCTAssertEqual(app.baseURL, "http://localhost:7070")
try await app.updateBaseUrl(to: "http://127.0.0.1:9090")
XCTAssertEqual(app.baseURL, "http://127.0.0.1:9090")

try await app.updateBaseUrl(to: nil)
XCTAssertEqual(app.baseURL, "https://services.cloud.mongodb.com")
// Fails as this appId doesn't exist in prod
await assertThrowsError(try await app.updateBaseUrl(to: nil)) { (error: AppError) in
XCTAssertEqual(error.code, .unknown)
}
}

@MainActor func testAsyncOpenStandalone() async throws {
Expand Down
31 changes: 21 additions & 10 deletions Realm/ObjectServerTests/RLMObjectServerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,39 @@ - (NSArray *)defaultObjectTypes {

- (void)testUpdateBaseUrl {
RLMApp *app = self.app;
XCTAssertEqual(app.baseURL, @"http://localhost:9090");
XCTAssertEqualObjects(app.baseURL, @"http://localhost:9090");

XCTestExpectation *expectation = [self expectationWithDescription:@"should update base url"];
[app updateBaseURL:@"http://localhost:8080" completion:^(NSError *error) {
[app updateBaseURL:@"http://127.0.0.1:9090" completion:^(NSError *error) {
XCTAssertNil(error);
[expectation fulfill];
}];
XCTAssertEqual(app.baseURL, @"http://localhost:8080");
[self waitForExpectations:@[expectation]];
XCTAssertEqualObjects(app.baseURL, @"http://127.0.0.1:9090");

XCTestExpectation *expectation1 = [self expectationWithDescription:@"should update base url"];
TimeoutProxyServer *proxy = [[TimeoutProxyServer alloc] initWithPort:7070 targetPort:9090];
proxy.delay = 0;
[proxy startAndReturnError:nil];

expectation = [self expectationWithDescription:@"should update base url"];
[app updateBaseURL:@"http://localhost:7070/" completion:^(NSError *error) {
XCTAssertNil(error);
[expectation1 fulfill];
[expectation fulfill];
}];
XCTAssertEqual(app.baseURL, @"http://localhost:7070");
[self waitForExpectations:@[expectation]];
XCTAssertEqualObjects(app.baseURL, @"http://localhost:7070");
[proxy stop];

XCTestExpectation *expectation2 = [self expectationWithDescription:@"should update base url to default value"];
expectation = [self expectationWithDescription:@"should fail to update base url to default value"];
[app updateBaseURL:nil completion:^(NSError *error) {
XCTAssertNil(error);
[expectation2 fulfill];
// This fails because our local app doesn't exist in the prod env
XCTAssertNotNil(error);
XCTAssertEqual(error.code, RLMAppErrorUnknown);
[expectation fulfill];
}];
XCTAssertEqual(app.baseURL, @"https://services.cloud.mongodb.com");
[self waitForExpectations:@[expectation]];
// baseURL update failed, so it's left unchanged
XCTAssertEqualObjects(app.baseURL, @"http://localhost:7070");
}

- (void)testAnonymousAuthentication {
Expand Down
2 changes: 1 addition & 1 deletion Realm/ObjectServerTests/RealmServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ public class RealmServer: NSObject {
let appServerId = try retrieveAppServerId(appId)
let ident = RLMGetClientFileIdent(ObjectiveCSupport.convert(object: realm))
XCTAssertNotEqual(ident, 0)
_ = try session.privateApps[appServerId].sync.forceReset.put(["file_ident": ident]).get()
_ = try session.apps[appServerId].sync.forceReset.put(["file_ident": ident]).get()
}

public func waitForSync(appId: String) throws {
Expand Down
44 changes: 21 additions & 23 deletions Realm/ObjectServerTests/SwiftFlexibleSyncServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {

let user = createUser()
var config = user.flexibleSyncConfiguration(initialSubscriptions: { subscriptions in
subscriptions.append(QuerySubscription<SwiftHugeSyncObject>())
subscriptions.append(QuerySubscription<SwiftHugeSyncObject> {
$0.partition == self.name
})
})
config.objectTypes = objectTypes
var downloadRealm: Realm?
Expand Down Expand Up @@ -982,13 +984,12 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {
XCTAssertTrue(p1.isTransferComplete)
}

func testNonStreamingDownloadNotifier() throws {
func testNonStreamingDownloadNotifier() async throws {
try populateRealm()

let realm = try openRealm(wait: false)

let session = try XCTUnwrap(realm.syncSession)
let ex = expectation(description: "first download")
let callCount = Locked(0)
let progress = Locked<SyncSession.Progress?>(nil)

Expand All @@ -1005,14 +1006,11 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {
XCTAssertNotNil(token)

let subscriptions = realm.subscriptions
subscriptions.update({
subscriptions.append(QuerySubscription<SwiftHugeSyncObject>(name: "huge_objects"))
}, onComplete: { err in
XCTAssertNil(err)
ex.fulfill()
})

waitForExpectations(timeout: 60.0)
try await subscriptions.update {
subscriptions.append(QuerySubscription<SwiftHugeSyncObject> {
$0.partition == self.name
})
}

XCTAssertEqual(realm.objects(SwiftHugeSyncObject.self).count, SwiftSyncTestCase.bigObjectCount)

Expand Down Expand Up @@ -1062,12 +1060,12 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {

let subscriptions = realm.subscriptions
subscriptions.update({
subscriptions.append(QuerySubscription<SwiftHugeSyncObject>(name: "huge_objects"))
subscriptions.append(QuerySubscription<SwiftHugeSyncObject> {
$0.partition == self.name
})
}, onComplete: { err in
DispatchQueue.main.async {
XCTAssertNil(err)
ex.fulfill()
}
XCTAssertNil(err)
ex.fulfill()
})

waitForExpectations(timeout: 60.0)
Expand Down Expand Up @@ -1099,21 +1097,21 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {
let realm = try openRealm(wait: false)
let subscriptions = realm.subscriptions
subscriptions.update {
subscriptions.append(QuerySubscription<SwiftHugeSyncObject>(name: "huge_objects"))
subscriptions.append(QuerySubscription<SwiftHugeSyncObject> {
$0.partition == self.name
})
}
let session = try XCTUnwrap(realm.syncSession)

let progress = Locked<SyncSession.Progress?>(nil)
let callCount = Locked(0)

let token = session.addProgressNotification(for: .upload, mode: .reportIndefinitely) { p in
if let progress = progress.value {
if progress.progressEstimate < 1 {
XCTAssertGreaterThanOrEqual(p.progressEstimate, progress.progressEstimate)
}
if let progress = progress.value, progress.progressEstimate < 1 {
XCTAssertGreaterThanOrEqual(p.progressEstimate, progress.progressEstimate)
}
progress.value = p
callCount.withLock({ $0 += 1 })
callCount.withLock { $0 += 1 }
}
XCTAssertNotNil(token)
waitForUploads(for: realm)
Expand All @@ -1123,7 +1121,7 @@ class SwiftFlexibleSyncTests: SwiftSyncTestCase {
let currentCount = callCount.value
try realm.write {
for _ in 0..<SwiftSyncTestCase.bigObjectCount {
realm.add(SwiftHugeSyncObject.create())
realm.add(SwiftHugeSyncObject.create(key: self.name))
}
}

Expand Down
50 changes: 12 additions & 38 deletions Realm/ObjectServerTests/SwiftObjectServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,16 @@ class SwiftObjectServerTests: SwiftSyncTestCase {

func testUpdateBaseUrl() {
let app = App(id: appId)
XCTAssertNotNil(app.baseURL)
XCTAssertEqual(app.baseURL, "http://localhost:9090")
XCTAssertEqual(app.baseURL, "https://services.cloud.mongodb.com")

let ex = expectation(description: "update base url")
app.updateBaseUrl(to: "http://localhost:8080", { result in
switch result {
case .success:
ex.fulfill()
case .failure:
XCTFail("Should not return an error")
}
})
XCTAssertEqual(app.baseURL, "http://localhost:8080")
app.updateBaseUrl(to: "http://localhost:9090").await(self)
XCTAssertEqual(app.baseURL, "http://localhost:9090")

let ex1 = expectation(description: "update base url")
app.updateBaseUrl(to: "http://localhost:7070/", { result in
switch result {
case .success:
ex1.fulfill()
case .failure:
XCTFail("Should not return an error")
}
})
XCTAssertEqual(app.baseURL, "http://localhost:7070")
app.updateBaseUrl(to: "http://127.0.0.1:9090").await(self)
XCTAssertEqual(app.baseURL, "http://127.0.0.1:9090")

let ex2 = expectation(description: "update base url")
app.updateBaseUrl(to: nil, { result in
switch result {
case .success:
ex2.fulfill()
case .failure:
XCTFail("Should not return an error")
}
})
XCTAssertEqual(app.baseURL, "https://services.cloud.mongodb.com")
app.updateBaseUrl(to: nil).awaitFailure(self)
XCTAssertEqual(app.baseURL, "http://127.0.0.1:9090")
}

func testBasicSwiftSync() throws {
Expand Down Expand Up @@ -354,22 +329,21 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
let realm = try openRealm(wait: false)
let session = try XCTUnwrap(realm.syncSession)

var progress: SyncSession.Progress?
let progress = Locked<SyncSession.Progress?>(nil)

let token = session.addProgressNotification(for: .upload, mode: .reportIndefinitely) { p in
DispatchQueue.main.async { @MainActor in
if let progress = progress {
progress.withLock { progress in
if let progress {
XCTAssertGreaterThanOrEqual(p.progressEstimate, progress.progressEstimate)
}
progress = p
}
}
XCTAssertNotNil(token)

waitForUploads(for: realm)

for _ in 0..<5 {
progress = nil
progress.value = nil
try realm.write {
for _ in 0..<SwiftSyncTestCase.bigObjectCount {
realm.add(SwiftHugeSyncObject.create())
Expand All @@ -380,7 +354,7 @@ class SwiftObjectServerTests: SwiftSyncTestCase {
}
token!.invalidate()

let p = try XCTUnwrap(progress)
let p = try XCTUnwrap(progress.value)
XCTAssertEqual(p.transferredBytes, p.transferrableBytes)
XCTAssertEqual(p.progressEstimate, 1.0)
XCTAssertTrue(p.isTransferComplete)
Expand Down
8 changes: 8 additions & 0 deletions Realm/ObjectServerTests/SwiftUIServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class SwiftUIServerTests: SwiftSyncTestCase {
super.tearDown()
}

override class var defaultTestSuite: XCTestSuite {
// Don't run tests for the base class
if self === SwiftUIServerTests.self {
return XCTestSuite(name: "SwiftUIServerTests")
}
return super.defaultTestSuite
}

var cancellables: Set<AnyCancellable> = []

func awaitOpen(_ wrapper: some AsyncOpenStateWrapper,
Expand Down
15 changes: 12 additions & 3 deletions Realm/ObjectServerTests/setup_baas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
STITCH_SUPPORT_URL="https://static.realm.io/downloads/swift/stitch-support.tar.xz"
MONGO_DIR="#{BUILD_DIR}/mongodb-macos-x86_64-#{MONGODB_VERSION}"

# exit immediately if any subcommand fails
class Object
def `(command)
ret = super
exit 1 unless $?.success?
ret
end
end

def setup_mongod
if !File.exist?("#{BIN_DIR}/mongo")
`cd '#{BUILD_DIR}' && curl --silent '#{MONGODB_URL}' | tar xz`
Expand All @@ -38,7 +47,7 @@ def setup_mongod

def run_mongod
puts "starting mongod..."
puts `mkdir '#{BUILD_DIR}'/db_files`
puts `mkdir -p '#{BUILD_DIR}'/db_files`
puts `#{MONGOD_EXE} --quiet \
--dbpath '#{BUILD_DIR}'/db_files \
--port 26000 \
Expand Down Expand Up @@ -132,13 +141,13 @@ def setup_stitch
puts `chmod +x '#{assisted_agg_filepath}'`
end

if `which node`.empty? && !Dir.exist?("#{BUILD_DIR}/node-v#{NODE_VERSION}-darwin-x64")
if `which node || true`.empty? && !Dir.exist?("#{BUILD_DIR}/node-v#{NODE_VERSION}-darwin-x64")
puts "downloading node 🚀"
puts `cd '#{BUILD_DIR}' && curl -O "https://nodejs.org/dist/v#{NODE_VERSION}/node-v#{NODE_VERSION}-darwin-x64.tar.gz" && tar xzf node-v#{NODE_VERSION}-darwin-x64.tar.gz`
exports << "export PATH=\"#{BUILD_DIR}/node-v#{NODE_VERSION}-darwin-x64/bin/:$PATH\""
end

if `which yarn`.empty?
if `which yarn || true`.empty?
`rm -rf "$HOME/.yarn"`
`export PATH=\"#{BUILD_DIR}/node-v#{NODE_VERSION}-darwin-x64/bin/:$PATH\" && curl -o- -L https://yarnpkg.com/install.sh | bash`
exports << "export PATH=\"$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH\""
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMApp.mm
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ - (RLMEmailPasswordAuth *)emailPasswordAuth {
return [[RLMEmailPasswordAuth alloc] initWithApp:_app];
}

- (NSString *)baseUrl {
- (NSString *)baseURL {
return getOptionalString(_app->get_base_url()) ?: RLMStringViewToNSString(_app->default_base_url());
}

Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMUser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ - (RLMRealmConfiguration *)flexibleSyncConfigurationWithInitialSubscriptions:(RL
RLMRealmConfiguration *config = [[RLMRealmConfiguration alloc] init];
syncConfig.clientResetMode = clientResetMode;
syncConfig.manualClientResetHandler = manualClientResetHandler;
syncConfig.initialSubscriptions = [[RLMInitialSubscriptionsConfiguration alloc] initWithCallback:initialSubscriptions rerunOnOpen:rerunOnOpen];
syncConfig.initialSubscriptions = [[RLMInitialSubscriptionsConfiguration alloc] initWithCallback:initialSubscriptions rerunOnOpen:rerunOnOpen];
config.syncConfiguration = syncConfig;
return config;
}
Expand Down
Loading

0 comments on commit 83f6804

Please sign in to comment.