Skip to content

Commit

Permalink
Maybe fix intermittent failures in offline autoopen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Mar 3, 2023
1 parent dc6b925 commit e9a2daa
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Realm/ObjectServerTests/RLMSyncTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ - (RLMApp *)app {
if (!_app) {
_app = [RLMApp appWithId:self.appId configuration:self.defaultAppConfiguration rootDirectory:self.clientDataRoot];
RLMSyncManager *syncManager = self.app.syncManager;
syncManager.logLevel = RLMSyncLogLevelTrace;
syncManager.logLevel = RLMSyncLogLevelOff;
syncManager.userAgent = self.name;
}
return _app;
Expand Down Expand Up @@ -682,7 +682,7 @@ - (RLMApp *)flexibleSyncApp {
configuration:self.defaultAppConfiguration
rootDirectory:self.clientDataRoot];
RLMSyncManager *syncManager = self.flexibleSyncApp.syncManager;
syncManager.logLevel = RLMSyncLogLevelTrace;
syncManager.logLevel = RLMSyncLogLevelOff;
syncManager.userAgent = self.name;
}
return _flexibleSyncApp;
Expand Down
17 changes: 10 additions & 7 deletions Realm/ObjectServerTests/SwiftSyncTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,19 @@ open class SwiftSyncTestCase: RLMSyncTestCase {

public static let bigObjectCount = 2
public func populateRealm<T: BSON>(user: User? = nil, partitionValue: T) throws {
let user = try (user ?? logInUser(for: basicCredentials()))
let config = user.configuration(testName: partitionValue)
try autoreleasepool {
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())
let realm = try openRealm(configuration: config)
try realm.write {
for _ in 0..<SwiftSyncTestCase.bigObjectCount {
realm.add(SwiftHugeSyncObject.create())
}
}
waitForUploads(for: realm)
realm.syncSession?.suspend()
}
waitForUploads(for: realm)
}

// MARK: - Flexible Sync Use Cases
Expand Down
36 changes: 17 additions & 19 deletions Realm/ObjectServerTests/SwiftUIServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,19 @@ class SwiftUIServerTests: SwiftSyncTestCase {

// In case of no internet connection AutoOpen should return an opened Realm, offline-first approach
func testAutoOpenOpenRealmWithoutInternetConnection() throws {
try autoreleasepool {
let user = try logInUser(for: basicCredentials(app: self.app), app: self.app)
try populateRealm(user: user, partitionValue: #function)
}
resetAppCache()

let proxy = TimeoutProxyServer(port: 5678, targetPort: 9090)
try proxy.start()
let appId = try RealmServer.shared.createApp()
let appConfig = AppConfiguration(baseURL: "http://localhost:5678",
transport: AsyncOpenConnectionTimeoutTransport(),
localAppName: nil,
localAppVersion: nil)

try autoreleasepool {
let app = App(id: appId, configuration: appConfig)
let user = try logInUser(for: basicCredentials(app: app), app: app)
try populateRealm(user: user, partitionValue: #function)
}
App.resetAppCache()

let app = App(id: appId, configuration: appConfig)
let app = App(id: appId, configuration: appConfig, rootDirectory: nil)
let user = try logInUser(for: basicCredentials(app: app), app: app)
proxy.dropConnections = true
let ex = expectation(description: "download-realm-auto-open-no-connection")
Expand All @@ -364,19 +361,13 @@ class SwiftUIServerTests: SwiftSyncTestCase {
}
}

// Clear cache to avoid leaking our app which connects to the proxy
App.resetAppCache()
proxy.stop()
}

// In case of no internet connection AutoOpen should return an opened Realm, offline-first approach
func testAutoOpenOpenForFlexibleSyncConfigWithoutInternetConnection() throws {
let proxy = TimeoutProxyServer(port: 5678, targetPort: 9090)
try proxy.start()

let appConfig = AppConfiguration(baseURL: "http://localhost:5678",
transport: AsyncOpenConnectionTimeoutTransport(),
localAppName: nil,
localAppVersion: nil)

try autoreleasepool {
try populateFlexibleSyncData { realm in
for i in 1...10 {
Expand All @@ -388,8 +379,14 @@ class SwiftUIServerTests: SwiftSyncTestCase {
}
}
}
App.resetAppCache()
resetAppCache()

let proxy = TimeoutProxyServer(port: 5678, targetPort: 9090)
try proxy.start()
let appConfig = AppConfiguration(baseURL: "http://localhost:5678",
transport: AsyncOpenConnectionTimeoutTransport(),
localAppName: nil,
localAppVersion: nil)
let app = App(id: flexibleSyncAppId, configuration: appConfig)

let user = try logInUser(for: basicCredentials(app: app), app: app)
Expand All @@ -413,6 +410,7 @@ class SwiftUIServerTests: SwiftSyncTestCase {
waitForExpectations(timeout: 10.0)
autoOpen.cancel()

App.resetAppCache()
proxy.stop()
}

Expand Down
6 changes: 4 additions & 2 deletions Realm/ObjectServerTests/TimeoutProxyServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public class TimeoutProxyServer: NSObject, @unchecked Sendable {
private func copyData(from: NWConnection, to: NWConnection) {
from.receive(minimumIncompleteLength: 1, maximumLength: 8192) { (data, context, isComplete, error) in
if let error = error {
if case .posix(.ECANCELED) = error {
switch error {
case .posix(.ECANCELED), .posix(.ECONNRESET):
return
default:
fatalError("\(error)")
}
fatalError("\(error)")
}

guard let data = data else {
Expand Down
13 changes: 13 additions & 0 deletions Realm/RLMApp.mm
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ + (instancetype)appWithId:(NSString *)appId
return app;
}

+ (instancetype)uncachedAppWithId:(NSString *)appId
configuration:(RLMAppConfiguration *)configuration
rootDirectory:(NSURL *)rootDirectory {
REALM_ASSERT(appId.length);

[configuration setAppId:appId];
auto app = RLMTranslateError([&] {
return app::App::get_uncached_app(configuration.config,
[RLMSyncManager configurationWithRootDirectory:rootDirectory appId:appId]);
});
return [[RLMApp alloc] initWithApp:app];
}

+ (instancetype)appWithId:(NSString *)appId configuration:(RLMAppConfiguration *)configuration {
return [self appWithId:appId configuration:configuration rootDirectory:nil];
}
Expand Down
4 changes: 4 additions & 0 deletions Realm/RLMApp_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ typedef void(^RLMAppNotificationBlock)(RLMApp *);
configuration:(nullable RLMAppConfiguration *)configuration
rootDirectory:(nullable NSURL *)rootDirectory;

+ (instancetype)uncachedAppWithId:(NSString *)appId
configuration:(RLMAppConfiguration *)configuration
rootDirectory:(nullable NSURL *)rootDirectory;

+ (void)resetAppCache;
@end

Expand Down
10 changes: 0 additions & 10 deletions Realm/RLMApp_Private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,13 @@
RLM_HEADER_AUDIT_BEGIN(nullability, sendability)

@interface RLMAppConfiguration ()

- (realm::app::App::Config&)config;

- (instancetype)initWithConfig:(const realm::app::App::Config&)config;

@end

@interface RLMApp ()

- (std::shared_ptr<realm::app::App>)_realmApp;

+ (instancetype)appWithId:(NSString *)appId
configuration:(nullable RLMAppConfiguration *)configuration
rootDirectory:(nullable NSURL *)rootDirectory;

- (instancetype)initWithApp:(std::shared_ptr<realm::app::App>)app;

@end

NSError * makeError(realm::app::AppError const& appError);
Expand Down

0 comments on commit e9a2daa

Please sign in to comment.