Skip to content

Commit 6446366

Browse files
committed
Make async_open_realm() return StatusWith<std::shared_ptr<Realm>>
This simplifies a lot of test code and eliminates some cases where the Realm was being opened on a background thread, which is unsupported on linux.
1 parent eef91de commit 6446366

File tree

5 files changed

+95
-168
lines changed

5 files changed

+95
-168
lines changed

test/object-store/sync/flx_migration.cpp

+16-26
Original file line numberDiff line numberDiff line change
@@ -908,21 +908,16 @@ TEST_CASE("Async open + client reset", "[sync][flx][flx migration][baas]") {
908908
shared_object.persisted_properties.push_back({"oid_field", PropertyType::ObjectId | PropertyType::Nullable});
909909
config->schema = {shared_object, locally_added};
910910

911-
async_open_realm(*config, [&](ThreadSafeReference&& ref, std::exception_ptr error) {
912-
REQUIRE(ref);
913-
REQUIRE_FALSE(error);
911+
auto realm = async_open_realm(*config).get_value();
914912

915-
auto realm = Realm::get_shared_realm(std::move(ref));
916-
917-
auto table = realm->read_group().get_table("class_Object");
918-
REQUIRE(table->size() == 0);
919-
REQUIRE(num_before_reset_notifications == 1);
920-
REQUIRE(num_after_reset_notifications == 1);
913+
auto table = realm->read_group().get_table("class_Object");
914+
REQUIRE(table->size() == 0);
915+
REQUIRE(num_before_reset_notifications == 1);
916+
REQUIRE(num_after_reset_notifications == 1);
921917

922-
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
923-
REQUIRE(locally_added_table);
924-
REQUIRE(locally_added_table->size() == 0);
925-
});
918+
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
919+
REQUIRE(locally_added_table);
920+
REQUIRE(locally_added_table->size() == 0);
926921
}
927922

928923
SECTION("initial state") {
@@ -941,21 +936,16 @@ TEST_CASE("Async open + client reset", "[sync][flx][flx migration][baas]") {
941936
{"oid_field", PropertyType::ObjectId | PropertyType::Nullable});
942937
config->schema = {shared_object, locally_added};
943938

944-
async_open_realm(*config, [&](ThreadSafeReference&& ref, std::exception_ptr error) {
945-
REQUIRE(ref);
946-
REQUIRE_FALSE(error);
939+
auto realm = async_open_realm(*config).get_value();
947940

948-
auto realm = Realm::get_shared_realm(std::move(ref));
949-
950-
auto table = realm->read_group().get_table("class_Object");
951-
REQUIRE(table->size() == 1);
952-
REQUIRE(num_before_reset_notifications == 1);
953-
REQUIRE(num_after_reset_notifications == 1);
941+
auto table = realm->read_group().get_table("class_Object");
942+
REQUIRE(table->size() == 1);
943+
REQUIRE(num_before_reset_notifications == 1);
944+
REQUIRE(num_after_reset_notifications == 1);
954945

955-
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
956-
REQUIRE(locally_added_table);
957-
REQUIRE(locally_added_table->size() == 0);
958-
});
946+
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
947+
REQUIRE(locally_added_table);
948+
REQUIRE(locally_added_table->size() == 0);
959949
}
960950
}
961951
}

test/object-store/sync/flx_schema_migration.cpp

+19-66
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,6 @@ void create_schema(const AppSession& app_session, Schema target_schema, int64_t
6868
});
6969
}
7070

71-
std::pair<SharedRealm, std::exception_ptr> async_open_realm(const Realm::Config& config)
72-
{
73-
auto task = Realm::get_synchronized_realm(config);
74-
ThreadSafeReference tsr;
75-
SharedRealm realm;
76-
std::exception_ptr err = nullptr;
77-
auto pf = util::make_promise_future<void>();
78-
task->start([&tsr, &err, promise = util::CopyablePromiseHolder(std::move(pf.promise))](
79-
ThreadSafeReference&& ref, std::exception_ptr e) mutable {
80-
tsr = std::move(ref);
81-
err = e;
82-
promise.get_promise().emplace_value();
83-
});
84-
pf.future.get();
85-
realm = err ? nullptr : Realm::get_shared_realm(std::move(tsr));
86-
return std::pair(realm, err);
87-
}
88-
8971
std::vector<ObjectSchema> get_schema_v0()
9072
{
9173
return {
@@ -373,20 +355,17 @@ TEST_CASE("Cannot migrate schema to unknown version", "[sync][flx][flx schema mi
373355
config.sync_config->error_handler = err_handler;
374356

375357
{
376-
auto [realm, error] = async_open_realm(config);
377-
REQUIRE_FALSE(realm);
378-
REQUIRE(error);
379-
REQUIRE_THROWS_CONTAINING(std::rethrow_exception(error), "Client provided invalid schema version");
358+
auto status = async_open_realm(config);
359+
REQUIRE_FALSE(status.is_ok());
360+
REQUIRE_THAT(status.get_status().reason(), Catch::Matchers::ContainsSubstring("Client provided invalid schema version"));
380361
error_future.get();
381362
check_realm_schema(config.path, target_schema, target_schema_version);
382363
}
383364

384365
// Update schema version to 0 and try again (the version now matches the actual schema).
385366
config.schema_version = 0;
386367
config.sync_config->error_handler = nullptr;
387-
auto [realm, error] = async_open_realm(config);
388-
REQUIRE(realm);
389-
REQUIRE_FALSE(error);
368+
REQUIRE(async_open_realm(config).is_ok());
390369
check_realm_schema(config.path, schema_v0, 0);
391370
}
392371

@@ -443,11 +422,9 @@ TEST_CASE("Schema version mismatch between client and server", "[sync][flx][flx
443422
return SyncClientHookAction::NoAction;
444423
};
445424

446-
auto [realm, error] = async_open_realm(config);
447-
REQUIRE_FALSE(realm);
448-
REQUIRE(error);
449-
REQUIRE_THROWS_CONTAINING(std::rethrow_exception(error),
450-
"The following changes cannot be made in additive-only schema mode");
425+
auto status = async_open_realm(config);
426+
REQUIRE_FALSE(status.is_ok());
427+
REQUIRE_THAT(status.get_status().reason(), Catch::Matchers::ContainsSubstring("The following changes cannot be made in additive-only schema mode"));
451428
REQUIRE(schema_migration_required);
452429
// Applying the new schema (and version) fails, therefore the schema is unversioned (the metadata table is removed
453430
// during migration). There is a schema though because the server schema is already applied by the time the client
@@ -479,9 +456,7 @@ TEST_CASE("Fresh realm does not require schema migration", "[sync][flx][flx sche
479456
return SyncClientHookAction::NoAction;
480457
};
481458

482-
auto [realm, error] = async_open_realm(config);
483-
REQUIRE(realm);
484-
REQUIRE_FALSE(error);
459+
REQUIRE(async_open_realm(config).is_ok());
485460
check_realm_schema(config.path, schema_v1, 1);
486461
}
487462

@@ -564,9 +539,7 @@ TEST_CASE("Upgrade schema version (with recovery) then downgrade", "[sync][flx][
564539
config.schema_version = 1;
565540
config.schema = schema_v1;
566541
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v1();
567-
auto [realm, error] = async_open_realm(config);
568-
REQUIRE(realm);
569-
REQUIRE_FALSE(error);
542+
auto realm = successfully_async_open_realm(config);
570543
check_realm_schema(config.path, schema_v1, 1);
571544

572545
auto table = realm->read_group().get_table("class_TopLevel");
@@ -596,9 +569,7 @@ TEST_CASE("Upgrade schema version (with recovery) then downgrade", "[sync][flx][
596569
config.schema = schema_v2;
597570
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v2();
598571

599-
auto [realm, error] = async_open_realm(config);
600-
REQUIRE(realm);
601-
REQUIRE_FALSE(error);
572+
auto realm = successfully_async_open_realm(config);
602573
check_realm_schema(config.path, schema_v2, 2);
603574

604575
auto table = realm->read_group().get_table("class_TopLevel");
@@ -616,9 +587,7 @@ TEST_CASE("Upgrade schema version (with recovery) then downgrade", "[sync][flx][
616587
config.schema = schema_v1;
617588
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v1();
618589

619-
auto [realm, error] = async_open_realm(config);
620-
REQUIRE(realm);
621-
REQUIRE_FALSE(error);
590+
auto realm = successfully_async_open_realm(config);
622591
check_realm_schema(config.path, schema_v1, 1);
623592

624593
auto table = realm->read_group().get_table("class_TopLevel");
@@ -636,9 +605,7 @@ TEST_CASE("Upgrade schema version (with recovery) then downgrade", "[sync][flx][
636605
config.schema = schema_v0;
637606
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v0();
638607

639-
auto [realm, error] = async_open_realm(config);
640-
REQUIRE(realm);
641-
REQUIRE_FALSE(error);
608+
auto realm = successfully_async_open_realm(config);
642609
check_realm_schema(config.path, schema_v0, 0);
643610

644611
auto table = realm->read_group().get_table("class_TopLevel");
@@ -720,9 +687,7 @@ TEST_CASE("An interrupted schema migration can recover on the next session",
720687
}
721688

722689
// Retry the migration.
723-
auto [realm, error] = async_open_realm(config);
724-
REQUIRE(realm);
725-
REQUIRE_FALSE(error);
690+
REQUIRE(async_open_realm(config).is_ok());
726691
REQUIRE(schema_version_changed_count == 2);
727692
check_realm_schema(config.path, schema_v1, 1);
728693
}
@@ -752,9 +717,7 @@ TEST_CASE("Migrate to new schema version with a schema subset", "[sync][flx][flx
752717
config.schema = schema_subset;
753718
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v1();
754719

755-
auto [realm, error] = async_open_realm(config);
756-
REQUIRE(realm);
757-
REQUIRE_FALSE(error);
720+
REQUIRE(async_open_realm(config).is_ok());
758721
check_realm_schema(config.path, schema_v1, 1);
759722
}
760723

@@ -835,9 +798,7 @@ TEST_CASE("Client reset during schema migration", "[sync][flx][flx schema migrat
835798
++after_reset_count;
836799
};
837800

838-
auto [realm, error] = async_open_realm(config);
839-
REQUIRE(realm);
840-
REQUIRE_FALSE(error);
801+
auto realm = successfully_async_open_realm(config);
841802
REQUIRE(before_reset_count == 0);
842803
REQUIRE(after_reset_count == 0);
843804
check_realm_schema(config.path, schema_v1, 1);
@@ -926,9 +887,7 @@ TEST_CASE("Migrate to new schema version after migration to intermediate version
926887
config.schema_version = 2;
927888
config.schema = schema_v2;
928889
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v2();
929-
auto [realm, error] = async_open_realm(config);
930-
REQUIRE(realm);
931-
REQUIRE_FALSE(error);
890+
auto realm = successfully_async_open_realm(config);
932891
REQUIRE(schema_version_changed_count == 2);
933892
check_realm_schema(config.path, schema_v2, 2);
934893

@@ -951,9 +910,7 @@ TEST_CASE("Send schema version zero if no schema is used to open the realm",
951910

952911
config.schema = {};
953912
config.schema_version = -1; // override the schema version set by SyncTestFile constructor
954-
auto [realm, error] = async_open_realm(config);
955-
REQUIRE(realm);
956-
REQUIRE_FALSE(error);
913+
REQUIRE(async_open_realm(config).is_ok());
957914
// The schema is received from the server, but it is unversioned.
958915
check_realm_schema(config.path, schema_v0, ObjectStore::NotVersioned);
959916
}
@@ -1061,9 +1018,7 @@ TEST_CASE("Client reset and schema migration", "[sync][flx][flx schema migration
10611018
++after_reset_count;
10621019
};
10631020

1064-
auto [realm, error] = async_open_realm(config);
1065-
REQUIRE(realm);
1066-
REQUIRE_FALSE(error);
1021+
auto realm = successfully_async_open_realm(config);
10671022
REQUIRE(before_reset_count == 0);
10681023
REQUIRE(after_reset_count == 0);
10691024
check_realm_schema(config.path, schema_v1, 1);
@@ -1204,9 +1159,7 @@ TEST_CASE("Upgrade schema version with no subscription initializer", "[sync][flx
12041159
config.schema_version = 1;
12051160
config.schema = schema_v1;
12061161
config.sync_config->subscription_initializer = nullptr;
1207-
auto [realm, error] = async_open_realm(config);
1208-
REQUIRE(realm);
1209-
REQUIRE_FALSE(error);
1162+
auto realm = successfully_async_open_realm(config);
12101163
check_realm_schema(config.path, schema_v1, 1);
12111164

12121165
auto table = realm->read_group().get_table("class_TopLevel");

0 commit comments

Comments
 (0)