realm.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #include "catch2/catch.hpp"
  19. #include "util/event_loop.hpp"
  20. #include "util/test_file.hpp"
  21. #include "util/test_utils.hpp"
  22. #include "binding_context.hpp"
  23. #include "impl/realm_coordinator.hpp"
  24. #include "object_schema.hpp"
  25. #include "object_store.hpp"
  26. #include "property.hpp"
  27. #include "results.hpp"
  28. #include "schema.hpp"
  29. #include "thread_safe_reference.hpp"
  30. #include "util/scheduler.hpp"
  31. #include <realm/db.hpp>
  32. #if REALM_ENABLE_SYNC
  33. #include "sync/async_open_task.hpp"
  34. #endif
  35. #include <realm/util/scope_exit.hpp>
  36. namespace realm {
  37. class TestHelper {
  38. public:
  39. static DBRef& get_db(SharedRealm const& shared_realm)
  40. {
  41. return Realm::Internal::get_db(*shared_realm);
  42. }
  43. static void begin_read(SharedRealm const& shared_realm, VersionID version)
  44. {
  45. Realm::Internal::begin_read(*shared_realm, version);
  46. }
  47. };
  48. }
  49. using namespace realm;
  50. TEST_CASE("SharedRealm: get_shared_realm()") {
  51. TestFile config;
  52. config.schema_version = 1;
  53. config.schema = Schema{
  54. {"object", {
  55. {"value", PropertyType::Int}
  56. }},
  57. };
  58. SECTION("should validate that the config is sensible") {
  59. SECTION("bad encryption key") {
  60. config.encryption_key = std::vector<char>(2, 0);
  61. REQUIRE_THROWS(Realm::get_shared_realm(config));
  62. }
  63. SECTION("schema without schema version") {
  64. config.schema_version = ObjectStore::NotVersioned;
  65. REQUIRE_THROWS(Realm::get_shared_realm(config));
  66. }
  67. SECTION("migration function for immutable") {
  68. config.schema_mode = SchemaMode::Immutable;
  69. config.migration_function = [](auto, auto, auto) { };
  70. REQUIRE_THROWS(Realm::get_shared_realm(config));
  71. }
  72. SECTION("migration function for read-only") {
  73. config.schema_mode = SchemaMode::ReadOnlyAlternative;
  74. config.migration_function = [](auto, auto, auto) { };
  75. REQUIRE_THROWS(Realm::get_shared_realm(config));
  76. }
  77. SECTION("migration function for additive-only") {
  78. config.schema_mode = SchemaMode::Additive;
  79. config.migration_function = [](auto, auto, auto) { };
  80. REQUIRE_THROWS(Realm::get_shared_realm(config));
  81. }
  82. SECTION("initialization function for immutable") {
  83. config.schema_mode = SchemaMode::Immutable;
  84. config.initialization_function = [](auto) { };
  85. REQUIRE_THROWS(Realm::get_shared_realm(config));
  86. }
  87. SECTION("initialization function for read-only") {
  88. config.schema_mode = SchemaMode::ReadOnlyAlternative;
  89. config.initialization_function = [](auto) { };
  90. REQUIRE_THROWS(Realm::get_shared_realm(config));
  91. }
  92. }
  93. SECTION("should reject mismatched config") {
  94. SECTION("schema version") {
  95. auto realm = Realm::get_shared_realm(config);
  96. config.schema_version = 2;
  97. REQUIRE_THROWS(Realm::get_shared_realm(config));
  98. config.schema = util::none;
  99. config.schema_version = ObjectStore::NotVersioned;
  100. REQUIRE_NOTHROW(Realm::get_shared_realm(config));
  101. }
  102. SECTION("schema mode") {
  103. auto realm = Realm::get_shared_realm(config);
  104. config.schema_mode = SchemaMode::Manual;
  105. REQUIRE_THROWS(Realm::get_shared_realm(config));
  106. }
  107. SECTION("durability") {
  108. auto realm = Realm::get_shared_realm(config);
  109. config.in_memory = true;
  110. REQUIRE_THROWS(Realm::get_shared_realm(config));
  111. }
  112. SECTION("schema") {
  113. auto realm = Realm::get_shared_realm(config);
  114. config.schema = Schema{
  115. {"object", {
  116. {"value", PropertyType::Int},
  117. {"value2", PropertyType::Int}
  118. }},
  119. };
  120. REQUIRE_THROWS(Realm::get_shared_realm(config));
  121. }
  122. }
  123. // Windows doesn't use fifos
  124. #ifndef _WIN32
  125. SECTION("should be able to set a FIFO fallback path") {
  126. std::string fallback_dir = tmp_dir() + "/fallback/";
  127. realm::util::try_make_dir(fallback_dir);
  128. TestFile config;
  129. config.fifo_files_fallback_path = fallback_dir;
  130. config.schema_version = 1;
  131. config.schema = Schema{
  132. {"object", {
  133. {"value", PropertyType::Int}
  134. }},
  135. };
  136. realm::util::make_dir(config.path + ".note");
  137. auto realm = Realm::get_shared_realm(config);
  138. auto fallback_file = util::format("%1realm_%2.note", fallback_dir, std::hash<std::string>()(config.path)); // Mirror internal implementation
  139. REQUIRE(File::exists(fallback_file));
  140. realm::util::remove_dir(config.path + ".note");
  141. realm::util::remove_dir_recursive(fallback_dir);
  142. }
  143. SECTION("automatically append dir separator to end of fallback path") {
  144. std::string fallback_dir = tmp_dir() + "/fallback";
  145. realm::util::try_make_dir(fallback_dir);
  146. TestFile config;
  147. config.fifo_files_fallback_path = fallback_dir;
  148. config.schema_version = 1;
  149. config.schema = Schema{
  150. {"object", {
  151. {"value", PropertyType::Int}
  152. }},
  153. };
  154. realm::util::make_dir(config.path + ".note");
  155. auto realm = Realm::get_shared_realm(config);
  156. auto fallback_file = util::format("%1/realm_%2.note", fallback_dir, std::hash<std::string>()(config.path)); // Mirror internal implementation
  157. REQUIRE(File::exists(fallback_file));
  158. realm::util::remove_dir(config.path + ".note");
  159. realm::util::remove_dir_recursive(fallback_dir);
  160. }
  161. #endif
  162. SECTION("should verify that the schema is valid") {
  163. config.schema = Schema{
  164. {"object",
  165. {{"value", PropertyType::Int}},
  166. {{"invalid backlink", PropertyType::LinkingObjects|PropertyType::Array, "object", "value"}}
  167. }
  168. };
  169. REQUIRE_THROWS_WITH(Realm::get_shared_realm(config),
  170. Catch::Matchers::Contains("origin of linking objects property"));
  171. }
  172. SECTION("should apply the schema if one is supplied") {
  173. Realm::get_shared_realm(config);
  174. {
  175. Group g(config.path);
  176. auto table = ObjectStore::table_for_object_type(g, "object");
  177. REQUIRE(table);
  178. REQUIRE(table->get_column_count() == 1);
  179. REQUIRE(table->get_column_name(*table->get_column_keys().begin()) == "value");
  180. }
  181. config.schema_version = 2;
  182. config.schema = Schema{
  183. {"object", {
  184. {"value", PropertyType::Int},
  185. {"value2", PropertyType::Int}
  186. }},
  187. };
  188. bool migration_called = false;
  189. config.migration_function = [&](SharedRealm old_realm, SharedRealm new_realm, Schema&) {
  190. migration_called = true;
  191. REQUIRE(ObjectStore::table_for_object_type(old_realm->read_group(), "object")->get_column_count() == 1);
  192. REQUIRE(ObjectStore::table_for_object_type(new_realm->read_group(), "object")->get_column_count() == 2);
  193. };
  194. Realm::get_shared_realm(config);
  195. REQUIRE(migration_called);
  196. }
  197. SECTION("should properly roll back from migration errors") {
  198. Realm::get_shared_realm(config);
  199. config.schema_version = 2;
  200. config.schema = Schema{
  201. {"object", {
  202. {"value", PropertyType::Int},
  203. {"value2", PropertyType::Int}
  204. }},
  205. };
  206. bool migration_called = false;
  207. config.migration_function = [&](SharedRealm old_realm, SharedRealm new_realm, Schema&) {
  208. REQUIRE(ObjectStore::table_for_object_type(old_realm->read_group(), "object")->get_column_count() == 1);
  209. REQUIRE(ObjectStore::table_for_object_type(new_realm->read_group(), "object")->get_column_count() == 2);
  210. if (!migration_called) {
  211. migration_called = true;
  212. throw "error";
  213. }
  214. };
  215. REQUIRE_THROWS_WITH(Realm::get_shared_realm(config), "error");
  216. REQUIRE(migration_called);
  217. REQUIRE_NOTHROW(Realm::get_shared_realm(config));
  218. }
  219. SECTION("should read the schema from the file if none is supplied") {
  220. Realm::get_shared_realm(config);
  221. config.schema = util::none;
  222. auto realm = Realm::get_shared_realm(config);
  223. REQUIRE(realm->schema().size() == 1);
  224. auto it = realm->schema().find("object");
  225. auto table = realm->read_group().get_table("class_object");
  226. REQUIRE(it != realm->schema().end());
  227. REQUIRE(it->table_key == table->get_key());
  228. REQUIRE(it->persisted_properties.size() == 1);
  229. REQUIRE(it->persisted_properties[0].name == "value");
  230. REQUIRE(it->persisted_properties[0].column_key == table->get_column_key("value"));
  231. }
  232. SECTION("should read the proper schema from the file if a custom version is supplied") {
  233. Realm::get_shared_realm(config);
  234. config.schema = util::none;
  235. config.schema_mode = SchemaMode::Additive;
  236. config.schema_version = 0;
  237. auto realm = Realm::get_shared_realm(config);
  238. REQUIRE(realm->schema().size() == 1);
  239. auto& db = TestHelper::get_db(realm);
  240. auto rt = db->start_read();
  241. VersionID old_version = rt->get_version_of_current_transaction();
  242. rt = nullptr;
  243. realm->close();
  244. config.schema = Schema{
  245. {"object", {
  246. {"value", PropertyType::Int}
  247. }},
  248. {"object1", {
  249. {"value", PropertyType::Int}
  250. }},
  251. };
  252. config.schema_version = 1;
  253. realm = Realm::get_shared_realm(config);
  254. REQUIRE(realm->schema().size() == 2);
  255. config.schema = util::none;
  256. auto old_realm = Realm::get_shared_realm(config);
  257. TestHelper::begin_read(old_realm, old_version);
  258. REQUIRE(old_realm->schema().size() == 1);
  259. }
  260. SECTION("should sensibly handle opening an uninitialized file without a schema specified") {
  261. // create an empty file
  262. File(config.path, File::mode_Write);
  263. // open the empty file, but don't initialize the schema
  264. Realm::Config config_without_schema = config;
  265. config_without_schema.schema = util::none;
  266. config_without_schema.schema_version = ObjectStore::NotVersioned;
  267. auto realm = Realm::get_shared_realm(config_without_schema);
  268. REQUIRE(realm->schema().empty());
  269. REQUIRE(realm->schema_version() == ObjectStore::NotVersioned);
  270. // verify that we can get another Realm instance
  271. REQUIRE_NOTHROW(Realm::get_shared_realm(config_without_schema));
  272. // verify that we can also still open the file with a proper schema
  273. auto realm2 = Realm::get_shared_realm(config);
  274. REQUIRE_FALSE(realm2->schema().empty());
  275. REQUIRE(realm2->schema_version() == 1);
  276. }
  277. SECTION("should populate the table columns in the schema when opening as immutable") {
  278. Realm::get_shared_realm(config);
  279. config.schema_mode = SchemaMode::Immutable;
  280. auto realm = Realm::get_shared_realm(config);
  281. auto it = realm->schema().find("object");
  282. auto table = realm->read_group().get_table("class_object");
  283. REQUIRE(it != realm->schema().end());
  284. REQUIRE(it->table_key == table->get_key());
  285. REQUIRE(it->persisted_properties.size() == 1);
  286. REQUIRE(it->persisted_properties[0].name == "value");
  287. REQUIRE(it->persisted_properties[0].column_key == table->get_column_key("value"));
  288. }
  289. SECTION("should support using different table subsets on different threads") {
  290. auto realm1 = Realm::get_shared_realm(config);
  291. config.schema = Schema{
  292. {"object 2", {
  293. {"value", PropertyType::Int}
  294. }},
  295. };
  296. auto realm2 = Realm::get_shared_realm(config);
  297. config.schema = util::none;
  298. auto realm3 = Realm::get_shared_realm(config);
  299. config.schema = Schema{
  300. {"object", {
  301. {"value", PropertyType::Int}
  302. }},
  303. };
  304. auto realm4 = Realm::get_shared_realm(config);
  305. realm1->refresh();
  306. realm2->refresh();
  307. REQUIRE(realm1->schema().size() == 1);
  308. REQUIRE(realm1->schema().find("object") != realm1->schema().end());
  309. REQUIRE(realm2->schema().size() == 1);
  310. REQUIRE(realm2->schema().find("object 2") != realm2->schema().end());
  311. REQUIRE(realm3->schema().size() == 2);
  312. REQUIRE(realm3->schema().find("object") != realm3->schema().end());
  313. REQUIRE(realm3->schema().find("object 2") != realm3->schema().end());
  314. REQUIRE(realm4->schema().size() == 1);
  315. REQUIRE(realm4->schema().find("object") != realm4->schema().end());
  316. }
  317. // The ExternalCommitHelper implementation on Windows doesn't rely on files
  318. #ifndef _WIN32
  319. SECTION("should throw when creating the notification pipe fails") {
  320. util::try_make_dir(config.path + ".note");
  321. auto sys_fallback_file = util::format("%1realm_%2.note", DBOptions::get_sys_tmp_dir(), std::hash<std::string>()(config.path)); // Mirror internal implementation
  322. util::try_make_dir(sys_fallback_file);
  323. REQUIRE_THROWS(Realm::get_shared_realm(config));
  324. util::remove_dir(config.path + ".note");
  325. util::remove_dir(sys_fallback_file);
  326. }
  327. #endif
  328. SECTION("should detect use of Realm on incorrect thread") {
  329. auto realm = Realm::get_shared_realm(config);
  330. std::thread([&]{
  331. REQUIRE_THROWS_AS(realm->verify_thread(), IncorrectThreadException);
  332. }).join();
  333. }
  334. SECTION("should not modify the schema when fetching from the cache") {
  335. auto realm = Realm::get_shared_realm(config);
  336. auto object_schema = &*realm->schema().find("object");
  337. Realm::get_shared_realm(config);
  338. REQUIRE(object_schema == &*realm->schema().find("object"));
  339. }
  340. }
  341. #if REALM_ENABLE_SYNC
  342. TEST_CASE("Get Realm using Async Open", "[asyncOpen]") {
  343. TestFile local_config;
  344. local_config.schema_version = 1;
  345. local_config.schema = Schema{
  346. {"object", {
  347. {"value", PropertyType::Int}
  348. }},
  349. };
  350. if (!util::EventLoop::has_implementation())
  351. return;
  352. TestSyncManager init_sync_manager;
  353. SyncServer server;
  354. SyncTestFile config(server, "default");
  355. config.schema = Schema{
  356. {"object", {
  357. {"value", PropertyType::Int},
  358. }},
  359. };
  360. SyncTestFile config2(server, "default");
  361. config2.schema = config.schema;
  362. std::mutex mutex;
  363. SECTION("can open synced Realms that don't already exist") {
  364. std::atomic<bool> called{false};
  365. auto task = Realm::get_synchronized_realm(config);
  366. task->start([&](auto ref, auto error) {
  367. std::lock_guard<std::mutex> lock(mutex);
  368. REQUIRE(!error);
  369. called = true;
  370. REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object"));
  371. });
  372. util::EventLoop::main().run_until([&]{ return called.load(); });
  373. std::lock_guard<std::mutex> lock(mutex);
  374. REQUIRE(called);
  375. }
  376. SECTION("downloads Realms which exist on the server") {
  377. {
  378. auto realm = Realm::get_shared_realm(config2);
  379. realm->begin_transaction();
  380. realm->read_group().get_table("class_object")->create_object();
  381. realm->commit_transaction();
  382. wait_for_upload(*realm);
  383. }
  384. std::atomic<bool> called{false};
  385. auto task = Realm::get_synchronized_realm(config);
  386. task->start([&](auto ref, auto error) {
  387. std::lock_guard<std::mutex> lock(mutex);
  388. REQUIRE(!error);
  389. called = true;
  390. REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object"));
  391. });
  392. util::EventLoop::main().run_until([&]{ return called.load(); });
  393. std::lock_guard<std::mutex> lock(mutex);
  394. REQUIRE(called);
  395. }
  396. SECTION("downloads latest state for Realms which already exist locally") {
  397. wait_for_upload(*Realm::get_shared_realm(config));
  398. {
  399. auto realm = Realm::get_shared_realm(config2);
  400. realm->begin_transaction();
  401. realm->read_group().get_table("class_object")->create_object();
  402. realm->commit_transaction();
  403. wait_for_upload(*realm);
  404. }
  405. std::atomic<bool> called{false};
  406. auto task = Realm::get_synchronized_realm(config);
  407. task->start([&](auto ref, auto error) {
  408. std::lock_guard<std::mutex> lock(mutex);
  409. REQUIRE(!error);
  410. called = true;
  411. REQUIRE(Realm::get_shared_realm(std::move(ref))->read_group().get_table("class_object")->size() == 1);
  412. });
  413. util::EventLoop::main().run_until([&]{ return called.load(); });
  414. std::lock_guard<std::mutex> lock(mutex);
  415. REQUIRE(called);
  416. }
  417. SECTION("can download partial Realms") {
  418. config.sync_config->is_partial = true;
  419. config2.sync_config->is_partial = true;
  420. {
  421. auto realm = Realm::get_shared_realm(config2);
  422. realm->begin_transaction();
  423. realm->read_group().get_table("class_object")->create_object();
  424. realm->commit_transaction();
  425. wait_for_upload(*realm);
  426. }
  427. std::atomic<bool> called{false};
  428. auto task = Realm::get_synchronized_realm(config);
  429. task->start([&](auto, auto error) {
  430. std::lock_guard<std::mutex> lock(mutex);
  431. REQUIRE(!error);
  432. called = true;
  433. });
  434. util::EventLoop::main().run_until([&]{ return called.load(); });
  435. std::lock_guard<std::mutex> lock(mutex);
  436. REQUIRE(called);
  437. // No subscriptions, so no objects
  438. REQUIRE(Realm::get_shared_realm(config)->read_group().get_table("class_object")->size() == 0);
  439. }
  440. SECTION("can download multiple Realms at a time") {
  441. SyncTestFile config1(server, "realm1");
  442. SyncTestFile config2(server, "realm2");
  443. SyncTestFile config3(server, "realm3");
  444. SyncTestFile config4(server, "realm4");
  445. std::vector<std::shared_ptr<AsyncOpenTask>> tasks = {
  446. Realm::get_synchronized_realm(config1),
  447. Realm::get_synchronized_realm(config2),
  448. Realm::get_synchronized_realm(config3),
  449. Realm::get_synchronized_realm(config4),
  450. };
  451. std::atomic<int> completed{0};
  452. for (auto& task : tasks) {
  453. task->start([&](auto, auto) {
  454. ++completed;
  455. });
  456. }
  457. util::EventLoop::main().run_until([&]{ return completed == 4; });
  458. }
  459. }
  460. #endif
  461. TEST_CASE("SharedRealm: notifications") {
  462. if (!util::EventLoop::has_implementation())
  463. return;
  464. TestFile config;
  465. config.schema_version = 0;
  466. config.schema = Schema{
  467. {"object", {
  468. {"value", PropertyType::Int}
  469. }},
  470. };
  471. struct Context : BindingContext {
  472. size_t* change_count;
  473. Context(size_t* out) : change_count(out) { }
  474. void did_change(std::vector<ObserverState> const&, std::vector<void*> const&, bool) override
  475. {
  476. ++*change_count;
  477. }
  478. };
  479. size_t change_count = 0;
  480. auto realm = Realm::get_shared_realm(config);
  481. realm->read_group();
  482. realm->m_binding_context.reset(new Context{&change_count});
  483. realm->m_binding_context->realm = realm;
  484. SECTION("local notifications are sent synchronously") {
  485. realm->begin_transaction();
  486. REQUIRE(change_count == 0);
  487. realm->commit_transaction();
  488. REQUIRE(change_count == 1);
  489. }
  490. SECTION("remote notifications are sent asynchronously") {
  491. auto r2 = Realm::get_shared_realm(config);
  492. r2->begin_transaction();
  493. r2->commit_transaction();
  494. REQUIRE(change_count == 0);
  495. util::EventLoop::main().run_until([&]{ return change_count > 0; });
  496. REQUIRE(change_count == 1);
  497. }
  498. SECTION("refresh() from within changes_available() refreshes") {
  499. struct Context : BindingContext {
  500. Realm& realm;
  501. Context(Realm& realm) : realm(realm) { }
  502. void changes_available() override
  503. {
  504. REQUIRE(realm.refresh());
  505. }
  506. };
  507. realm->m_binding_context.reset(new Context{*realm});
  508. realm->set_auto_refresh(false);
  509. auto r2 = Realm::get_shared_realm(config);
  510. r2->begin_transaction();
  511. r2->commit_transaction();
  512. realm->notify();
  513. // Should return false as the realm was already advanced
  514. REQUIRE_FALSE(realm->refresh());
  515. }
  516. SECTION("refresh() from within did_change() is a no-op") {
  517. struct Context : BindingContext {
  518. Realm& realm;
  519. Context(Realm& realm) : realm(realm) { }
  520. void did_change(std::vector<ObserverState> const&, std::vector<void*> const&, bool) override
  521. {
  522. // Create another version so that refresh() could do something
  523. auto r2 = Realm::get_shared_realm(realm.config());
  524. r2->begin_transaction();
  525. r2->commit_transaction();
  526. // Should be a no-op
  527. REQUIRE_FALSE(realm.refresh());
  528. }
  529. };
  530. realm->m_binding_context.reset(new Context{*realm});
  531. auto r2 = Realm::get_shared_realm(config);
  532. r2->begin_transaction();
  533. r2->commit_transaction();
  534. REQUIRE(realm->refresh());
  535. auto ver = realm->current_transaction_version();
  536. realm->m_binding_context.reset();
  537. // Should advance to the version created in the previous did_change()
  538. REQUIRE(realm->refresh());
  539. auto new_ver = realm->current_transaction_version();
  540. REQUIRE(*new_ver > *ver);
  541. // No more versions, so returns false
  542. REQUIRE_FALSE(realm->refresh());
  543. }
  544. SECTION("begin_write() from within did_change() produces recursive notifications") {
  545. struct Context : BindingContext {
  546. Realm& realm;
  547. size_t calls = 0;
  548. Context(Realm& realm) : realm(realm) { }
  549. void did_change(std::vector<ObserverState> const&, std::vector<void*> const&, bool) override
  550. {
  551. ++calls;
  552. if (realm.is_in_transaction())
  553. return;
  554. // Create another version so that begin_write() advances the version
  555. auto r2 = Realm::get_shared_realm(realm.config());
  556. r2->begin_transaction();
  557. r2->commit_transaction();
  558. realm.begin_transaction();
  559. realm.cancel_transaction();
  560. }
  561. };
  562. auto context = new Context{*realm};
  563. realm->m_binding_context.reset(context);
  564. auto r2 = Realm::get_shared_realm(config);
  565. r2->begin_transaction();
  566. r2->commit_transaction();
  567. REQUIRE(realm->refresh());
  568. REQUIRE(context->calls == 2);
  569. // Despite not sending a new notification we did advance the version, so
  570. // no more versions to refresh to
  571. REQUIRE_FALSE(realm->refresh());
  572. }
  573. }
  574. TEST_CASE("SharedRealm: schema updating from external changes") {
  575. TestFile config;
  576. config.schema_version = 0;
  577. config.schema_mode = SchemaMode::Additive;
  578. config.schema = Schema{
  579. {"object", {
  580. {"value", PropertyType::Int, Property::IsPrimary{true}},
  581. {"value 2", PropertyType::Int, Property::IsPrimary{false}, Property::IsIndexed{true}},
  582. }},
  583. };
  584. SECTION("newly added columns update table columns but are not added to properties") {
  585. // Does this test add any value when column keys are stable?
  586. auto r1 = Realm::get_shared_realm(config);
  587. auto r2 = Realm::get_shared_realm(config);
  588. auto test = [&] {
  589. r2->begin_transaction();
  590. r2->read_group().get_table("class_object")->add_column(type_String, "new col");
  591. r2->commit_transaction();
  592. auto& object_schema = *r1->schema().find("object");
  593. REQUIRE(object_schema.persisted_properties.size() == 2);
  594. ColKey col = object_schema.persisted_properties[0].column_key;
  595. r1->refresh();
  596. REQUIRE(object_schema.persisted_properties[0].column_key == col);
  597. };
  598. SECTION("with an active read transaction") {
  599. r1->read_group();
  600. test();
  601. }
  602. SECTION("without an active read transaction") {
  603. r1->invalidate();
  604. test();
  605. }
  606. }
  607. SECTION("beginning a read transaction checks for incompatible changes") {
  608. auto r = Realm::get_shared_realm(config);
  609. r->invalidate();
  610. auto& db = TestHelper::get_db(r);
  611. WriteTransaction wt(db);
  612. auto& table = *wt.get_table("class_object");
  613. SECTION("removing a property") {
  614. table.remove_column(table.get_column_key("value"));
  615. wt.commit();
  616. REQUIRE_THROWS_WITH(r->refresh(),
  617. Catch::Matchers::Contains("Property 'object.value' has been removed."));
  618. }
  619. SECTION("change property type") {
  620. table.remove_column(table.get_column_key("value 2"));
  621. table.add_column(type_Float, "value 2");
  622. wt.commit();
  623. REQUIRE_THROWS_WITH(r->refresh(),
  624. Catch::Matchers::Contains("Property 'object.value 2' has been changed from 'int' to 'float'"));
  625. }
  626. SECTION("make property optional") {
  627. table.remove_column(table.get_column_key("value 2"));
  628. table.add_column(type_Int, "value 2", true);
  629. wt.commit();
  630. REQUIRE_THROWS_WITH(r->refresh(),
  631. Catch::Matchers::Contains("Property 'object.value 2' has been made optional"));
  632. }
  633. SECTION("recreate column with no changes") {
  634. table.remove_column(table.get_column_key("value 2"));
  635. table.add_column(type_Int, "value 2");
  636. wt.commit();
  637. REQUIRE_NOTHROW(r->refresh());
  638. }
  639. SECTION("remove index from non-PK") {
  640. table.remove_search_index(table.get_column_key("value 2"));
  641. wt.commit();
  642. REQUIRE_NOTHROW(r->refresh());
  643. }
  644. }
  645. }
  646. TEST_CASE("SharedRealm: close()") {
  647. TestFile config;
  648. config.schema_version = 1;
  649. config.schema = Schema{
  650. {"object", {
  651. {"value", PropertyType::Int}
  652. }},
  653. {"list", {
  654. {"list", PropertyType::Object|PropertyType::Array, "object"}
  655. }},
  656. };
  657. auto realm = Realm::get_shared_realm(config);
  658. SECTION("all functions throw ClosedRealmException after close") {
  659. realm->close();
  660. REQUIRE(realm->is_closed());
  661. REQUIRE_THROWS_AS(realm->read_group(), ClosedRealmException);
  662. REQUIRE_THROWS_AS(realm->begin_transaction(), ClosedRealmException);
  663. REQUIRE(!realm->is_in_transaction());
  664. REQUIRE_THROWS_AS(realm->commit_transaction(), InvalidTransactionException);
  665. REQUIRE_THROWS_AS(realm->cancel_transaction(), InvalidTransactionException);
  666. REQUIRE_THROWS_AS(realm->refresh(), ClosedRealmException);
  667. REQUIRE_THROWS_AS(realm->invalidate(), ClosedRealmException);
  668. REQUIRE_THROWS_AS(realm->compact(), ClosedRealmException);
  669. }
  670. SECTION("fully closes database file even with live notifiers") {
  671. auto& group = realm->read_group();
  672. realm->begin_transaction();
  673. auto obj = ObjectStore::table_for_object_type(group, "list")->create_object();
  674. realm->commit_transaction();
  675. Results results(realm, ObjectStore::table_for_object_type(group, "object"));
  676. List list(realm, obj.get_linklist("list"));
  677. Object object(realm, obj);
  678. auto obj_token = object.add_notification_callback([](CollectionChangeSet, std::exception_ptr) {});
  679. auto list_token = list.add_notification_callback([](CollectionChangeSet, std::exception_ptr) {});
  680. auto results_token = results.add_notification_callback([](CollectionChangeSet, std::exception_ptr) {});
  681. // Perform a dummy transaction to ensure the notifiers actually acquire
  682. // resources that need to be closed
  683. realm->begin_transaction();
  684. realm->commit_transaction();
  685. realm->close();
  686. // Verify that we're able to acquire an exclusive lock
  687. REQUIRE(DB::call_with_lock(config.path, [](auto) {}));
  688. }
  689. }
  690. TEST_CASE("ShareRealm: in-memory mode from buffer") {
  691. TestFile config;
  692. config.schema_version = 1;
  693. config.schema = Schema{
  694. {"object", {
  695. {"value", PropertyType::Int}
  696. }},
  697. };
  698. SECTION("Save and open Realm from in-memory buffer") {
  699. // Write in-memory copy of Realm to a buffer
  700. auto realm = Realm::get_shared_realm(config);
  701. OwnedBinaryData realm_buffer = realm->write_copy();
  702. // Open the buffer as a new (immutable in-memory) Realm
  703. realm::Realm::Config config2;
  704. config2.in_memory = true;
  705. config2.schema_mode = SchemaMode::Immutable;
  706. config2.realm_data = realm_buffer.get();
  707. auto realm2 = Realm::get_shared_realm(config2);
  708. // Verify that it can read the schema and that it is the same
  709. REQUIRE(realm->schema().size() == 1);
  710. auto it = realm->schema().find("object");
  711. auto table = realm->read_group().get_table("class_object");
  712. REQUIRE(it != realm->schema().end());
  713. REQUIRE(it->table_key == table->get_key());
  714. REQUIRE(it->persisted_properties.size() == 1);
  715. REQUIRE(it->persisted_properties[0].name == "value");
  716. REQUIRE(it->persisted_properties[0].column_key == table->get_column_key("value"));
  717. // Test invalid configs
  718. realm::Realm::Config config3;
  719. config3.realm_data = realm_buffer.get();
  720. REQUIRE_THROWS(Realm::get_shared_realm(config3)); // missing in_memory and immutable
  721. config3.in_memory = true;
  722. config3.schema_mode = SchemaMode::Immutable;
  723. config3.path = "path";
  724. REQUIRE_THROWS(Realm::get_shared_realm(config3)); // both buffer and path
  725. config3.path = "";
  726. config3.encryption_key = {'a'};
  727. REQUIRE_THROWS(Realm::get_shared_realm(config3)); // both buffer and encryption
  728. }
  729. }
  730. TEST_CASE("ShareRealm: realm closed in did_change callback") {
  731. TestFile config;
  732. config.schema_version = 1;
  733. config.schema = Schema{
  734. {"object", {
  735. {"value", PropertyType::Int}
  736. }},
  737. };
  738. config.automatic_change_notifications = false;
  739. auto r1 = Realm::get_shared_realm(config);
  740. r1->begin_transaction();
  741. auto table = r1->read_group().get_table("class_object");
  742. table->create_object();
  743. r1->commit_transaction();
  744. // Cannot be a member var of Context since Realm.close will free the context.
  745. static SharedRealm* shared_realm;
  746. shared_realm = &r1;
  747. struct Context : public BindingContext {
  748. void did_change(std::vector<ObserverState> const&, std::vector<void*> const&, bool) override
  749. {
  750. (*shared_realm)->close();
  751. (*shared_realm).reset();
  752. }
  753. };
  754. SECTION("did_change") {
  755. r1->m_binding_context.reset(new Context());
  756. r1->invalidate();
  757. auto r2 = Realm::get_shared_realm(config);
  758. r2->begin_transaction();
  759. r2->read_group().get_table("class_object")->create_object();
  760. r2->commit_transaction();
  761. r2.reset();
  762. r1->notify();
  763. }
  764. SECTION("did_change with async results") {
  765. r1->m_binding_context.reset(new Context());
  766. Results results(r1, table->where());
  767. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  768. // Should not be called.
  769. REQUIRE(false);
  770. });
  771. auto r2 = Realm::get_shared_realm(config);
  772. r2->begin_transaction();
  773. r2->read_group().get_table("class_object")->create_object();
  774. r2->commit_transaction();
  775. r2.reset();
  776. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  777. coordinator->on_change();
  778. r1->notify();
  779. }
  780. SECTION("refresh") {
  781. r1->m_binding_context.reset(new Context());
  782. auto r2 = Realm::get_shared_realm(config);
  783. r2->begin_transaction();
  784. r2->read_group().get_table("class_object")->create_object();
  785. r2->commit_transaction();
  786. r2.reset();
  787. REQUIRE_FALSE(r1->refresh());
  788. }
  789. shared_realm = nullptr;
  790. }
  791. TEST_CASE("RealmCoordinator: schema cache") {
  792. TestFile config;
  793. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  794. Schema cache_schema;
  795. uint64_t cache_sv = -1, cache_tv = -1;
  796. Schema schema{
  797. {"object", {
  798. {"value", PropertyType::Int}
  799. }},
  800. };
  801. Schema schema2{
  802. {"object", {
  803. {"value", PropertyType::Int},
  804. }},
  805. {"object 2", {
  806. {"value", PropertyType::Int},
  807. }},
  808. };
  809. SECTION("valid initial schema sets cache") {
  810. coordinator->cache_schema(schema, 5, 10);
  811. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  812. REQUIRE(cache_schema == schema);
  813. REQUIRE(cache_sv == 5);
  814. REQUIRE(cache_tv == 10);
  815. }
  816. SECTION("cache can be updated with newer schema") {
  817. coordinator->cache_schema(schema, 5, 10);
  818. coordinator->cache_schema(schema2, 6, 11);
  819. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  820. REQUIRE(cache_schema == schema2);
  821. REQUIRE(cache_sv == 6);
  822. REQUIRE(cache_tv == 11);
  823. }
  824. SECTION("empty schema is ignored") {
  825. coordinator->cache_schema(Schema{}, 5, 10);
  826. REQUIRE_FALSE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  827. coordinator->cache_schema(schema, 5, 10);
  828. coordinator->cache_schema(Schema{}, 5, 10);
  829. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  830. REQUIRE(cache_schema == schema);
  831. REQUIRE(cache_sv == 5);
  832. REQUIRE(cache_tv == 10);
  833. }
  834. SECTION("schema for older transaction is ignored") {
  835. coordinator->cache_schema(schema, 5, 10);
  836. coordinator->cache_schema(schema2, 4, 8);
  837. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  838. REQUIRE(cache_schema == schema);
  839. REQUIRE(cache_sv == 5);
  840. REQUIRE(cache_tv == 10);
  841. coordinator->advance_schema_cache(10, 20);
  842. coordinator->cache_schema(schema, 6, 15);
  843. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  844. REQUIRE(cache_tv == 20); // should not have dropped to 15
  845. }
  846. SECTION("advance_schema() from transaction version bumps transaction version") {
  847. coordinator->cache_schema(schema, 5, 10);
  848. coordinator->advance_schema_cache(10, 12);
  849. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  850. REQUIRE(cache_schema == schema);
  851. REQUIRE(cache_sv == 5);
  852. REQUIRE(cache_tv == 12);
  853. }
  854. SECTION("advance_schema() ending before transaction version does nothing") {
  855. coordinator->cache_schema(schema, 5, 10);
  856. coordinator->advance_schema_cache(8, 9);
  857. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  858. REQUIRE(cache_schema == schema);
  859. REQUIRE(cache_sv == 5);
  860. REQUIRE(cache_tv == 10);
  861. }
  862. SECTION("advance_schema() extending over transaction version bumps version") {
  863. coordinator->cache_schema(schema, 5, 10);
  864. coordinator->advance_schema_cache(3, 15);
  865. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  866. REQUIRE(cache_schema == schema);
  867. REQUIRE(cache_sv == 5);
  868. REQUIRE(cache_tv == 15);
  869. }
  870. SECTION("advance_schema() with no cahced schema does nothing") {
  871. coordinator->advance_schema_cache(3, 15);
  872. REQUIRE_FALSE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  873. }
  874. }
  875. TEST_CASE("SharedRealm: coordinator schema cache") {
  876. TestFile config;
  877. auto r = Realm::get_shared_realm(config);
  878. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  879. Schema cache_schema;
  880. uint64_t cache_sv = -1, cache_tv = -1;
  881. Schema schema{
  882. {"object", {
  883. {"value", PropertyType::Int}
  884. }},
  885. };
  886. Schema schema2{
  887. {"object", {
  888. {"value", PropertyType::Int},
  889. }},
  890. {"object 2", {
  891. {"value", PropertyType::Int},
  892. }},
  893. };
  894. class ExternalWriter {
  895. private:
  896. std::shared_ptr<Realm> m_realm;
  897. public:
  898. WriteTransaction wt;
  899. ExternalWriter(Realm::Config const& config)
  900. : m_realm([&] {
  901. auto c = config;
  902. c.scheduler = util::Scheduler::get_frozen();
  903. return _impl::RealmCoordinator::get_coordinator(c.path)->get_realm(c, util::none);
  904. }())
  905. , wt(TestHelper::get_db(m_realm))
  906. {
  907. }
  908. };
  909. auto external_write = [&](Realm::Config const& config, auto&& fn) {
  910. ExternalWriter wt(config);
  911. fn(wt.wt);
  912. wt.wt.commit();
  913. };
  914. SECTION("is initially empty for uninitialized file") {
  915. REQUIRE_FALSE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  916. }
  917. r->update_schema(schema);
  918. SECTION("is populated after calling update_schema()") {
  919. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  920. REQUIRE(cache_sv == 0);
  921. REQUIRE(cache_schema == schema);
  922. REQUIRE(cache_schema.begin()->persisted_properties[0].column_key != ColKey{});
  923. }
  924. coordinator = nullptr;
  925. r = nullptr;
  926. r = Realm::get_shared_realm(config);
  927. coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  928. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  929. SECTION("is populated after opening an initialized file") {
  930. REQUIRE(cache_sv == 0);
  931. REQUIRE(cache_tv == 2); // with in-realm history the version doesn't reset
  932. REQUIRE(cache_schema == schema);
  933. REQUIRE(cache_schema.begin()->persisted_properties[0].column_key != ColKey{});
  934. }
  935. SECTION("transaction version is bumped after a local write") {
  936. auto tv = cache_tv;
  937. r->begin_transaction();
  938. r->commit_transaction();
  939. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  940. REQUIRE(cache_tv == tv + 1);
  941. }
  942. SECTION("notify() without a read transaction does not bump transaction version") {
  943. auto tv = cache_tv;
  944. SECTION("non-schema change") {
  945. external_write(config, [](auto& wt) {
  946. wt.get_table("class_object")->create_object();
  947. });
  948. }
  949. SECTION("schema change") {
  950. external_write(config, [](auto& wt) {
  951. wt.add_table("class_object 2");
  952. });
  953. }
  954. r->notify();
  955. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  956. REQUIRE(cache_tv == tv);
  957. REQUIRE(cache_schema == schema);
  958. }
  959. SECTION("notify() with a read transaction bumps transaction version") {
  960. r->read_group();
  961. external_write(config, [](auto& wt) {
  962. wt.get_table("class_object")->create_object();
  963. });
  964. r->notify();
  965. auto tv = cache_tv;
  966. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  967. REQUIRE(cache_tv == tv + 1);
  968. }
  969. SECTION("notify() with a read transaction updates schema folloing external schema change") {
  970. r->read_group();
  971. external_write(config, [](auto& wt) {
  972. wt.add_table("class_object 2");
  973. });
  974. r->notify();
  975. auto tv = cache_tv;
  976. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  977. REQUIRE(cache_tv == tv + 1);
  978. REQUIRE(cache_schema.size() == 2);
  979. REQUIRE(cache_schema.find("object 2") != cache_schema.end());
  980. }
  981. SECTION("transaction version is bumped after refresh() following external non-schema write") {
  982. external_write(config, [](auto& wt) {
  983. wt.get_table("class_object")->create_object();
  984. });
  985. r->refresh();
  986. auto tv = cache_tv;
  987. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  988. REQUIRE(cache_tv == tv + 1);
  989. }
  990. SECTION("schema is reread following refresh() over external schema change") {
  991. external_write(config, [](auto& wt) {
  992. wt.add_table("class_object 2");
  993. });
  994. r->refresh();
  995. auto tv = cache_tv;
  996. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  997. REQUIRE(cache_tv == tv + 1);
  998. REQUIRE(cache_schema.size() == 2);
  999. REQUIRE(cache_schema.find("object 2") != cache_schema.end());
  1000. }
  1001. SECTION("update_schema() to version already on disk updates cache") {
  1002. r->read_group();
  1003. external_write(config, [](auto& wt) {
  1004. auto table = wt.add_table("class_object 2");
  1005. table->add_column(type_Int, "value");
  1006. });
  1007. auto tv = cache_tv;
  1008. r->update_schema(schema2);
  1009. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  1010. REQUIRE(cache_tv == tv + 1); // only +1 because update_schema() did not perform a write
  1011. REQUIRE(cache_schema.size() == 2);
  1012. REQUIRE(cache_schema.find("object 2") != cache_schema.end());
  1013. }
  1014. SECTION("update_schema() to version already on disk updates cache") {
  1015. r->read_group();
  1016. external_write(config, [](auto& wt) {
  1017. auto table = wt.add_table("class_object 2");
  1018. table->add_column(type_Int, "value");
  1019. });
  1020. auto tv = cache_tv;
  1021. r->update_schema(schema2);
  1022. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  1023. REQUIRE(cache_tv == tv + 1); // only +1 because update_schema() did not perform a write
  1024. REQUIRE(cache_schema.size() == 2);
  1025. REQUIRE(cache_schema.find("object 2") != cache_schema.end());
  1026. }
  1027. SECTION("update_schema() to version populated on disk while waiting for the write lock updates cache") {
  1028. r->read_group();
  1029. // We want to commit the write while we're waiting on the write lock on
  1030. // this thread, which can't really be done in a properly synchronized manner
  1031. std::chrono::microseconds wait_time{5000};
  1032. #if REALM_ANDROID
  1033. // When running on device or in an emulator we need to wait longer due
  1034. // to them being slow
  1035. wait_time *= 10;
  1036. #endif
  1037. bool did_run = false;
  1038. JoiningThread thread([&] {
  1039. ExternalWriter writer(config);
  1040. if (writer.wt.get_table("class_object 2"))
  1041. return;
  1042. did_run = true;
  1043. auto table = writer.wt.add_table("class_object 2");
  1044. table->add_column(type_Int, "value");
  1045. std::this_thread::sleep_for(wait_time * 2);
  1046. writer.wt.commit();
  1047. });
  1048. std::this_thread::sleep_for(wait_time);
  1049. auto tv = cache_tv;
  1050. r->update_schema(Schema{
  1051. {"object", {{"value", PropertyType::Int}}},
  1052. {"object 2", {{"value", PropertyType::Int}}},
  1053. });
  1054. // just skip the test if the timing was wrong to avoid spurious failures
  1055. if (!did_run)
  1056. return;
  1057. REQUIRE(coordinator->get_cached_schema(cache_schema, cache_sv, cache_tv));
  1058. REQUIRE(cache_tv == tv + 1); // only +1 because update_schema()'s write was rolled back
  1059. REQUIRE(cache_schema.size() == 2);
  1060. REQUIRE(cache_schema.find("object 2") != cache_schema.end());
  1061. }
  1062. }
  1063. TEST_CASE("SharedRealm: dynamic schema mode doesn't invalidate object schema pointers when schema hasn't changed") {
  1064. TestFile config;
  1065. // Prepopulate the Realm with the schema.
  1066. Realm::Config config_with_schema = config;
  1067. config_with_schema.schema_version = 1;
  1068. config_with_schema.schema_mode = SchemaMode::Automatic;
  1069. config_with_schema.schema = Schema{
  1070. {"object", {
  1071. {"value", PropertyType::Int, Property::IsPrimary{true}},
  1072. {"value 2", PropertyType::Int, Property::IsPrimary{false}, Property::IsIndexed{true}},
  1073. }}
  1074. };
  1075. auto r1 = Realm::get_shared_realm(config_with_schema);
  1076. // Retrieve the object schema in dynamic mode.
  1077. auto r2 = Realm::get_shared_realm(config);
  1078. auto* object_schema = &*r2->schema().find("object");
  1079. // Perform an empty write to create a new version, resulting in the other Realm needing to re-read the schema.
  1080. r1->begin_transaction();
  1081. r1->commit_transaction();
  1082. // Advance to the latest version, and verify the object schema is at the same location in memory.
  1083. r2->read_group();
  1084. REQUIRE(object_schema == &*r2->schema().find("object"));
  1085. }
  1086. TEST_CASE("SharedRealm: SchemaChangedFunction") {
  1087. struct Context : BindingContext {
  1088. size_t* change_count;
  1089. Schema* schema;
  1090. Context(size_t* count_out, Schema* schema_out) : change_count(count_out), schema(schema_out) { }
  1091. void schema_did_change(Schema const& changed_schema) override
  1092. {
  1093. ++*change_count;
  1094. *schema = changed_schema;
  1095. }
  1096. };
  1097. size_t schema_changed_called = 0;
  1098. Schema changed_fixed_schema;
  1099. TestFile config;
  1100. auto dynamic_config = config;
  1101. config.schema = Schema{
  1102. {"object1", {
  1103. {"value", PropertyType::Int},
  1104. }},
  1105. {"object2", {
  1106. {"value", PropertyType::Int},
  1107. }}
  1108. };
  1109. config.schema_version = 1;
  1110. auto r1 = Realm::get_shared_realm(config);
  1111. r1->read_group();
  1112. r1->m_binding_context.reset(new Context(&schema_changed_called, &changed_fixed_schema));
  1113. SECTION("Fixed schema") {
  1114. SECTION("update_schema") {
  1115. auto new_schema = Schema{
  1116. {"object3", {
  1117. {"value", PropertyType::Int},
  1118. }}
  1119. };
  1120. r1->update_schema(new_schema, 2);
  1121. REQUIRE(schema_changed_called == 1);
  1122. REQUIRE(changed_fixed_schema.find("object3")->property_for_name("value")->column_key != ColKey{});
  1123. }
  1124. SECTION("Open a new Realm instance with same config won't trigger") {
  1125. auto r2 = Realm::get_shared_realm(config);
  1126. REQUIRE(schema_changed_called == 0);
  1127. }
  1128. SECTION("Non schema related transaction doesn't trigger") {
  1129. auto r2 = Realm::get_shared_realm(config);
  1130. r2->begin_transaction();
  1131. r2->commit_transaction();
  1132. r1->refresh();
  1133. REQUIRE(schema_changed_called == 0);
  1134. }
  1135. SECTION("Schema is changed by another Realm") {
  1136. auto r2 = Realm::get_shared_realm(config);
  1137. r2->begin_transaction();
  1138. r2->read_group().get_table("class_object1")->add_column(type_String, "new col");
  1139. r2->commit_transaction();
  1140. r1->refresh();
  1141. REQUIRE(schema_changed_called == 1);
  1142. REQUIRE(changed_fixed_schema.find("object1")->property_for_name("value")->column_key != ColKey{});
  1143. }
  1144. // This is not a valid use case. m_schema won't be refreshed.
  1145. SECTION("Schema is changed by this Realm won't trigger") {
  1146. r1->begin_transaction();
  1147. r1->read_group().get_table("class_object1")->add_column(type_String, "new col");
  1148. r1->commit_transaction();
  1149. REQUIRE(schema_changed_called == 0);
  1150. }
  1151. }
  1152. SECTION("Dynamic schema") {
  1153. size_t dynamic_schema_changed_called = 0;
  1154. Schema changed_dynamic_schema;
  1155. auto r2 = Realm::get_shared_realm(dynamic_config);
  1156. r2->m_binding_context.reset(new Context(&dynamic_schema_changed_called, &changed_dynamic_schema));
  1157. SECTION("set_schema_subset") {
  1158. auto new_schema = Schema{
  1159. {"object1", {
  1160. {"value", PropertyType::Int},
  1161. }}
  1162. };
  1163. r2->set_schema_subset(new_schema);
  1164. REQUIRE(schema_changed_called == 0);
  1165. REQUIRE(dynamic_schema_changed_called == 1);
  1166. REQUIRE(changed_dynamic_schema.find("object1")->property_for_name("value")->column_key != ColKey{});
  1167. }
  1168. SECTION("Non schema related transaction will always trigger in dynamic mode") {
  1169. auto r1 = Realm::get_shared_realm(config);
  1170. // An empty transaction will trigger the schema changes always in dynamic mode.
  1171. r1->begin_transaction();
  1172. r1->commit_transaction();
  1173. r2->refresh();
  1174. REQUIRE(dynamic_schema_changed_called == 1);
  1175. REQUIRE(changed_dynamic_schema.find("object1")->property_for_name("value")->column_key != ColKey{});
  1176. }
  1177. SECTION("Schema is changed by another Realm") {
  1178. r1->begin_transaction();
  1179. r1->read_group().get_table("class_object1")->add_column(type_String, "new col");
  1180. r1->commit_transaction();
  1181. r2->refresh();
  1182. REQUIRE(dynamic_schema_changed_called == 1);
  1183. REQUIRE(changed_dynamic_schema.find("object1")->property_for_name("value")->column_key != ColKey{});
  1184. }
  1185. }
  1186. }
  1187. #ifndef _WIN32
  1188. TEST_CASE("SharedRealm: compact on launch") {
  1189. // Make compactable Realm
  1190. TestFile config;
  1191. config.automatic_change_notifications = false;
  1192. int num_opens = 0;
  1193. config.should_compact_on_launch_function = [&](size_t total_bytes, size_t used_bytes) {
  1194. REQUIRE(total_bytes > used_bytes);
  1195. num_opens++;
  1196. return num_opens != 2;
  1197. };
  1198. config.schema = Schema{
  1199. {"object", {
  1200. {"value", PropertyType::String}
  1201. }},
  1202. };
  1203. REQUIRE(num_opens == 0);
  1204. auto r = Realm::get_shared_realm(config);
  1205. REQUIRE(num_opens == 1);
  1206. r->begin_transaction();
  1207. auto table = r->read_group().get_table("class_object");
  1208. size_t count = 1000;
  1209. for (size_t i = 0; i < count; ++i)
  1210. table->create_object().set_all(util::format("Foo_%1", i % 10).c_str());
  1211. r->commit_transaction();
  1212. REQUIRE(table->size() == count);
  1213. r->close();
  1214. SECTION("compact reduces the file size") {
  1215. // Confirm expected sizes before and after opening the Realm
  1216. size_t size_before = size_t(File(config.path).get_size());
  1217. r = Realm::get_shared_realm(config);
  1218. REQUIRE(num_opens == 2);
  1219. r->close();
  1220. REQUIRE(size_t(File(config.path).get_size()) == size_before); // File size after returning false
  1221. r = Realm::get_shared_realm(config);
  1222. REQUIRE(num_opens == 3);
  1223. REQUIRE(size_t(File(config.path).get_size()) < size_before); // File size after returning true
  1224. // Validate that the file still contains what it should
  1225. REQUIRE(r->read_group().get_table("class_object")->size() == count);
  1226. // Registering for a collection notification shouldn't crash when compact on launch is used.
  1227. Results results(r, r->read_group().get_table("class_object"));
  1228. results.add_notification_callback([](CollectionChangeSet const&, std::exception_ptr) { });
  1229. r->close();
  1230. }
  1231. SECTION("compact function does not get invoked if realm is open on another thread") {
  1232. config.scheduler = util::Scheduler::get_frozen();
  1233. r = Realm::get_shared_realm(config);
  1234. REQUIRE(num_opens == 2);
  1235. std::thread([&]{
  1236. auto r2 = Realm::get_shared_realm(config);
  1237. REQUIRE(num_opens == 2);
  1238. }).join();
  1239. r->close();
  1240. std::thread([&]{
  1241. auto r3 = Realm::get_shared_realm(config);
  1242. REQUIRE(num_opens == 3);
  1243. }).join();
  1244. }
  1245. }
  1246. #endif
  1247. struct ModeAutomatic {
  1248. static SchemaMode mode() { return SchemaMode::Automatic; }
  1249. static bool should_call_init_on_version_bump() { return false; }
  1250. };
  1251. struct ModeAdditive {
  1252. static SchemaMode mode() { return SchemaMode::Additive; }
  1253. static bool should_call_init_on_version_bump() { return false; }
  1254. };
  1255. struct ModeManual {
  1256. static SchemaMode mode() { return SchemaMode::Manual; }
  1257. static bool should_call_init_on_version_bump() { return false; }
  1258. };
  1259. struct ModeResetFile {
  1260. static SchemaMode mode() { return SchemaMode::ResetFile; }
  1261. static bool should_call_init_on_version_bump() { return true; }
  1262. };
  1263. TEMPLATE_TEST_CASE("SharedRealm: update_schema with initialization_function", "[init][update_schema]",
  1264. ModeAutomatic, ModeAdditive, ModeManual, ModeResetFile) {
  1265. TestFile config;
  1266. config.schema_mode = TestType::mode();
  1267. bool initialization_function_called = false;
  1268. uint64_t schema_version_in_callback = -1;
  1269. Schema schema_in_callback;
  1270. auto initialization_function = [&initialization_function_called, &schema_version_in_callback,
  1271. &schema_in_callback](auto shared_realm) {
  1272. REQUIRE(shared_realm->is_in_transaction());
  1273. initialization_function_called = true;
  1274. schema_version_in_callback = shared_realm->schema_version();
  1275. schema_in_callback = shared_realm->schema();
  1276. };
  1277. Schema schema{
  1278. {"object", {
  1279. {"value", PropertyType::String}
  1280. }},
  1281. };
  1282. SECTION("call initialization function directly by update_schema") {
  1283. // Open in dynamic mode with no schema specified
  1284. auto realm = Realm::get_shared_realm(config);
  1285. REQUIRE_FALSE(initialization_function_called);
  1286. realm->update_schema(schema, 0, nullptr, initialization_function);
  1287. REQUIRE(initialization_function_called);
  1288. REQUIRE(schema_version_in_callback == 0);
  1289. REQUIRE(schema_in_callback.compare(schema).size() == 0);
  1290. }
  1291. config.schema_version = 0;
  1292. config.schema = schema;
  1293. SECTION("initialization function should be called for unversioned realm") {
  1294. config.initialization_function = initialization_function;
  1295. Realm::get_shared_realm(config);
  1296. REQUIRE(initialization_function_called);
  1297. REQUIRE(schema_version_in_callback == 0);
  1298. REQUIRE(schema_in_callback.compare(schema).size() == 0);
  1299. }
  1300. SECTION("initialization function for versioned realm") {
  1301. // Initialize v0
  1302. Realm::get_shared_realm(config);
  1303. config.schema_version = 1;
  1304. config.initialization_function = initialization_function;
  1305. Realm::get_shared_realm(config);
  1306. REQUIRE(initialization_function_called == TestType::should_call_init_on_version_bump());
  1307. if (TestType::should_call_init_on_version_bump()) {
  1308. REQUIRE(schema_version_in_callback == 1);
  1309. REQUIRE(schema_in_callback.compare(schema).size() == 0);
  1310. }
  1311. }
  1312. }
  1313. TEST_CASE("BindingContext is notified about delivery of change notifications") {
  1314. _impl::RealmCoordinator::assert_no_open_realms();
  1315. InMemoryTestFile config;
  1316. config.automatic_change_notifications = false;
  1317. auto r = Realm::get_shared_realm(config);
  1318. r->update_schema({
  1319. {"object", {
  1320. {"value", PropertyType::Int}
  1321. }},
  1322. });
  1323. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  1324. auto table = r->read_group().get_table("class_object");
  1325. SECTION("BindingContext notified even if no callbacks are registered") {
  1326. static int binding_context_start_notify_calls = 0;
  1327. static int binding_context_end_notify_calls = 0;
  1328. struct Context : BindingContext {
  1329. void will_send_notifications() override
  1330. {
  1331. ++binding_context_start_notify_calls;
  1332. }
  1333. void did_send_notifications() override
  1334. {
  1335. ++binding_context_end_notify_calls;
  1336. }
  1337. };
  1338. r->m_binding_context.reset(new Context());
  1339. SECTION("local commit") {
  1340. binding_context_start_notify_calls = 0;
  1341. binding_context_end_notify_calls = 0;
  1342. coordinator->on_change();
  1343. r->begin_transaction();
  1344. REQUIRE(binding_context_start_notify_calls == 1);
  1345. REQUIRE(binding_context_end_notify_calls == 1);
  1346. r->cancel_transaction();
  1347. }
  1348. SECTION("remote commit") {
  1349. binding_context_start_notify_calls = 0;
  1350. binding_context_end_notify_calls = 0;
  1351. JoiningThread([&] {
  1352. auto r2 = coordinator->get_realm(util::Scheduler::get_frozen());
  1353. r2->begin_transaction();
  1354. auto table2 = r2->read_group().get_table("class_object");
  1355. table2->create_object();
  1356. r2->commit_transaction();
  1357. });
  1358. advance_and_notify(*r);
  1359. REQUIRE(binding_context_start_notify_calls == 1);
  1360. REQUIRE(binding_context_end_notify_calls == 1);
  1361. }
  1362. }
  1363. SECTION("notify BindingContext before and after sending notifications") {
  1364. static int binding_context_start_notify_calls = 0;
  1365. static int binding_context_end_notify_calls = 0;
  1366. static int notification_calls = 0;
  1367. auto col = table->get_column_key("value");
  1368. Results results1(r, table->where().greater_equal(col, 0));
  1369. Results results2(r, table->where().less(col, 10));
  1370. auto token1 = results1.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  1371. REQUIRE_FALSE(err);
  1372. ++notification_calls;
  1373. });
  1374. auto token2 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  1375. REQUIRE_FALSE(err);
  1376. ++notification_calls;
  1377. });
  1378. struct Context : BindingContext {
  1379. void will_send_notifications() override
  1380. {
  1381. REQUIRE(notification_calls == 0);
  1382. REQUIRE(binding_context_end_notify_calls == 0);
  1383. ++binding_context_start_notify_calls;
  1384. }
  1385. void did_send_notifications() override
  1386. {
  1387. REQUIRE(notification_calls == 2);
  1388. REQUIRE(binding_context_start_notify_calls == 1);
  1389. ++binding_context_end_notify_calls;
  1390. }
  1391. };
  1392. r->m_binding_context.reset(new Context());
  1393. SECTION("local commit") {
  1394. binding_context_start_notify_calls = 0;
  1395. binding_context_end_notify_calls = 0;
  1396. notification_calls = 0;
  1397. coordinator->on_change();
  1398. r->begin_transaction();
  1399. table->create_object();
  1400. r->commit_transaction();
  1401. REQUIRE(binding_context_start_notify_calls == 1);
  1402. REQUIRE(binding_context_end_notify_calls == 1);
  1403. }
  1404. SECTION("remote commit") {
  1405. binding_context_start_notify_calls = 0;
  1406. binding_context_end_notify_calls = 0;
  1407. notification_calls = 0;
  1408. JoiningThread([&] {
  1409. auto r2 = coordinator->get_realm(util::Scheduler::get_frozen());
  1410. r2->begin_transaction();
  1411. auto table2 = r2->read_group().get_table("class_object");
  1412. table2->create_object();
  1413. r2->commit_transaction();
  1414. });
  1415. advance_and_notify(*r);
  1416. REQUIRE(binding_context_start_notify_calls == 1);
  1417. REQUIRE(binding_context_end_notify_calls == 1);
  1418. }
  1419. }
  1420. SECTION("did_send() is skipped if the Realm is closed first") {
  1421. Results results(r, table->where());
  1422. bool do_close = true;
  1423. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  1424. if (do_close)
  1425. r->close();
  1426. });
  1427. struct FailOnDidSend : BindingContext {
  1428. void did_send_notifications() override
  1429. {
  1430. FAIL("did_send_notifications() should not have been called");
  1431. }
  1432. };
  1433. struct CloseOnWillChange : FailOnDidSend {
  1434. Realm& realm;
  1435. CloseOnWillChange(Realm& realm) : realm(realm) {}
  1436. void will_send_notifications() override
  1437. {
  1438. realm.close();
  1439. }
  1440. };
  1441. SECTION("closed in notification callback for notify()") {
  1442. r->m_binding_context.reset(new FailOnDidSend);
  1443. coordinator->on_change();
  1444. r->notify();
  1445. }
  1446. SECTION("closed in notification callback for refresh()") {
  1447. do_close = false;
  1448. coordinator->on_change();
  1449. r->notify();
  1450. do_close = true;
  1451. JoiningThread([&] {
  1452. auto r = coordinator->get_realm(util::Scheduler::get_frozen());
  1453. r->begin_transaction();
  1454. r->read_group().get_table("class_object")->create_object();
  1455. r->commit_transaction();
  1456. });
  1457. r->m_binding_context.reset(new FailOnDidSend);
  1458. coordinator->on_change();
  1459. r->refresh();
  1460. }
  1461. SECTION("closed in will_send() for notify()") {
  1462. r->m_binding_context.reset(new CloseOnWillChange(*r));
  1463. coordinator->on_change();
  1464. r->notify();
  1465. }
  1466. SECTION("closed in will_send() for refresh()") {
  1467. do_close = false;
  1468. coordinator->on_change();
  1469. r->notify();
  1470. do_close = true;
  1471. JoiningThread([&] {
  1472. auto r = coordinator->get_realm(util::Scheduler::get_frozen());
  1473. r->begin_transaction();
  1474. r->read_group().get_table("class_object")->create_object();
  1475. r->commit_transaction();
  1476. });
  1477. r->m_binding_context.reset(new CloseOnWillChange(*r));
  1478. coordinator->on_change();
  1479. r->refresh();
  1480. }
  1481. }
  1482. }
  1483. TEST_CASE("Statistics on Realms") {
  1484. _impl::RealmCoordinator::assert_no_open_realms();
  1485. InMemoryTestFile config;
  1486. // config.cache = false;
  1487. config.automatic_change_notifications = false;
  1488. auto r = Realm::get_shared_realm(config);
  1489. r->update_schema({
  1490. {"object", {
  1491. {"value", PropertyType::Int}
  1492. }},
  1493. });
  1494. SECTION("compute_size") {
  1495. auto s = r->read_group().compute_aggregated_byte_size();
  1496. REQUIRE(s > 0);
  1497. }
  1498. }
  1499. #if REALM_PLATFORM_APPLE && NOTIFIER_BACKGROUND_ERRORS
  1500. TEST_CASE("BindingContext is notified in case of notifier errors") {
  1501. _impl::RealmCoordinator::assert_no_open_realms();
  1502. class OpenFileLimiter {
  1503. public:
  1504. OpenFileLimiter()
  1505. {
  1506. // Set the max open files to zero so that opening new files will fail
  1507. getrlimit(RLIMIT_NOFILE, &m_old);
  1508. rlimit rl = m_old;
  1509. rl.rlim_cur = 0;
  1510. setrlimit(RLIMIT_NOFILE, &rl);
  1511. }
  1512. ~OpenFileLimiter()
  1513. {
  1514. setrlimit(RLIMIT_NOFILE, &m_old);
  1515. }
  1516. private:
  1517. rlimit m_old;
  1518. };
  1519. InMemoryTestFile config;
  1520. config.automatic_change_notifications = false;
  1521. auto r = Realm::get_shared_realm(config);
  1522. r->update_schema({
  1523. {"object", {
  1524. {"value", PropertyType::Int}
  1525. }},
  1526. });
  1527. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  1528. auto table = r->read_group().get_table("class_object");
  1529. Results results(r, *r->read_group().get_table("class_object"));
  1530. static int binding_context_start_notify_calls = 0;
  1531. static int binding_context_end_notify_calls = 0;
  1532. static bool error_called = false;
  1533. struct Context : BindingContext {
  1534. void will_send_notifications() override
  1535. {
  1536. REQUIRE_FALSE(error_called);
  1537. ++binding_context_start_notify_calls;
  1538. }
  1539. void did_send_notifications() override
  1540. {
  1541. REQUIRE(error_called);
  1542. ++binding_context_end_notify_calls;
  1543. }
  1544. };
  1545. r->m_binding_context.reset(new Context());
  1546. SECTION("realm on background thread could not be opened") {
  1547. OpenFileLimiter limiter;
  1548. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  1549. REQUIRE(err);
  1550. REQUIRE_FALSE(error_called);
  1551. error_called = true;
  1552. });
  1553. advance_and_notify(*r);
  1554. REQUIRE(error_called);
  1555. REQUIRE(binding_context_start_notify_calls == 1);
  1556. REQUIRE(binding_context_end_notify_calls == 1);
  1557. }
  1558. }
  1559. #endif
  1560. TEST_CASE("RealmCoordinator: get_unbound_realm()") {
  1561. TestFile config;
  1562. config.schema = Schema{
  1563. {"object", {
  1564. {"value", PropertyType::Int}
  1565. }},
  1566. };
  1567. ThreadSafeReference ref;
  1568. std::thread([&] { ref = _impl::RealmCoordinator::get_coordinator(config)->get_unbound_realm(); }).join();
  1569. SECTION("checks thread after being resolved") {
  1570. auto realm = Realm::get_shared_realm(std::move(ref));
  1571. REQUIRE_NOTHROW(realm->verify_thread());
  1572. std::thread([&] {
  1573. REQUIRE_THROWS(realm->verify_thread());
  1574. }).join();
  1575. }
  1576. SECTION("delivers notifications to the thread it is resolved on") {
  1577. if (!util::EventLoop::has_implementation())
  1578. return;
  1579. auto realm = Realm::get_shared_realm(std::move(ref));
  1580. Results results(realm, ObjectStore::table_for_object_type(realm->read_group(), "object")->where());
  1581. bool called = false;
  1582. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  1583. called = true;
  1584. });
  1585. util::EventLoop::main().run_until([&] { return called; });
  1586. }
  1587. SECTION("resolves to a new Realm if caching is disabled") {
  1588. auto r1 = Realm::get_shared_realm(config);
  1589. auto r2 = Realm::get_shared_realm(std::move(ref));
  1590. REQUIRE(r1 != r2);
  1591. // New unbound with cache disabled
  1592. std::thread([&] { ref = _impl::RealmCoordinator::get_coordinator(config)->get_unbound_realm(); }).join();
  1593. auto r3 = Realm::get_shared_realm(std::move(ref));
  1594. REQUIRE(r1 != r3);
  1595. REQUIRE(r2 != r3);
  1596. }
  1597. }