Skip to content

Commit b2a419b

Browse files
committed
Use the successfully_async_open_realm() helper in more tests
1 parent 9efccbd commit b2a419b

File tree

1 file changed

+43
-86
lines changed

1 file changed

+43
-86
lines changed

test/object-store/realm.cpp

Lines changed: 43 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -943,27 +943,9 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
943943

944944
std::mutex mutex;
945945

946-
auto async_open_realm = [&](const Realm::Config& config) {
947-
ThreadSafeReference realm_ref;
948-
std::exception_ptr error;
949-
auto task = Realm::get_synchronized_realm(config);
950-
task->start([&](ThreadSafeReference&& ref, std::exception_ptr e) {
951-
std::lock_guard lock(mutex);
952-
realm_ref = std::move(ref);
953-
error = e;
954-
});
955-
util::EventLoop::main().run_until([&] {
956-
std::lock_guard lock(mutex);
957-
return realm_ref || error;
958-
});
959-
return std::pair(std::move(realm_ref), error);
960-
};
961-
962946
SECTION("can open synced Realms that don't already exist") {
963-
auto [ref, error] = async_open_realm(config);
964-
REQUIRE(ref);
965-
REQUIRE_FALSE(error);
966-
REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object"));
947+
auto realm = successfully_async_open_realm(config);
948+
REQUIRE(realm->read_group().get_table("class_object"));
967949
}
968950

969951
SECTION("can write a realm file without client file id") {
@@ -982,11 +964,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
982964

983965
// Create realm file without client file id
984966
{
985-
auto [ref, error] = async_open_realm(config);
986-
REQUIRE(ref);
987-
REQUIRE_FALSE(error);
967+
auto realm = successfully_async_open_realm(config);
988968
// Write some data
989-
SharedRealm realm = Realm::get_shared_realm(std::move(ref));
990969
realm->begin_transaction();
991970
realm->get_class("object").create_object(2);
992971
realm->commit_transaction();
@@ -1034,10 +1013,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
10341013
wait_for_upload(*realm);
10351014
}
10361015

1037-
auto [ref, error] = async_open_realm(config);
1038-
REQUIRE(ref);
1039-
REQUIRE_FALSE(error);
1040-
REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object"));
1016+
auto realm = successfully_async_open_realm(config);
1017+
REQUIRE(realm->read_group().get_table("class_object"));
10411018
}
10421019

10431020
SECTION("progress notifiers of a task are cancelled if the task is cancelled") {
@@ -1112,10 +1089,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
11121089
wait_for_upload(*realm);
11131090
}
11141091

1115-
auto [ref, error] = async_open_realm(config);
1116-
REQUIRE(ref);
1117-
REQUIRE_FALSE(error);
1118-
REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object")->size() == 1);
1092+
auto realm = successfully_async_open_realm(config);
1093+
REQUIRE(realm->read_group().get_table("class_object")->size() == 1);
11191094
}
11201095

11211096
SECTION("can download multiple Realms at a time") {
@@ -1261,9 +1236,9 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
12611236
app_config.base_file_path = util::make_temp_dir();
12621237
app_config.metadata_mode = app::AppConfig::MetadataMode::NoEncryption;
12631238

1264-
auto the_app = app::App::get_app(app::App::CacheMode::Disabled, app_config);
1265-
create_user_and_log_in(the_app);
1266-
auto user = the_app->current_user();
1239+
auto app = app::App::get_app(app::App::CacheMode::Disabled, app_config);
1240+
create_user_and_log_in(app);
1241+
auto user = app->current_user();
12671242
// User should be logged in at this point
12681243
REQUIRE(user->is_logged_in());
12691244

@@ -1274,22 +1249,20 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
12741249
TestMode test_mode = GENERATE(expired_at_start, expired_by_websocket, websocket_fails);
12751250
FailureMode failure = GENERATE(location_fails, token_fails, token_not_authorized);
12761251

1277-
DYNAMIC_SECTION(txt_test_mode(test_mode) << " - " << txt_failure_mode(failure)) {
1278-
logger->info("TEST: %1 - %2", txt_test_mode(test_mode), txt_failure_mode(failure));
1279-
if (test_mode == TestMode::expired_at_start) {
1280-
// invalidate the user's cached access token
1281-
auto app_user = the_app->current_user();
1282-
app_user->update_data_for_testing([&](app::UserData& data) {
1283-
data.access_token = RealmJWT(expired_token);
1284-
});
1285-
}
1286-
else if (test_mode == TestMode::expired_by_websocket) {
1287-
// tell websocket to return not authorized to refresh access token
1288-
not_authorized = true;
1289-
}
1252+
logger->info("TEST: %1 - %2", txt_test_mode(test_mode), txt_failure_mode(failure));
1253+
if (test_mode == TestMode::expired_at_start) {
1254+
// invalidate the user's cached access token
1255+
auto app_user = app->current_user();
1256+
app_user->update_data_for_testing([&](app::UserData& data) {
1257+
data.access_token = RealmJWT(expired_token);
1258+
});
1259+
}
1260+
else if (test_mode == TestMode::expired_by_websocket) {
1261+
// tell websocket to return not authorized to refresh access token
1262+
not_authorized = true;
12901263
}
12911264

1292-
the_app.reset();
1265+
app.reset();
12931266

12941267
auto err_handler = [](std::shared_ptr<SyncSession> session, SyncError error) {
12951268
auto logger = util::Logger::get_default_logger();
@@ -1336,8 +1309,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
13361309
"Operation timed out");
13371310
};
13381311

1339-
the_app = app::App::get_app(app::App::CacheMode::Disabled, app_config);
1340-
SyncTestFile config(the_app->current_user(), "realm");
1312+
app = app::App::get_app(app::App::CacheMode::Disabled, app_config);
1313+
SyncTestFile config(app->current_user(), "realm");
13411314
config.sync_config->cancel_waits_on_nonfatal_error = true;
13421315
config.sync_config->error_handler = err_handler;
13431316

@@ -1368,10 +1341,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
13681341
}
13691342

13701343
config2.schema_mode = SchemaMode::ReadOnly;
1371-
auto [ref, error] = async_open_realm(config2);
1372-
REQUIRE(ref);
1373-
REQUIRE_FALSE(error);
1374-
REQUIRE(Realm::get_shared_realm(std::move(ref))->schema_version() == 1);
1344+
auto realm = successfully_async_open_realm(config2);
1345+
REQUIRE(realm->schema_version() == 1);
13751346
}
13761347

13771348
Schema with_added_object = Schema{object_schema,
@@ -1395,10 +1366,7 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
13951366

13961367
// Verify that the table gets added when reopening
13971368
config2.schema_mode = SchemaMode::ReadOnly;
1398-
auto [ref, error] = async_open_realm(config2);
1399-
REQUIRE(ref);
1400-
REQUIRE_FALSE(error);
1401-
auto realm = Realm::get_shared_realm(std::move(ref));
1369+
auto realm = successfully_async_open_realm(config2);
14021370
REQUIRE(realm->schema().find("added") != realm->schema().end());
14031371
REQUIRE(realm->read_group().get_table("class_added"));
14041372
}
@@ -1409,10 +1377,7 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
14091377

14101378
config2.schema = with_added_object;
14111379
config2.schema_mode = SchemaMode::ReadOnly;
1412-
auto [ref, error] = async_open_realm(config2);
1413-
REQUIRE(ref);
1414-
REQUIRE_FALSE(error);
1415-
auto realm = Realm::get_shared_realm(std::move(ref));
1380+
auto realm = successfully_async_open_realm(config2);
14161381
REQUIRE(realm->schema().find("added") != realm->schema().end());
14171382
REQUIRE_FALSE(realm->read_group().get_table("class_added"));
14181383
}
@@ -1429,10 +1394,10 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
14291394
{"value2", PropertyType::Int},
14301395
}}};
14311396

1432-
auto [ref, error] = async_open_realm(config);
1433-
REQUIRE_FALSE(ref);
1434-
REQUIRE(error);
1435-
REQUIRE_THROWS_CONTAINING(std::rethrow_exception(error), "Property 'object.value2' has been added.");
1397+
auto status = async_open_realm(config);
1398+
REQUIRE_FALSE(status.is_ok());
1399+
REQUIRE_THAT(status.get_status().reason(),
1400+
Catch::Matchers::ContainsSubstring("Property 'object.value2' has been added."));
14361401
}
14371402

14381403
SECTION("adding a property to an existing read-only Realm reports an error") {
@@ -1447,10 +1412,10 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
14471412
}}};
14481413
REQUIRE_THROWS_CONTAINING(Realm::get_shared_realm(config), "Property 'object.value2' has been added.");
14491414

1450-
auto [ref, error] = async_open_realm(config);
1451-
REQUIRE_FALSE(ref);
1452-
REQUIRE(error);
1453-
REQUIRE_THROWS_CONTAINING(std::rethrow_exception(error), "Property 'object.value2' has been added.");
1415+
auto status = async_open_realm(config);
1416+
REQUIRE_FALSE(status.is_ok());
1417+
REQUIRE_THAT(status.get_status().reason(),
1418+
Catch::Matchers::ContainsSubstring("Property 'object.value2' has been added."));
14541419
}
14551420

14561421
SECTION("removing a property from a newly downloaded read-only Realm leaves the column in place") {
@@ -1464,13 +1429,8 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
14641429
{"_id", PropertyType::Int, Property::IsPrimary{true}},
14651430
}}};
14661431

1467-
auto [ref, error] = async_open_realm(config);
1468-
REQUIRE(ref);
1469-
REQUIRE_FALSE(error);
1470-
REQUIRE(Realm::get_shared_realm(std::move(ref))
1471-
->read_group()
1472-
.get_table("class_object")
1473-
->get_column_key("value") != ColKey{});
1432+
auto realm = successfully_async_open_realm(config);
1433+
REQUIRE(realm->read_group().get_table("class_object")->get_column_key("value") != ColKey{});
14741434
}
14751435

14761436
SECTION("removing a property from a existing read-only Realm leaves the column in place") {
@@ -1483,19 +1443,16 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
14831443
{"_id", PropertyType::Int, Property::IsPrimary{true}},
14841444
}}};
14851445

1486-
auto [ref, error] = async_open_realm(config);
1487-
REQUIRE(ref);
1488-
REQUIRE_FALSE(error);
1489-
REQUIRE(Realm::get_shared_realm(std::move(ref))
1490-
->read_group()
1491-
.get_table("class_object")
1492-
->get_column_key("value") != ColKey{});
1446+
auto realm = successfully_async_open_realm(config);
1447+
REQUIRE(realm->read_group().get_table("class_object")->get_column_key("value") != ColKey{});
14931448
}
1449+
1450+
_impl::RealmCoordinator::assert_no_open_realms();
14941451
}
14951452

14961453
#if REALM_ENABLE_AUTH_TESTS
14971454

1498-
TEST_CASE("Syhcnronized realm: AutoOpen", "[sync][baas][pbs][async open]") {
1455+
TEST_CASE("Synchronized realm: AutoOpen", "[sync][baas][pbs][async open]") {
14991456
const auto partition = random_string(100);
15001457
auto schema = get_default_schema();
15011458
enum TestMode { expired_at_start, expired_by_websocket, websocket_fails };

0 commit comments

Comments
 (0)