Skip to content

Commit

Permalink
Make async_open_realm() return StatusWith<std::shared_ptr<Realm>>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgoyne committed Jun 12, 2024
1 parent eef91de commit 6446366
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 168 deletions.
42 changes: 16 additions & 26 deletions test/object-store/sync/flx_migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,21 +908,16 @@ TEST_CASE("Async open + client reset", "[sync][flx][flx migration][baas]") {
shared_object.persisted_properties.push_back({"oid_field", PropertyType::ObjectId | PropertyType::Nullable});
config->schema = {shared_object, locally_added};

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

auto realm = Realm::get_shared_realm(std::move(ref));

auto table = realm->read_group().get_table("class_Object");
REQUIRE(table->size() == 0);
REQUIRE(num_before_reset_notifications == 1);
REQUIRE(num_after_reset_notifications == 1);
auto table = realm->read_group().get_table("class_Object");
REQUIRE(table->size() == 0);
REQUIRE(num_before_reset_notifications == 1);
REQUIRE(num_after_reset_notifications == 1);

auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
REQUIRE(locally_added_table);
REQUIRE(locally_added_table->size() == 0);
});
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
REQUIRE(locally_added_table);
REQUIRE(locally_added_table->size() == 0);
}

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

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

auto realm = Realm::get_shared_realm(std::move(ref));

auto table = realm->read_group().get_table("class_Object");
REQUIRE(table->size() == 1);
REQUIRE(num_before_reset_notifications == 1);
REQUIRE(num_after_reset_notifications == 1);
auto table = realm->read_group().get_table("class_Object");
REQUIRE(table->size() == 1);
REQUIRE(num_before_reset_notifications == 1);
REQUIRE(num_after_reset_notifications == 1);

auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
REQUIRE(locally_added_table);
REQUIRE(locally_added_table->size() == 0);
});
auto locally_added_table = realm->read_group().get_table("class_LocallyAdded");
REQUIRE(locally_added_table);
REQUIRE(locally_added_table->size() == 0);
}
}
}
Expand Down
85 changes: 19 additions & 66 deletions test/object-store/sync/flx_schema_migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,6 @@ void create_schema(const AppSession& app_session, Schema target_schema, int64_t
});
}

std::pair<SharedRealm, std::exception_ptr> async_open_realm(const Realm::Config& config)
{
auto task = Realm::get_synchronized_realm(config);
ThreadSafeReference tsr;
SharedRealm realm;
std::exception_ptr err = nullptr;
auto pf = util::make_promise_future<void>();
task->start([&tsr, &err, promise = util::CopyablePromiseHolder(std::move(pf.promise))](
ThreadSafeReference&& ref, std::exception_ptr e) mutable {
tsr = std::move(ref);
err = e;
promise.get_promise().emplace_value();
});
pf.future.get();
realm = err ? nullptr : Realm::get_shared_realm(std::move(tsr));
return std::pair(realm, err);
}

std::vector<ObjectSchema> get_schema_v0()
{
return {
Expand Down Expand Up @@ -373,20 +355,17 @@ TEST_CASE("Cannot migrate schema to unknown version", "[sync][flx][flx schema mi
config.sync_config->error_handler = err_handler;

{
auto [realm, error] = async_open_realm(config);
REQUIRE_FALSE(realm);
REQUIRE(error);
REQUIRE_THROWS_CONTAINING(std::rethrow_exception(error), "Client provided invalid schema version");
auto status = async_open_realm(config);
REQUIRE_FALSE(status.is_ok());
REQUIRE_THAT(status.get_status().reason(), Catch::Matchers::ContainsSubstring("Client provided invalid schema version"));
error_future.get();
check_realm_schema(config.path, target_schema, target_schema_version);
}

// Update schema version to 0 and try again (the version now matches the actual schema).
config.schema_version = 0;
config.sync_config->error_handler = nullptr;
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
REQUIRE(async_open_realm(config).is_ok());
check_realm_schema(config.path, schema_v0, 0);
}

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

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

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
REQUIRE(async_open_realm(config).is_ok());
check_realm_schema(config.path, schema_v1, 1);
}

Expand Down Expand Up @@ -564,9 +539,7 @@ TEST_CASE("Upgrade schema version (with recovery) then downgrade", "[sync][flx][
config.schema_version = 1;
config.schema = schema_v1;
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v1();
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
check_realm_schema(config.path, schema_v1, 1);

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

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
check_realm_schema(config.path, schema_v2, 2);

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

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
check_realm_schema(config.path, schema_v1, 1);

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

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
check_realm_schema(config.path, schema_v0, 0);

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

// Retry the migration.
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
REQUIRE(async_open_realm(config).is_ok());
REQUIRE(schema_version_changed_count == 2);
check_realm_schema(config.path, schema_v1, 1);
}
Expand Down Expand Up @@ -752,9 +717,7 @@ TEST_CASE("Migrate to new schema version with a schema subset", "[sync][flx][flx
config.schema = schema_subset;
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v1();

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
REQUIRE(async_open_realm(config).is_ok());
check_realm_schema(config.path, schema_v1, 1);
}

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

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
REQUIRE(before_reset_count == 0);
REQUIRE(after_reset_count == 0);
check_realm_schema(config.path, schema_v1, 1);
Expand Down Expand Up @@ -926,9 +887,7 @@ TEST_CASE("Migrate to new schema version after migration to intermediate version
config.schema_version = 2;
config.schema = schema_v2;
config.sync_config->subscription_initializer = get_subscription_initializer_callback_for_schema_v2();
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
REQUIRE(schema_version_changed_count == 2);
check_realm_schema(config.path, schema_v2, 2);

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

config.schema = {};
config.schema_version = -1; // override the schema version set by SyncTestFile constructor
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
REQUIRE(async_open_realm(config).is_ok());
// The schema is received from the server, but it is unversioned.
check_realm_schema(config.path, schema_v0, ObjectStore::NotVersioned);
}
Expand Down Expand Up @@ -1061,9 +1018,7 @@ TEST_CASE("Client reset and schema migration", "[sync][flx][flx schema migration
++after_reset_count;
};

auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
REQUIRE(before_reset_count == 0);
REQUIRE(after_reset_count == 0);
check_realm_schema(config.path, schema_v1, 1);
Expand Down Expand Up @@ -1204,9 +1159,7 @@ TEST_CASE("Upgrade schema version with no subscription initializer", "[sync][flx
config.schema_version = 1;
config.schema = schema_v1;
config.sync_config->subscription_initializer = nullptr;
auto [realm, error] = async_open_realm(config);
REQUIRE(realm);
REQUIRE_FALSE(error);
auto realm = successfully_async_open_realm(config);
check_realm_schema(config.path, schema_v1, 1);

auto table = realm->read_group().get_table("class_TopLevel");
Expand Down
Loading

0 comments on commit 6446366

Please sign in to comment.