results.cpp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116
  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/index_helpers.hpp"
  21. #include "util/test_file.hpp"
  22. #include "impl/object_accessor_impl.hpp"
  23. #include "impl/realm_coordinator.hpp"
  24. #include "binding_context.hpp"
  25. #include "object_schema.hpp"
  26. #include "property.hpp"
  27. #include "results.hpp"
  28. #include "schema.hpp"
  29. #include <realm/group_shared.hpp>
  30. #include <realm/link_view.hpp>
  31. #include <realm/query_engine.hpp>
  32. #include <realm/query_expression.hpp>
  33. #if REALM_ENABLE_SYNC
  34. #include "sync/sync_manager.hpp"
  35. #include "sync/sync_session.hpp"
  36. #endif
  37. namespace realm {
  38. class TestHelper {
  39. public:
  40. static SharedGroup& get_shared_group(SharedRealm const& shared_realm)
  41. {
  42. return *Realm::Internal::get_shared_group(*shared_realm);
  43. }
  44. };
  45. }
  46. using namespace realm;
  47. using namespace std::string_literals;
  48. namespace {
  49. using AnyDict = std::map<std::string, util::Any>;
  50. using AnyVec = std::vector<util::Any>;
  51. }
  52. struct TestContext : CppContext {
  53. std::map<std::string, AnyDict> defaults;
  54. using CppContext::CppContext;
  55. TestContext(TestContext& parent, realm::Property const& prop)
  56. : CppContext(parent, prop)
  57. , defaults(parent.defaults)
  58. { }
  59. void will_change(Object const&, Property const&) {}
  60. void did_change() {}
  61. std::string print(util::Any) { return "not implemented"; }
  62. bool allow_missing(util::Any) { return false; }
  63. };
  64. TEST_CASE("notifications: async delivery") {
  65. _impl::RealmCoordinator::assert_no_open_realms();
  66. InMemoryTestFile config;
  67. config.cache = false;
  68. config.automatic_change_notifications = false;
  69. auto r = Realm::get_shared_realm(config);
  70. r->update_schema({
  71. {"object", {
  72. {"value", PropertyType::Int}
  73. }},
  74. });
  75. auto coordinator = _impl::RealmCoordinator::get_existing_coordinator(config.path);
  76. auto table = r->read_group().get_table("class_object");
  77. r->begin_transaction();
  78. table->add_empty_row(10);
  79. for (int i = 0; i < 10; ++i)
  80. table->set_int(0, i, i * 2);
  81. r->commit_transaction();
  82. Results results(r, table->where().greater(0, 0).less(0, 10));
  83. int notification_calls = 0;
  84. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  85. REQUIRE_FALSE(err);
  86. ++notification_calls;
  87. });
  88. auto make_local_change = [&] {
  89. r->begin_transaction();
  90. table->set_int(0, 0, 4);
  91. r->commit_transaction();
  92. };
  93. auto make_remote_change = [&] {
  94. auto r2 = coordinator->get_realm();
  95. r2->begin_transaction();
  96. r2->read_group().get_table("class_object")->set_int(0, 0, 5);
  97. r2->commit_transaction();
  98. };
  99. SECTION("initial notification") {
  100. SECTION("is delivered on notify()") {
  101. REQUIRE(notification_calls == 0);
  102. advance_and_notify(*r);
  103. REQUIRE(notification_calls == 1);
  104. }
  105. SECTION("is delivered on refresh()") {
  106. coordinator->on_change();
  107. REQUIRE(notification_calls == 0);
  108. r->refresh();
  109. REQUIRE(notification_calls == 1);
  110. }
  111. SECTION("is delivered on begin_transaction()") {
  112. coordinator->on_change();
  113. REQUIRE(notification_calls == 0);
  114. r->begin_transaction();
  115. REQUIRE(notification_calls == 1);
  116. r->cancel_transaction();
  117. }
  118. SECTION("is delivered on notify() even with autorefresh disabled") {
  119. r->set_auto_refresh(false);
  120. REQUIRE(notification_calls == 0);
  121. advance_and_notify(*r);
  122. REQUIRE(notification_calls == 1);
  123. }
  124. SECTION("refresh() blocks due to initial results not being ready") {
  125. REQUIRE(notification_calls == 0);
  126. JoiningThread thread([&] {
  127. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  128. coordinator->on_change();
  129. });
  130. r->refresh();
  131. REQUIRE(notification_calls == 1);
  132. }
  133. SECTION("begin_transaction() blocks due to initial results not being ready") {
  134. REQUIRE(notification_calls == 0);
  135. JoiningThread thread([&] {
  136. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  137. coordinator->on_change();
  138. });
  139. r->begin_transaction();
  140. REQUIRE(notification_calls == 1);
  141. r->cancel_transaction();
  142. }
  143. SECTION("notify() does not block due to initial results not being ready") {
  144. REQUIRE(notification_calls == 0);
  145. r->notify();
  146. REQUIRE(notification_calls == 0);
  147. }
  148. SECTION("is delivered after invalidate()") {
  149. r->invalidate();
  150. SECTION("notify()") {
  151. coordinator->on_change();
  152. REQUIRE_FALSE(r->is_in_read_transaction());
  153. r->notify();
  154. REQUIRE(notification_calls == 1);
  155. }
  156. SECTION("notify() without autorefresh") {
  157. r->set_auto_refresh(false);
  158. coordinator->on_change();
  159. REQUIRE_FALSE(r->is_in_read_transaction());
  160. r->notify();
  161. REQUIRE(notification_calls == 1);
  162. }
  163. SECTION("refresh()") {
  164. coordinator->on_change();
  165. REQUIRE_FALSE(r->is_in_read_transaction());
  166. r->refresh();
  167. REQUIRE(notification_calls == 1);
  168. }
  169. SECTION("begin_transaction()") {
  170. coordinator->on_change();
  171. REQUIRE_FALSE(r->is_in_read_transaction());
  172. r->begin_transaction();
  173. REQUIRE(notification_calls == 1);
  174. r->cancel_transaction();
  175. }
  176. }
  177. SECTION("is delivered by notify() even if there are later versions") {
  178. REQUIRE(notification_calls == 0);
  179. coordinator->on_change();
  180. make_remote_change();
  181. r->notify();
  182. REQUIRE(notification_calls == 1);
  183. }
  184. }
  185. advance_and_notify(*r);
  186. SECTION("notifications for local changes") {
  187. make_local_change();
  188. coordinator->on_change();
  189. REQUIRE(notification_calls == 1);
  190. SECTION("notify()") {
  191. r->notify();
  192. REQUIRE(notification_calls == 2);
  193. }
  194. SECTION("notify() without autorefresh") {
  195. r->set_auto_refresh(false);
  196. r->notify();
  197. REQUIRE(notification_calls == 2);
  198. }
  199. SECTION("refresh()") {
  200. r->refresh();
  201. REQUIRE(notification_calls == 2);
  202. }
  203. SECTION("begin_transaction()") {
  204. r->begin_transaction();
  205. REQUIRE(notification_calls == 2);
  206. r->cancel_transaction();
  207. }
  208. }
  209. SECTION("notifications for remote changes") {
  210. make_remote_change();
  211. coordinator->on_change();
  212. REQUIRE(notification_calls == 1);
  213. SECTION("notify()") {
  214. r->notify();
  215. REQUIRE(notification_calls == 2);
  216. }
  217. SECTION("notify() without autorefresh") {
  218. r->set_auto_refresh(false);
  219. r->notify();
  220. REQUIRE(notification_calls == 1);
  221. r->refresh();
  222. REQUIRE(notification_calls == 2);
  223. }
  224. SECTION("refresh()") {
  225. r->refresh();
  226. REQUIRE(notification_calls == 2);
  227. }
  228. SECTION("begin_transaction()") {
  229. r->begin_transaction();
  230. REQUIRE(notification_calls == 2);
  231. r->cancel_transaction();
  232. }
  233. }
  234. SECTION("notifications are not delivered when the token is destroyed before they are calculated") {
  235. make_remote_change();
  236. REQUIRE(notification_calls == 1);
  237. token = {};
  238. advance_and_notify(*r);
  239. REQUIRE(notification_calls == 1);
  240. }
  241. SECTION("notifications are not delivered when the token is destroyed before they are delivered") {
  242. make_remote_change();
  243. REQUIRE(notification_calls == 1);
  244. coordinator->on_change();
  245. token = {};
  246. r->notify();
  247. REQUIRE(notification_calls == 1);
  248. }
  249. SECTION("notifications are delivered on the next cycle when a new callback is added from within a callback") {
  250. NotificationToken token2, token3;
  251. bool called = false;
  252. token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  253. token2 = {};
  254. token3 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  255. called = true;
  256. });
  257. });
  258. advance_and_notify(*r);
  259. REQUIRE_FALSE(called);
  260. advance_and_notify(*r);
  261. REQUIRE(called);
  262. }
  263. SECTION("notifications are delivered on the next cycle when a new callback is added from within a callback") {
  264. auto results2 = results;
  265. auto results3 = results;
  266. NotificationToken token2, token3, token4;
  267. bool called = false;
  268. auto check = [&](Results& outer, Results& inner) {
  269. token2 = outer.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  270. token2 = {};
  271. token3 = inner.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  272. called = true;
  273. });
  274. });
  275. advance_and_notify(*r);
  276. REQUIRE_FALSE(called);
  277. advance_and_notify(*r);
  278. REQUIRE(called);
  279. };
  280. SECTION("same Results") {
  281. check(results, results);
  282. }
  283. SECTION("Results which has never had a notifier") {
  284. check(results, results2);
  285. }
  286. SECTION("Results which used to have callbacks but no longer does") {
  287. SECTION("notifier before active") {
  288. token3 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  289. token3 = {};
  290. });
  291. check(results3, results2);
  292. }
  293. SECTION("notifier after active") {
  294. token3 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  295. token3 = {};
  296. });
  297. check(results, results2);
  298. }
  299. }
  300. SECTION("Results which already has callbacks") {
  301. SECTION("notifier before active") {
  302. token4 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) { });
  303. check(results3, results2);
  304. }
  305. SECTION("notifier after active") {
  306. token4 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) { });
  307. check(results, results2);
  308. }
  309. }
  310. }
  311. SECTION("remote changes made before adding a callback from within a callback are not reported") {
  312. NotificationToken token2, token3;
  313. bool called = false;
  314. token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  315. token2 = {};
  316. make_remote_change();
  317. coordinator->on_change();
  318. token3 = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr) {
  319. called = true;
  320. REQUIRE(c.empty());
  321. REQUIRE(table->get_int(0, 0) == 5);
  322. });
  323. });
  324. advance_and_notify(*r);
  325. REQUIRE_FALSE(called);
  326. advance_and_notify(*r);
  327. REQUIRE(called);
  328. }
  329. SECTION("notifications are not delivered when a callback is removed from within a callback") {
  330. NotificationToken token2, token3;
  331. token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  332. token3 = {};
  333. });
  334. token3 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  335. REQUIRE(false);
  336. });
  337. advance_and_notify(*r);
  338. }
  339. SECTION("removing the current callback does not stop later ones from being called") {
  340. NotificationToken token2, token3;
  341. bool called = false;
  342. token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  343. token2 = {};
  344. });
  345. token3 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  346. called = true;
  347. });
  348. advance_and_notify(*r);
  349. REQUIRE(called);
  350. }
  351. SECTION("the first call of a notification can include changes if it previously ran for a different callback") {
  352. r->begin_transaction();
  353. auto token2 = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr) {
  354. REQUIRE(!c.empty());
  355. });
  356. table->set_int(0, table->add_empty_row(), 5);
  357. r->commit_transaction();
  358. advance_and_notify(*r);
  359. }
  360. SECTION("handling of results not ready") {
  361. make_remote_change();
  362. SECTION("notify() does nothing") {
  363. r->notify();
  364. REQUIRE(notification_calls == 1);
  365. coordinator->on_change();
  366. r->notify();
  367. REQUIRE(notification_calls == 2);
  368. }
  369. SECTION("refresh() blocks") {
  370. REQUIRE(notification_calls == 1);
  371. JoiningThread thread([&] {
  372. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  373. coordinator->on_change();
  374. });
  375. r->refresh();
  376. REQUIRE(notification_calls == 2);
  377. }
  378. SECTION("refresh() advances to the first version with notifiers ready that is at least a recent as the newest at the time it is called") {
  379. JoiningThread thread([&] {
  380. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  381. make_remote_change();
  382. coordinator->on_change();
  383. make_remote_change();
  384. });
  385. // advances to the version after the one it was waiting for, but still
  386. // not the latest
  387. r->refresh();
  388. REQUIRE(notification_calls == 2);
  389. thread.join();
  390. REQUIRE(notification_calls == 2);
  391. // now advances to the latest
  392. coordinator->on_change();
  393. r->refresh();
  394. REQUIRE(notification_calls == 3);
  395. }
  396. SECTION("begin_transaction() blocks") {
  397. REQUIRE(notification_calls == 1);
  398. JoiningThread thread([&] {
  399. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  400. coordinator->on_change();
  401. });
  402. r->begin_transaction();
  403. REQUIRE(notification_calls == 2);
  404. r->cancel_transaction();
  405. }
  406. SECTION("refresh() does not block for results without callbacks") {
  407. token = {};
  408. // this would deadlock if it waits for the notifier to be ready
  409. r->refresh();
  410. }
  411. SECTION("begin_transaction() does not block for results without callbacks") {
  412. token = {};
  413. // this would deadlock if it waits for the notifier to be ready
  414. r->begin_transaction();
  415. r->cancel_transaction();
  416. }
  417. SECTION("begin_transaction() does not block for Results for different Realms") {
  418. // this would deadlock if beginning the write on the secondary Realm
  419. // waited for the primary Realm to be ready
  420. make_remote_change();
  421. // sanity check that the notifications never did run
  422. r->notify();
  423. REQUIRE(notification_calls == 1);
  424. }
  425. }
  426. SECTION("handling of stale results") {
  427. make_remote_change();
  428. coordinator->on_change();
  429. make_remote_change();
  430. SECTION("notify() uses the older version") {
  431. r->notify();
  432. REQUIRE(notification_calls == 2);
  433. coordinator->on_change();
  434. r->notify();
  435. REQUIRE(notification_calls == 3);
  436. r->notify();
  437. REQUIRE(notification_calls == 3);
  438. }
  439. SECTION("refresh() blocks") {
  440. REQUIRE(notification_calls == 1);
  441. JoiningThread thread([&] {
  442. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  443. coordinator->on_change();
  444. });
  445. r->refresh();
  446. REQUIRE(notification_calls == 2);
  447. }
  448. SECTION("begin_transaction() blocks") {
  449. REQUIRE(notification_calls == 1);
  450. JoiningThread thread([&] {
  451. std::this_thread::sleep_for(std::chrono::microseconds(5000));
  452. coordinator->on_change();
  453. });
  454. r->begin_transaction();
  455. REQUIRE(notification_calls == 2);
  456. r->cancel_transaction();
  457. }
  458. }
  459. SECTION("updates are delivered after invalidate()") {
  460. r->invalidate();
  461. make_remote_change();
  462. SECTION("notify()") {
  463. coordinator->on_change();
  464. REQUIRE_FALSE(r->is_in_read_transaction());
  465. r->notify();
  466. REQUIRE(notification_calls == 2);
  467. }
  468. SECTION("notify() without autorefresh") {
  469. r->set_auto_refresh(false);
  470. coordinator->on_change();
  471. REQUIRE_FALSE(r->is_in_read_transaction());
  472. r->notify();
  473. REQUIRE(notification_calls == 1);
  474. r->refresh();
  475. REQUIRE(notification_calls == 2);
  476. }
  477. SECTION("refresh()") {
  478. coordinator->on_change();
  479. REQUIRE_FALSE(r->is_in_read_transaction());
  480. r->refresh();
  481. REQUIRE(notification_calls == 2);
  482. }
  483. SECTION("begin_transaction()") {
  484. coordinator->on_change();
  485. REQUIRE_FALSE(r->is_in_read_transaction());
  486. r->begin_transaction();
  487. REQUIRE(notification_calls == 2);
  488. r->cancel_transaction();
  489. }
  490. }
  491. SECTION("refresh() from within changes_available() do not interfere with notification delivery") {
  492. struct Context : BindingContext {
  493. Realm& realm;
  494. Context(Realm& realm) : realm(realm) { }
  495. void changes_available() override
  496. {
  497. REQUIRE(realm.refresh());
  498. }
  499. };
  500. make_remote_change();
  501. coordinator->on_change();
  502. r->set_auto_refresh(false);
  503. REQUIRE(notification_calls == 1);
  504. r->notify();
  505. REQUIRE(notification_calls == 1);
  506. r->m_binding_context.reset(new Context(*r));
  507. r->notify();
  508. REQUIRE(notification_calls == 2);
  509. }
  510. SECTION("refresh() from within a notification is a no-op") {
  511. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  512. REQUIRE_FALSE(err);
  513. REQUIRE_FALSE(r->refresh()); // would deadlock if it actually tried to refresh
  514. });
  515. advance_and_notify(*r);
  516. make_remote_change(); // 1
  517. coordinator->on_change();
  518. make_remote_change(); // 2
  519. r->notify(); // advances to version from 1
  520. coordinator->on_change();
  521. REQUIRE(r->refresh()); // advances to version from 2
  522. REQUIRE_FALSE(r->refresh()); // does not advance since it's now up-to-date
  523. }
  524. SECTION("begin_transaction() from within a notification does not send notifications immediately") {
  525. bool first = true;
  526. auto token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  527. REQUIRE_FALSE(err);
  528. if (first)
  529. first = false;
  530. else {
  531. // would deadlock if it tried to send notifications as they aren't ready yet
  532. r->begin_transaction();
  533. r->cancel_transaction();
  534. }
  535. });
  536. advance_and_notify(*r);
  537. make_remote_change(); // 1
  538. coordinator->on_change();
  539. make_remote_change(); // 2
  540. r->notify(); // advances to version from 1
  541. REQUIRE(notification_calls == 2);
  542. coordinator->on_change();
  543. REQUIRE_FALSE(r->refresh()); // we made the commit locally, so no advancing here
  544. REQUIRE(notification_calls == 3);
  545. }
  546. SECTION("begin_transaction() from within a notification does not break delivering additional notifications") {
  547. size_t calls = 0;
  548. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  549. REQUIRE_FALSE(err);
  550. if (++calls == 1)
  551. return;
  552. // force the read version to advance by beginning a transaction
  553. r->begin_transaction();
  554. r->cancel_transaction();
  555. });
  556. auto results2 = results;
  557. size_t calls2 = 0;
  558. auto token2 = results2.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  559. REQUIRE_FALSE(err);
  560. if (++calls2 == 1)
  561. return;
  562. REQUIRE_INDICES(c.insertions, 0);
  563. });
  564. advance_and_notify(*r);
  565. REQUIRE(calls == 1);
  566. REQUIRE(calls2 == 1);
  567. make_remote_change(); // 1
  568. coordinator->on_change();
  569. make_remote_change(); // 2
  570. r->notify(); // advances to version from 1
  571. REQUIRE(calls == 2);
  572. REQUIRE(calls2 == 2);
  573. }
  574. SECTION("begin_transaction() from within did_change() does not break delivering collection notification") {
  575. struct Context : BindingContext {
  576. Realm& realm;
  577. Context(Realm& realm) : realm(realm) { }
  578. void did_change(std::vector<ObserverState> const&, std::vector<void*> const&, bool) override
  579. {
  580. if (!realm.is_in_transaction()) {
  581. // advances to version from 2 (and recursively calls this, hence the check above)
  582. realm.begin_transaction();
  583. realm.cancel_transaction();
  584. }
  585. }
  586. };
  587. r->m_binding_context.reset(new Context(*r));
  588. make_remote_change(); // 1
  589. coordinator->on_change();
  590. make_remote_change(); // 2
  591. r->notify(); // advances to version from 1
  592. }
  593. SECTION("is_in_transaction() is reported correctly within a notification from begin_transaction() and changes can be made") {
  594. bool first = true;
  595. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  596. REQUIRE_FALSE(err);
  597. if (first) {
  598. REQUIRE_FALSE(r->is_in_transaction());
  599. first = false;
  600. }
  601. else {
  602. REQUIRE(r->is_in_transaction());
  603. table->set_int(0, 0, 100);
  604. }
  605. });
  606. advance_and_notify(*r);
  607. make_remote_change();
  608. coordinator->on_change();
  609. r->begin_transaction();
  610. REQUIRE(table->get_int(0, 0) == 100);
  611. r->cancel_transaction();
  612. REQUIRE(table->get_int(0, 0) != 100);
  613. }
  614. SECTION("invalidate() from within notification is a no-op") {
  615. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  616. REQUIRE_FALSE(err);
  617. r->invalidate();
  618. REQUIRE(r->is_in_read_transaction());
  619. });
  620. advance_and_notify(*r);
  621. REQUIRE(r->is_in_read_transaction());
  622. make_remote_change();
  623. coordinator->on_change();
  624. r->begin_transaction();
  625. REQUIRE(r->is_in_transaction());
  626. r->cancel_transaction();
  627. }
  628. SECTION("cancel_transaction() from within notification ends the write transaction started by begin_transaction()") {
  629. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  630. REQUIRE_FALSE(err);
  631. if (r->is_in_transaction())
  632. r->cancel_transaction();
  633. });
  634. advance_and_notify(*r);
  635. make_remote_change();
  636. coordinator->on_change();
  637. r->begin_transaction();
  638. REQUIRE_FALSE(r->is_in_transaction());
  639. }
  640. }
  641. TEST_CASE("notifications: skip") {
  642. _impl::RealmCoordinator::assert_no_open_realms();
  643. InMemoryTestFile config;
  644. config.cache = false;
  645. config.automatic_change_notifications = false;
  646. auto r = Realm::get_shared_realm(config);
  647. r->update_schema({
  648. {"object", {
  649. {"value", PropertyType::Int}
  650. }},
  651. });
  652. auto coordinator = _impl::RealmCoordinator::get_existing_coordinator(config.path);
  653. auto table = r->read_group().get_table("class_object");
  654. r->begin_transaction();
  655. table->add_empty_row(10);
  656. for (int i = 0; i < 10; ++i)
  657. table->set_int(0, i, i * 2);
  658. r->commit_transaction();
  659. Results results(r, table->where());
  660. auto add_callback = [](Results& results, int& calls, CollectionChangeSet& changes) {
  661. return results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  662. REQUIRE_FALSE(err);
  663. ++calls;
  664. changes = std::move(c);
  665. });
  666. };
  667. auto make_local_change = [&](auto& token) {
  668. r->begin_transaction();
  669. table->add_empty_row();
  670. token.suppress_next();
  671. r->commit_transaction();
  672. };
  673. auto make_remote_change = [&] {
  674. auto r2 = coordinator->get_realm();
  675. r2->begin_transaction();
  676. r2->read_group().get_table("class_object")->add_empty_row();
  677. r2->commit_transaction();
  678. };
  679. int calls1 = 0;
  680. CollectionChangeSet changes1;
  681. auto token1 = add_callback(results, calls1, changes1);
  682. SECTION("no notification is sent when only callback is skipped") {
  683. advance_and_notify(*r);
  684. REQUIRE(calls1 == 1);
  685. make_local_change(token1);
  686. advance_and_notify(*r);
  687. REQUIRE(calls1 == 1);
  688. REQUIRE(changes1.empty());
  689. }
  690. SECTION("unskipped tokens for the same Results are still delivered") {
  691. int calls2 = 0;
  692. CollectionChangeSet changes2;
  693. auto token2 = add_callback(results, calls2, changes2);
  694. advance_and_notify(*r);
  695. REQUIRE(calls1 == 1);
  696. REQUIRE(calls2 == 1);
  697. make_local_change(token1);
  698. advance_and_notify(*r);
  699. REQUIRE(calls1 == 1);
  700. REQUIRE(changes1.empty());
  701. REQUIRE(calls2 == 2);
  702. REQUIRE_INDICES(changes2.insertions, 10);
  703. }
  704. SECTION("unskipped tokens for different Results are still delivered") {
  705. Results results2(r, table->where());
  706. int calls2 = 0;
  707. CollectionChangeSet changes2;
  708. auto token2 = add_callback(results2, calls2, changes2);
  709. advance_and_notify(*r);
  710. REQUIRE(calls1 == 1);
  711. REQUIRE(calls2 == 1);
  712. make_local_change(token1);
  713. advance_and_notify(*r);
  714. REQUIRE(calls1 == 1);
  715. REQUIRE(changes1.empty());
  716. REQUIRE(calls2 == 2);
  717. REQUIRE_INDICES(changes2.insertions, 10);
  718. }
  719. SECTION("additional commits which occur before calculation are merged in") {
  720. int calls2 = 0;
  721. CollectionChangeSet changes2;
  722. auto token2 = add_callback(results, calls2, changes2);
  723. advance_and_notify(*r);
  724. REQUIRE(calls1 == 1);
  725. REQUIRE(calls2 == 1);
  726. make_local_change(token1);
  727. make_remote_change();
  728. advance_and_notify(*r);
  729. REQUIRE(calls1 == 2);
  730. REQUIRE_INDICES(changes1.insertions, 11);
  731. REQUIRE(calls2 == 2);
  732. REQUIRE_INDICES(changes2.insertions, 10, 11);
  733. }
  734. SECTION("additional commits which occur before delivery are merged in") {
  735. int calls2 = 0;
  736. CollectionChangeSet changes2;
  737. auto token2 = add_callback(results, calls2, changes2);
  738. advance_and_notify(*r);
  739. REQUIRE(calls1 == 1);
  740. REQUIRE(calls2 == 1);
  741. make_local_change(token1);
  742. coordinator->on_change();
  743. make_remote_change();
  744. advance_and_notify(*r);
  745. REQUIRE(calls1 == 2);
  746. REQUIRE_INDICES(changes1.insertions, 11);
  747. REQUIRE(calls2 == 2);
  748. REQUIRE_INDICES(changes2.insertions, 10, 11);
  749. }
  750. SECTION("skipping must be done from within a write transaction") {
  751. REQUIRE_THROWS(token1.suppress_next());
  752. }
  753. SECTION("skipping must be done from the Realm's thread") {
  754. advance_and_notify(*r);
  755. r->begin_transaction();
  756. std::thread([&] {
  757. REQUIRE_THROWS(token1.suppress_next());
  758. }).join();
  759. r->cancel_transaction();
  760. }
  761. SECTION("new notifiers do not interfere with skipping") {
  762. advance_and_notify(*r);
  763. REQUIRE(calls1 == 1);
  764. CollectionChangeSet changes;
  765. // new notifier at a version before the skipped one
  766. auto r2 = coordinator->get_realm();
  767. Results results2(r2, r2->read_group().get_table("class_object")->where());
  768. int calls2 = 0;
  769. auto token2 = add_callback(results2, calls2, changes);
  770. make_local_change(token1);
  771. // new notifier at the skipped version
  772. auto r3 = coordinator->get_realm();
  773. Results results3(r3, r3->read_group().get_table("class_object")->where());
  774. int calls3 = 0;
  775. auto token3 = add_callback(results3, calls3, changes);
  776. make_remote_change();
  777. // new notifier at version after the skipped one
  778. auto r4 = coordinator->get_realm();
  779. Results results4(r4, r4->read_group().get_table("class_object")->where());
  780. int calls4 = 0;
  781. auto token4 = add_callback(results4, calls4, changes);
  782. coordinator->on_change();
  783. r->notify();
  784. r2->notify();
  785. r3->notify();
  786. r4->notify();
  787. REQUIRE(calls1 == 2);
  788. REQUIRE(calls2 == 1);
  789. REQUIRE(calls3 == 1);
  790. REQUIRE(calls4 == 1);
  791. }
  792. SECTION("skipping only effects the current transaction even if no notification would occur anyway") {
  793. advance_and_notify(*r);
  794. REQUIRE(calls1 == 1);
  795. // would not produce a notification even if it wasn't skipped because no changes were made
  796. r->begin_transaction();
  797. token1.suppress_next();
  798. r->commit_transaction();
  799. advance_and_notify(*r);
  800. REQUIRE(calls1 == 1);
  801. // should now produce a notification
  802. r->begin_transaction();
  803. table->add_empty_row();
  804. r->commit_transaction();
  805. advance_and_notify(*r);
  806. REQUIRE(calls1 == 2);
  807. }
  808. SECTION("removing skipped notifier before it gets the chance to run") {
  809. advance_and_notify(*r);
  810. REQUIRE(calls1 == 1);
  811. // Set the skip version
  812. make_local_change(token1);
  813. // Advance the file to a version after the skip version
  814. make_remote_change();
  815. REQUIRE(calls1 == 1);
  816. // Remove the skipped notifier and add an entirely new notifier, so that
  817. // notifications need to run but the skip logic shouldn't be used
  818. token1 = {};
  819. results = {};
  820. Results results2(r, table->where());
  821. auto token2 = add_callback(results2, calls1, changes1);
  822. advance_and_notify(*r);
  823. REQUIRE(calls1 == 2);
  824. }
  825. }
  826. #if REALM_PLATFORM_APPLE
  827. TEST_CASE("notifications: async error handling") {
  828. _impl::RealmCoordinator::assert_no_open_realms();
  829. InMemoryTestFile config;
  830. config.cache = false;
  831. config.automatic_change_notifications = false;
  832. auto r = Realm::get_shared_realm(config);
  833. r->update_schema({
  834. {"object", {
  835. {"value", PropertyType::Int},
  836. }},
  837. });
  838. auto coordinator = _impl::RealmCoordinator::get_existing_coordinator(config.path);
  839. Results results(r, *r->read_group().get_table("class_object"));
  840. auto r2 = Realm::get_shared_realm(config);
  841. class OpenFileLimiter {
  842. public:
  843. OpenFileLimiter()
  844. {
  845. // Set the max open files to zero so that opening new files will fail
  846. getrlimit(RLIMIT_NOFILE, &m_old);
  847. rlimit rl = m_old;
  848. rl.rlim_cur = 0;
  849. setrlimit(RLIMIT_NOFILE, &rl);
  850. }
  851. ~OpenFileLimiter()
  852. {
  853. setrlimit(RLIMIT_NOFILE, &m_old);
  854. }
  855. private:
  856. rlimit m_old;
  857. };
  858. SECTION("error when opening the advancer SG") {
  859. OpenFileLimiter limiter;
  860. bool called = false;
  861. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  862. REQUIRE(err);
  863. REQUIRE_FALSE(called);
  864. called = true;
  865. });
  866. REQUIRE(!called);
  867. SECTION("error is delivered on notify() without changes") {
  868. coordinator->on_change();
  869. REQUIRE(!called);
  870. r->notify();
  871. REQUIRE(called);
  872. }
  873. SECTION("error is delivered on notify() with changes") {
  874. r2->begin_transaction(); r2->commit_transaction();
  875. REQUIRE(!called);
  876. coordinator->on_change();
  877. REQUIRE(!called);
  878. r->notify();
  879. REQUIRE(called);
  880. }
  881. SECTION("error is delivered on refresh() without changes") {
  882. coordinator->on_change();
  883. REQUIRE(!called);
  884. r->refresh();
  885. REQUIRE(called);
  886. }
  887. SECTION("error is delivered on refresh() with changes") {
  888. r2->begin_transaction(); r2->commit_transaction();
  889. REQUIRE(!called);
  890. coordinator->on_change();
  891. REQUIRE(!called);
  892. r->refresh();
  893. REQUIRE(called);
  894. }
  895. SECTION("error is delivered on begin_transaction() without changes") {
  896. coordinator->on_change();
  897. REQUIRE(!called);
  898. r->begin_transaction();
  899. REQUIRE(called);
  900. r->cancel_transaction();
  901. }
  902. SECTION("error is delivered on begin_transaction() with changes") {
  903. r2->begin_transaction(); r2->commit_transaction();
  904. REQUIRE(!called);
  905. coordinator->on_change();
  906. REQUIRE(!called);
  907. r->begin_transaction();
  908. REQUIRE(called);
  909. r->cancel_transaction();
  910. }
  911. SECTION("adding another callback sends the error to only the newly added one") {
  912. advance_and_notify(*r);
  913. REQUIRE(called);
  914. bool called2 = false;
  915. auto token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  916. REQUIRE(err);
  917. REQUIRE_FALSE(called2);
  918. called2 = true;
  919. });
  920. advance_and_notify(*r);
  921. REQUIRE(called2);
  922. }
  923. SECTION("destroying a token from before the error does not remove newly added callbacks") {
  924. advance_and_notify(*r);
  925. bool called = false;
  926. auto token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  927. REQUIRE(err);
  928. REQUIRE_FALSE(called);
  929. called = true;
  930. });
  931. token = {};
  932. advance_and_notify(*r);
  933. REQUIRE(called);
  934. }
  935. SECTION("adding another callback from within an error callback defers delivery") {
  936. NotificationToken token2;
  937. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  938. token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  939. REQUIRE(err);
  940. REQUIRE_FALSE(called);
  941. called = true;
  942. });
  943. });
  944. advance_and_notify(*r);
  945. REQUIRE(!called);
  946. advance_and_notify(*r);
  947. REQUIRE(called);
  948. }
  949. SECTION("adding a callback to a different collection from within the error callback defers delivery") {
  950. auto results2 = results;
  951. NotificationToken token2;
  952. token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) {
  953. token2 = results2.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  954. REQUIRE(err);
  955. REQUIRE_FALSE(called);
  956. called = true;
  957. });
  958. });
  959. advance_and_notify(*r);
  960. REQUIRE(!called);
  961. advance_and_notify(*r);
  962. REQUIRE(called);
  963. }
  964. }
  965. SECTION("error when opening the executor SG") {
  966. SECTION("error is delivered asynchronously") {
  967. bool called = false;
  968. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  969. REQUIRE(err);
  970. called = true;
  971. });
  972. OpenFileLimiter limiter;
  973. REQUIRE(!called);
  974. coordinator->on_change();
  975. REQUIRE(!called);
  976. r->notify();
  977. REQUIRE(called);
  978. }
  979. SECTION("adding another callback only sends the error to the new one") {
  980. bool called = false;
  981. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  982. REQUIRE(err);
  983. REQUIRE_FALSE(called);
  984. called = true;
  985. });
  986. OpenFileLimiter limiter;
  987. advance_and_notify(*r);
  988. bool called2 = false;
  989. auto token2 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  990. REQUIRE(err);
  991. REQUIRE_FALSE(called2);
  992. called2 = true;
  993. });
  994. advance_and_notify(*r);
  995. REQUIRE(called2);
  996. }
  997. }
  998. }
  999. #endif
  1000. #if REALM_ENABLE_SYNC
  1001. TEST_CASE("notifications: sync") {
  1002. _impl::RealmCoordinator::assert_no_open_realms();
  1003. SyncServer server(false);
  1004. SyncTestFile config(server);
  1005. config.cache = false;
  1006. config.schema = Schema{
  1007. {"object", {
  1008. {"value", PropertyType::Int},
  1009. }},
  1010. };
  1011. SECTION("sync progress commits do not distrupt notifications") {
  1012. auto r = Realm::get_shared_realm(config);
  1013. auto wait_realm = Realm::get_shared_realm(config);
  1014. Results results(r, *r->read_group().get_table("class_object"));
  1015. Results wait_results(wait_realm, *wait_realm->read_group().get_table("class_object"));
  1016. auto token1 = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) { });
  1017. auto token2 = wait_results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr) { });
  1018. // Add an object to the Realm so that notifications are needed
  1019. {
  1020. auto write_realm = Realm::get_shared_realm(config);
  1021. write_realm->begin_transaction();
  1022. sync::create_object(write_realm->read_group(), *write_realm->read_group().get_table("class_object"));
  1023. write_realm->commit_transaction();
  1024. }
  1025. // Wait for the notifications to become ready for the new version
  1026. wait_realm->refresh();
  1027. // Start the server and wait for the Realm to be uploaded so that sync
  1028. // makes some writes to the Realm and bumps the version
  1029. server.start();
  1030. wait_for_upload(*r);
  1031. // Make sure that the notifications still get delivered rather than
  1032. // waiting forever due to that we don't get a commit notification from
  1033. // the commits sync makes to store the upload progress
  1034. r->refresh();
  1035. }
  1036. }
  1037. #endif
  1038. TEST_CASE("notifications: results") {
  1039. _impl::RealmCoordinator::assert_no_open_realms();
  1040. InMemoryTestFile config;
  1041. config.cache = false;
  1042. config.automatic_change_notifications = false;
  1043. auto r = Realm::get_shared_realm(config);
  1044. r->update_schema({
  1045. {"object", {
  1046. {"value", PropertyType::Int},
  1047. {"link", PropertyType::Object|PropertyType::Nullable, "linked to object"}
  1048. }},
  1049. {"other object", {
  1050. {"value", PropertyType::Int}
  1051. }},
  1052. {"linking object", {
  1053. {"link", PropertyType::Object|PropertyType::Nullable, "object"}
  1054. }},
  1055. {"linked to object", {
  1056. {"value", PropertyType::Int}
  1057. }}
  1058. });
  1059. auto coordinator = _impl::RealmCoordinator::get_existing_coordinator(config.path);
  1060. auto table = r->read_group().get_table("class_object");
  1061. r->begin_transaction();
  1062. r->read_group().get_table("class_linked to object")->add_empty_row(10);
  1063. table->add_empty_row(10);
  1064. for (int i = 0; i < 10; ++i) {
  1065. table->set_int(0, i, i * 2);
  1066. table->set_link(1, i, i);
  1067. }
  1068. r->commit_transaction();
  1069. auto r2 = coordinator->get_realm();
  1070. auto r2_table = r2->read_group().get_table("class_object");
  1071. Results results(r, table->where().greater(0, 0).less(0, 10));
  1072. SECTION("unsorted notifications") {
  1073. int notification_calls = 0;
  1074. CollectionChangeSet change;
  1075. auto token = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  1076. REQUIRE_FALSE(err);
  1077. change = c;
  1078. ++notification_calls;
  1079. });
  1080. advance_and_notify(*r);
  1081. auto write = [&](auto&& f) {
  1082. r->begin_transaction();
  1083. f();
  1084. r->commit_transaction();
  1085. advance_and_notify(*r);
  1086. };
  1087. SECTION("modifications to unrelated tables do not send notifications") {
  1088. write([&] {
  1089. r->read_group().get_table("class_other object")->add_empty_row();
  1090. });
  1091. REQUIRE(notification_calls == 1);
  1092. }
  1093. SECTION("irrelevant modifications to linked tables do not send notifications") {
  1094. write([&] {
  1095. r->read_group().get_table("class_linked to object")->add_empty_row();
  1096. });
  1097. REQUIRE(notification_calls == 1);
  1098. }
  1099. SECTION("irrelevant modifications to linking tables do not send notifications") {
  1100. write([&] {
  1101. r->read_group().get_table("class_linking object")->add_empty_row();
  1102. });
  1103. REQUIRE(notification_calls == 1);
  1104. }
  1105. SECTION("modifications that leave a non-matching row non-matching do not send notifications") {
  1106. write([&] {
  1107. table->set_int(0, 6, 13);
  1108. });
  1109. REQUIRE(notification_calls == 1);
  1110. }
  1111. SECTION("deleting non-matching rows does not send a notification") {
  1112. write([&] {
  1113. table->move_last_over(0);
  1114. table->move_last_over(6);
  1115. });
  1116. REQUIRE(notification_calls == 1);
  1117. }
  1118. SECTION("swapping adjacent matching and non-matching rows does not send notifications") {
  1119. write([&] {
  1120. table->swap_rows(0, 1);
  1121. });
  1122. REQUIRE(notification_calls == 1);
  1123. }
  1124. SECTION("swapping non-adjacent matching and non-matching rows send a single insert/delete pair") {
  1125. write([&] {
  1126. table->swap_rows(0, 2);
  1127. });
  1128. REQUIRE(notification_calls == 2);
  1129. REQUIRE_INDICES(change.deletions, 1);
  1130. REQUIRE_INDICES(change.insertions, 0);
  1131. }
  1132. SECTION("swapping matching rows sends insert/delete pairs") {
  1133. write([&] {
  1134. table->swap_rows(1, 4);
  1135. });
  1136. REQUIRE(notification_calls == 2);
  1137. REQUIRE_INDICES(change.deletions, 0, 3);
  1138. REQUIRE_INDICES(change.insertions, 0, 3);
  1139. write([&] {
  1140. table->swap_rows(1, 2);
  1141. table->swap_rows(2, 3);
  1142. table->swap_rows(3, 4);
  1143. });
  1144. REQUIRE(notification_calls == 3);
  1145. REQUIRE_INDICES(change.deletions, 1, 2, 3);
  1146. REQUIRE_INDICES(change.insertions, 0, 1, 2);
  1147. }
  1148. SECTION("swap does not inhibit move collapsing after removals") {
  1149. write([&] {
  1150. table->swap_rows(2, 3);
  1151. table->set_int(0, 3, 100);
  1152. });
  1153. REQUIRE(notification_calls == 2);
  1154. REQUIRE_INDICES(change.deletions, 1);
  1155. REQUIRE(change.insertions.empty());
  1156. }
  1157. SECTION("modifying a matching row and leaving it matching marks that row as modified") {
  1158. write([&] {
  1159. table->set_int(0, 1, 3);
  1160. });
  1161. REQUIRE(notification_calls == 2);
  1162. REQUIRE_INDICES(change.modifications, 0);
  1163. REQUIRE_INDICES(change.modifications_new, 0);
  1164. }
  1165. SECTION("modifying a matching row to no longer match marks that row as deleted") {
  1166. write([&] {
  1167. table->set_int(0, 2, 0);
  1168. });
  1169. REQUIRE(notification_calls == 2);
  1170. REQUIRE_INDICES(change.deletions, 1);
  1171. }
  1172. SECTION("modifying a non-matching row to match marks that row as inserted, but not modified") {
  1173. write([&] {
  1174. table->set_int(0, 7, 3);
  1175. });
  1176. REQUIRE(notification_calls == 2);
  1177. REQUIRE_INDICES(change.insertions, 4);
  1178. REQUIRE(change.modifications.empty());
  1179. REQUIRE(change.modifications_new.empty());
  1180. }
  1181. SECTION("deleting a matching row marks that row as deleted") {
  1182. write([&] {
  1183. table->move_last_over(3);
  1184. });
  1185. REQUIRE(notification_calls == 2);
  1186. REQUIRE_INDICES(change.deletions, 2);
  1187. }
  1188. SECTION("moving a matching row via deletion marks that row as moved") {
  1189. write([&] {
  1190. table->where().greater_equal(0, 10).find_all().clear(RemoveMode::unordered);
  1191. table->move_last_over(0);
  1192. });
  1193. REQUIRE(notification_calls == 2);
  1194. REQUIRE_MOVES(change, {3, 0});
  1195. }
  1196. SECTION("moving a matching row via subsumption marks that row as modified") {
  1197. write([&] {
  1198. table->where().greater_equal(0, 10).find_all().clear(RemoveMode::unordered);
  1199. table->move_last_over(0);
  1200. });
  1201. REQUIRE(notification_calls == 2);
  1202. REQUIRE_MOVES(change, {3, 0});
  1203. }
  1204. SECTION("modifications from multiple transactions are collapsed") {
  1205. r2->begin_transaction();
  1206. r2_table->set_int(0, 0, 6);
  1207. r2->commit_transaction();
  1208. coordinator->on_change();
  1209. r2->begin_transaction();
  1210. r2_table->set_int(0, 1, 0);
  1211. r2->commit_transaction();
  1212. REQUIRE(notification_calls == 1);
  1213. coordinator->on_change();
  1214. r->notify();
  1215. REQUIRE(notification_calls == 2);
  1216. }
  1217. SECTION("inserting a row then modifying it in a second transaction does not report it as modified") {
  1218. r2->begin_transaction();
  1219. size_t ndx = r2_table->add_empty_row();
  1220. r2_table->set_int(0, ndx, 6);
  1221. r2->commit_transaction();
  1222. coordinator->on_change();
  1223. r2->begin_transaction();
  1224. r2_table->set_int(0, ndx, 7);
  1225. r2->commit_transaction();
  1226. advance_and_notify(*r);
  1227. REQUIRE(notification_calls == 2);
  1228. REQUIRE_INDICES(change.insertions, 4);
  1229. REQUIRE(change.modifications.empty());
  1230. REQUIRE(change.modifications_new.empty());
  1231. }
  1232. SECTION("modification indices are pre-insert/delete") {
  1233. r->begin_transaction();
  1234. table->set_int(0, 2, 0);
  1235. table->set_int(0, 3, 6);
  1236. r->commit_transaction();
  1237. advance_and_notify(*r);
  1238. REQUIRE(notification_calls == 2);
  1239. REQUIRE_INDICES(change.deletions, 1);
  1240. REQUIRE_INDICES(change.modifications, 2);
  1241. REQUIRE_INDICES(change.modifications_new, 1);
  1242. }
  1243. SECTION("notifications are not delivered when collapsing transactions results in no net change") {
  1244. r2->begin_transaction();
  1245. size_t ndx = r2_table->add_empty_row();
  1246. r2_table->set_int(0, ndx, 5);
  1247. r2->commit_transaction();
  1248. coordinator->on_change();
  1249. r2->begin_transaction();
  1250. r2_table->move_last_over(ndx);
  1251. r2->commit_transaction();
  1252. REQUIRE(notification_calls == 1);
  1253. coordinator->on_change();
  1254. r->notify();
  1255. REQUIRE(notification_calls == 1);
  1256. }
  1257. SECTION("inserting a non-matching row at the beginning does not produce a notification") {
  1258. write([&] {
  1259. table->insert_empty_row(1);
  1260. });
  1261. REQUIRE(notification_calls == 1);
  1262. }
  1263. SECTION("inserting a matching row at the beginning marks just it as inserted") {
  1264. write([&] {
  1265. table->insert_empty_row(0);
  1266. table->set_int(0, 0, 5);
  1267. });
  1268. REQUIRE(notification_calls == 2);
  1269. REQUIRE_INDICES(change.insertions, 0);
  1270. }
  1271. }
  1272. SECTION("before/after change callback") {
  1273. struct Callback {
  1274. size_t before_calls = 0;
  1275. size_t after_calls = 0;
  1276. CollectionChangeSet before_change;
  1277. CollectionChangeSet after_change;
  1278. std::function<void(void)> on_before = []{};
  1279. std::function<void(void)> on_after = []{};
  1280. void before(CollectionChangeSet c) {
  1281. before_change = c;
  1282. ++before_calls;
  1283. on_before();
  1284. }
  1285. void after(CollectionChangeSet c) {
  1286. after_change = c;
  1287. ++after_calls;
  1288. on_after();
  1289. }
  1290. void error(std::exception_ptr) {
  1291. FAIL("error() should not be called");
  1292. }
  1293. } callback;
  1294. auto token = results.add_notification_callback(&callback);
  1295. advance_and_notify(*r);
  1296. SECTION("only after() is called for initial results") {
  1297. REQUIRE(callback.before_calls == 0);
  1298. REQUIRE(callback.after_calls == 1);
  1299. REQUIRE(callback.after_change.empty());
  1300. }
  1301. auto write = [&](auto&& func) {
  1302. r2->begin_transaction();
  1303. func(*r2_table);
  1304. r2->commit_transaction();
  1305. advance_and_notify(*r);
  1306. };
  1307. SECTION("both are called after a write") {
  1308. write([&](auto&& t) {
  1309. t.set_int(0, t.add_empty_row(), 5);
  1310. });
  1311. REQUIRE(callback.before_calls == 1);
  1312. REQUIRE(callback.after_calls == 2);
  1313. REQUIRE_INDICES(callback.before_change.insertions, 4);
  1314. REQUIRE_INDICES(callback.after_change.insertions, 4);
  1315. }
  1316. SECTION("deleted objects are usable in before()") {
  1317. callback.on_before = [&] {
  1318. REQUIRE(results.size() == 4);
  1319. REQUIRE_INDICES(callback.before_change.deletions, 0);
  1320. REQUIRE(results.get(0).is_attached());
  1321. REQUIRE(results.get(0).get_int(0) == 2);
  1322. };
  1323. write([&](auto&& t) {
  1324. t.move_last_over(results.get(0).get_index());
  1325. });
  1326. REQUIRE(callback.before_calls == 1);
  1327. REQUIRE(callback.after_calls == 2);
  1328. }
  1329. SECTION("inserted objects are usable in after()") {
  1330. callback.on_after = [&] {
  1331. REQUIRE(results.size() == 5);
  1332. REQUIRE_INDICES(callback.after_change.insertions, 4);
  1333. REQUIRE(results.last()->get_int(0) == 5);
  1334. };
  1335. write([&](auto&& t) {
  1336. t.set_int(0, t.add_empty_row(), 5);
  1337. });
  1338. REQUIRE(callback.before_calls == 1);
  1339. REQUIRE(callback.after_calls == 2);
  1340. }
  1341. }
  1342. SECTION("sorted notifications") {
  1343. // Sort in descending order
  1344. results = results.sort({*table, {{0}}, {false}});
  1345. int notification_calls = 0;
  1346. CollectionChangeSet change;
  1347. auto token = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  1348. REQUIRE_FALSE(err);
  1349. change = c;
  1350. ++notification_calls;
  1351. });
  1352. advance_and_notify(*r);
  1353. auto write = [&](auto&& f) {
  1354. r->begin_transaction();
  1355. f();
  1356. r->commit_transaction();
  1357. advance_and_notify(*r);
  1358. };
  1359. SECTION("swapping rows does not send notifications") {
  1360. write([&] {
  1361. table->swap_rows(2, 3);
  1362. });
  1363. REQUIRE(notification_calls == 1);
  1364. }
  1365. SECTION("modifications that leave a non-matching row non-matching do not send notifications") {
  1366. write([&] {
  1367. table->set_int(0, 6, 13);
  1368. });
  1369. REQUIRE(notification_calls == 1);
  1370. }
  1371. SECTION("deleting non-matching rows does not send a notification") {
  1372. write([&] {
  1373. table->move_last_over(0);
  1374. table->move_last_over(6);
  1375. });
  1376. REQUIRE(notification_calls == 1);
  1377. }
  1378. SECTION("modifying a matching row and leaving it matching marks that row as modified") {
  1379. write([&] {
  1380. table->set_int(0, 1, 3);
  1381. });
  1382. REQUIRE(notification_calls == 2);
  1383. REQUIRE_INDICES(change.modifications, 3);
  1384. REQUIRE_INDICES(change.modifications_new, 3);
  1385. }
  1386. SECTION("swapping leaves modified rows marked as modified") {
  1387. write([&] {
  1388. table->set_int(0, 1, 3);
  1389. table->swap_rows(1, 2);
  1390. });
  1391. REQUIRE(notification_calls == 2);
  1392. REQUIRE_INDICES(change.modifications, 3);
  1393. REQUIRE_INDICES(change.modifications_new, 3);
  1394. write([&] {
  1395. table->swap_rows(3, 1);
  1396. table->set_int(0, 1, 7);
  1397. });
  1398. REQUIRE(notification_calls == 3);
  1399. REQUIRE_INDICES(change.modifications, 1);
  1400. REQUIRE_INDICES(change.modifications_new, 1);
  1401. }
  1402. SECTION("modifying a matching row to no longer match marks that row as deleted") {
  1403. write([&] {
  1404. table->set_int(0, 2, 0);
  1405. });
  1406. REQUIRE(notification_calls == 2);
  1407. REQUIRE_INDICES(change.deletions, 2);
  1408. }
  1409. SECTION("modifying a non-matching row to match marks that row as inserted") {
  1410. write([&] {
  1411. table->set_int(0, 7, 3);
  1412. });
  1413. REQUIRE(notification_calls == 2);
  1414. REQUIRE_INDICES(change.insertions, 3);
  1415. }
  1416. SECTION("deleting a matching row marks that row as deleted") {
  1417. write([&] {
  1418. table->move_last_over(3);
  1419. });
  1420. REQUIRE(notification_calls == 2);
  1421. REQUIRE_INDICES(change.deletions, 1);
  1422. }
  1423. SECTION("moving a matching row via deletion does not send a notification") {
  1424. write([&] {
  1425. table->where().greater_equal(0, 10).find_all().clear(RemoveMode::unordered);
  1426. table->move_last_over(0);
  1427. });
  1428. REQUIRE(notification_calls == 1);
  1429. }
  1430. SECTION("modifying a matching row to change its position sends insert+delete") {
  1431. write([&] {
  1432. table->set_int(0, 2, 9);
  1433. });
  1434. REQUIRE(notification_calls == 2);
  1435. REQUIRE_INDICES(change.deletions, 2);
  1436. REQUIRE_INDICES(change.insertions, 0);
  1437. }
  1438. SECTION("modifications from multiple transactions are collapsed") {
  1439. r2->begin_transaction();
  1440. r2_table->set_int(0, 0, 5);
  1441. r2->commit_transaction();
  1442. r2->begin_transaction();
  1443. r2_table->set_int(0, 1, 0);
  1444. r2->commit_transaction();
  1445. REQUIRE(notification_calls == 1);
  1446. advance_and_notify(*r);
  1447. REQUIRE(notification_calls == 2);
  1448. }
  1449. SECTION("moving a matching row by deleting all other rows") {
  1450. r->begin_transaction();
  1451. table->clear();
  1452. table->add_empty_row(2);
  1453. table->set_int(0, 0, 15);
  1454. table->set_int(0, 1, 5);
  1455. r->commit_transaction();
  1456. advance_and_notify(*r);
  1457. write([&] {
  1458. table->move_last_over(0);
  1459. table->add_empty_row();
  1460. table->set_int(0, 1, 3);
  1461. });
  1462. REQUIRE(notification_calls == 3);
  1463. REQUIRE(change.deletions.empty());
  1464. REQUIRE_INDICES(change.insertions, 1);
  1465. }
  1466. }
  1467. SECTION("distinct notifications") {
  1468. results = results.distinct(SortDescriptor(*table, {{0}}));
  1469. int notification_calls = 0;
  1470. CollectionChangeSet change;
  1471. auto token = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  1472. REQUIRE_FALSE(err);
  1473. change = c;
  1474. ++notification_calls;
  1475. });
  1476. advance_and_notify(*r);
  1477. auto write = [&](auto&& f) {
  1478. r->begin_transaction();
  1479. f();
  1480. r->commit_transaction();
  1481. advance_and_notify(*r);
  1482. };
  1483. SECTION("modifications that leave a non-matching row non-matching do not send notifications") {
  1484. write([&] {
  1485. table->set_int(0, 6, 13);
  1486. });
  1487. REQUIRE(notification_calls == 1);
  1488. }
  1489. SECTION("deleting non-matching rows does not send a notification") {
  1490. write([&] {
  1491. table->move_last_over(0);
  1492. table->move_last_over(6);
  1493. });
  1494. REQUIRE(notification_calls == 1);
  1495. }
  1496. SECTION("modifying a matching row and leaving it matching marks that row as modified") {
  1497. write([&] {
  1498. table->set_int(0, 1, 3);
  1499. });
  1500. REQUIRE(notification_calls == 2);
  1501. REQUIRE_INDICES(change.modifications, 0);
  1502. REQUIRE_INDICES(change.modifications_new, 0);
  1503. }
  1504. SECTION("modifying a non-matching row which is after the distinct results in the table to be a same value \
  1505. in the distinct results doesn't send notification.") {
  1506. write([&] {
  1507. table->set_int(0, 6, 2);
  1508. });
  1509. REQUIRE(notification_calls == 1);
  1510. }
  1511. SECTION("modifying a non-matching row which is before the distinct results in the table to be a same value \
  1512. in the distinct results send insert + delete.") {
  1513. write([&] {
  1514. table->set_int(0, 0, 2);
  1515. });
  1516. REQUIRE(notification_calls == 2);
  1517. REQUIRE_INDICES(change.deletions, 0);
  1518. REQUIRE_INDICES(change.insertions, 0);
  1519. }
  1520. SECTION("modifying a matching row to duplicated value in distinct results marks that row as deleted") {
  1521. write([&] {
  1522. table->set_int(0, 2, 2);
  1523. });
  1524. REQUIRE(notification_calls == 2);
  1525. REQUIRE_INDICES(change.deletions, 1);
  1526. }
  1527. SECTION("modifying a non-matching row to match and different value marks that row as inserted") {
  1528. write([&] {
  1529. table->set_int(0, 0, 1);
  1530. });
  1531. REQUIRE(notification_calls == 2);
  1532. REQUIRE_INDICES(change.insertions, 0);
  1533. }
  1534. }
  1535. SECTION("schema changes") {
  1536. CollectionChangeSet change;
  1537. auto token = results.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  1538. REQUIRE_FALSE(err);
  1539. change = c;
  1540. });
  1541. advance_and_notify(*r);
  1542. auto write = [&](auto&& f) {
  1543. r->begin_transaction();
  1544. f();
  1545. r->commit_transaction();
  1546. advance_and_notify(*r);
  1547. };
  1548. SECTION("insert table before observed table") {
  1549. write([&] {
  1550. size_t row = table->add_empty_row();
  1551. table->set_int(0, row, 5);
  1552. r->read_group().insert_table(0, "new table");
  1553. table->insert_empty_row(0);
  1554. table->set_int(0, 0, 5);
  1555. });
  1556. REQUIRE_INDICES(change.insertions, 0, 5);
  1557. }
  1558. auto linked_table = table->get_link_target(1);
  1559. SECTION("insert new column before link column") {
  1560. write([&] {
  1561. linked_table->set_int(0, 1, 5);
  1562. table->insert_column(0, type_Int, "new col");
  1563. linked_table->set_int(0, 2, 5);
  1564. });
  1565. REQUIRE_INDICES(change.modifications, 0, 1);
  1566. }
  1567. SECTION("insert table before link target") {
  1568. write([&] {
  1569. linked_table->set_int(0, 1, 5);
  1570. r->read_group().insert_table(0, "new table");
  1571. linked_table->set_int(0, 2, 5);
  1572. });
  1573. REQUIRE_INDICES(change.modifications, 0, 1);
  1574. }
  1575. }
  1576. }
  1577. TEST_CASE("results: notifications after move") {
  1578. InMemoryTestFile config;
  1579. config.cache = false;
  1580. config.automatic_change_notifications = false;
  1581. auto r = Realm::get_shared_realm(config);
  1582. r->update_schema({
  1583. {"object", {
  1584. {"value", PropertyType::Int},
  1585. }},
  1586. });
  1587. auto table = r->read_group().get_table("class_object");
  1588. auto results = std::make_unique<Results>(r, *table);
  1589. int notification_calls = 0;
  1590. auto token = results->add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  1591. REQUIRE_FALSE(err);
  1592. ++notification_calls;
  1593. });
  1594. advance_and_notify(*r);
  1595. auto write = [&](auto&& f) {
  1596. r->begin_transaction();
  1597. f();
  1598. r->commit_transaction();
  1599. advance_and_notify(*r);
  1600. };
  1601. SECTION("notifications continue to work after Results is moved (move-constructor)") {
  1602. Results r(std::move(*results));
  1603. results.reset();
  1604. write([&] {
  1605. table->set_int(0, table->add_empty_row(), 1);
  1606. });
  1607. REQUIRE(notification_calls == 2);
  1608. }
  1609. SECTION("notifications continue to work after Results is moved (move-assignment)") {
  1610. Results r;
  1611. r = std::move(*results);
  1612. results.reset();
  1613. write([&] {
  1614. table->set_int(0, table->add_empty_row(), 1);
  1615. });
  1616. REQUIRE(notification_calls == 2);
  1617. }
  1618. }
  1619. TEST_CASE("results: notifier with no callbacks") {
  1620. _impl::RealmCoordinator::assert_no_open_realms();
  1621. InMemoryTestFile config;
  1622. config.cache = false;
  1623. config.automatic_change_notifications = false;
  1624. auto coordinator = _impl::RealmCoordinator::get_coordinator(config.path);
  1625. auto r = coordinator->get_realm(std::move(config));
  1626. r->update_schema({
  1627. {"object", {
  1628. {"value", PropertyType::Int},
  1629. }},
  1630. });
  1631. auto table = r->read_group().get_table("class_object");
  1632. Results results(r, table->where());
  1633. results.last(); // force evaluation and creation of TableView
  1634. SECTION("refresh() does not block due to implicit notifier") {
  1635. // Create and then immediately remove a callback because
  1636. // `automatic_change_notifications = false` makes Results not implicitly
  1637. // create a notifier
  1638. results.add_notification_callback([](CollectionChangeSet const&, std::exception_ptr) {});
  1639. auto r2 = coordinator->get_realm();
  1640. r2->begin_transaction();
  1641. r2->read_group().get_table("class_object")->add_empty_row();
  1642. r2->commit_transaction();
  1643. r->refresh(); // would deadlock if there was a callback
  1644. }
  1645. SECTION("refresh() does not attempt to deliver stale results") {
  1646. results.add_notification_callback([](CollectionChangeSet const&, std::exception_ptr) {});
  1647. // Create version 1
  1648. r->begin_transaction();
  1649. table->add_empty_row();
  1650. r->commit_transaction();
  1651. r->begin_transaction();
  1652. // Run async query for version 1
  1653. coordinator->on_change();
  1654. // Create version 2 without ever letting 1 be delivered
  1655. table->add_empty_row();
  1656. r->commit_transaction();
  1657. // Give it a chance to deliver the async query results (and fail, becuse
  1658. // they're for version 1 and the realm is at 2)
  1659. r->refresh();
  1660. }
  1661. SECTION("should not pin the source version even after the Realm has been closed") {
  1662. auto r2 = coordinator->get_realm();
  1663. REQUIRE(r != r2);
  1664. r->close();
  1665. auto& shared_group = TestHelper::get_shared_group(r2);
  1666. // There's always at least 2 live versions because the previous version
  1667. // isn't clean up until the *next* commit
  1668. REQUIRE(shared_group.get_number_of_versions() == 2);
  1669. auto table = r2->read_group().get_table("class_object");
  1670. r2->begin_transaction();
  1671. table->add_empty_row();
  1672. r2->commit_transaction();
  1673. r2->begin_transaction();
  1674. table->add_empty_row();
  1675. r2->commit_transaction();
  1676. // Would now be 3 if the closed Realm is still pinning the version it was at
  1677. REQUIRE(shared_group.get_number_of_versions() == 2);
  1678. }
  1679. }
  1680. TEST_CASE("results: error messages") {
  1681. InMemoryTestFile config;
  1682. config.schema = Schema{
  1683. {"object", {
  1684. {"value", PropertyType::String},
  1685. }},
  1686. };
  1687. auto r = Realm::get_shared_realm(config);
  1688. auto table = r->read_group().get_table("class_object");
  1689. Results results(r, *table);
  1690. r->begin_transaction();
  1691. table->add_empty_row();
  1692. r->commit_transaction();
  1693. SECTION("out of bounds access") {
  1694. REQUIRE_THROWS_WITH(results.get(5), "Requested index 5 greater than max 0");
  1695. }
  1696. SECTION("unsupported aggregate operation") {
  1697. REQUIRE_THROWS_WITH(results.sum(0), "Cannot sum property 'value': operation not supported for 'string' properties");
  1698. }
  1699. }
  1700. TEST_CASE("results: snapshots") {
  1701. InMemoryTestFile config;
  1702. config.cache = false;
  1703. config.automatic_change_notifications = false;
  1704. config.schema = Schema{
  1705. {"object", {
  1706. {"value", PropertyType::Int},
  1707. {"array", PropertyType::Array|PropertyType::Object, "linked to object"}
  1708. }},
  1709. {"linked to object", {
  1710. {"value", PropertyType::Int}
  1711. }}
  1712. };
  1713. auto r = Realm::get_shared_realm(config);
  1714. auto write = [&](auto&& f) {
  1715. r->begin_transaction();
  1716. f();
  1717. r->commit_transaction();
  1718. advance_and_notify(*r);
  1719. };
  1720. SECTION("snapshot of empty Results") {
  1721. Results results;
  1722. auto snapshot = results.snapshot();
  1723. REQUIRE(snapshot.size() == 0);
  1724. }
  1725. SECTION("snapshot of Results based on Table") {
  1726. auto table = r->read_group().get_table("class_object");
  1727. Results results(r, *table);
  1728. {
  1729. // A newly-added row should not appear in the snapshot.
  1730. auto snapshot = results.snapshot();
  1731. REQUIRE(results.size() == 0);
  1732. REQUIRE(snapshot.size() == 0);
  1733. write([=]{
  1734. table->add_empty_row();
  1735. });
  1736. REQUIRE(results.size() == 1);
  1737. REQUIRE(snapshot.size() == 0);
  1738. }
  1739. {
  1740. // Removing a row present in the snapshot should not affect the size of the snapshot,
  1741. // but will result in the snapshot returning a detached row accessor.
  1742. auto snapshot = results.snapshot();
  1743. REQUIRE(results.size() == 1);
  1744. REQUIRE(snapshot.size() == 1);
  1745. write([=]{
  1746. table->move_last_over(0);
  1747. });
  1748. REQUIRE(results.size() == 0);
  1749. REQUIRE(snapshot.size() == 1);
  1750. REQUIRE(!snapshot.get(0).is_attached());
  1751. // Adding a row at the same index that was formerly present in the snapshot shouldn't
  1752. // affect the state of the snapshot.
  1753. write([=]{
  1754. table->add_empty_row();
  1755. });
  1756. REQUIRE(snapshot.size() == 1);
  1757. REQUIRE(!snapshot.get(0).is_attached());
  1758. }
  1759. }
  1760. SECTION("snapshot of Results based on LinkView") {
  1761. auto object = r->read_group().get_table("class_object");
  1762. auto linked_to = r->read_group().get_table("class_linked to object");
  1763. write([=]{
  1764. object->add_empty_row();
  1765. });
  1766. LinkViewRef lv = object->get_linklist(1, 0);
  1767. Results results(r, lv);
  1768. {
  1769. // A newly-added row should not appear in the snapshot.
  1770. auto snapshot = results.snapshot();
  1771. REQUIRE(results.size() == 0);
  1772. REQUIRE(snapshot.size() == 0);
  1773. write([=]{
  1774. lv->add(linked_to->add_empty_row());
  1775. });
  1776. REQUIRE(results.size() == 1);
  1777. REQUIRE(snapshot.size() == 0);
  1778. }
  1779. {
  1780. // Removing a row from the link list should not affect the snapshot.
  1781. auto snapshot = results.snapshot();
  1782. REQUIRE(results.size() == 1);
  1783. REQUIRE(snapshot.size() == 1);
  1784. write([=]{
  1785. lv->remove(0);
  1786. });
  1787. REQUIRE(results.size() == 0);
  1788. REQUIRE(snapshot.size() == 1);
  1789. REQUIRE(snapshot.get(0).is_attached());
  1790. // Removing a row present in the snapshot from its table should result in the snapshot
  1791. // returning a detached row accessor.
  1792. write([=]{
  1793. linked_to->remove(0);
  1794. });
  1795. REQUIRE(snapshot.size() == 1);
  1796. REQUIRE(!snapshot.get(0).is_attached());
  1797. // Adding a new row to the link list shouldn't affect the state of the snapshot.
  1798. write([=]{
  1799. lv->add(linked_to->add_empty_row());
  1800. });
  1801. REQUIRE(snapshot.size() == 1);
  1802. REQUIRE(!snapshot.get(0).is_attached());
  1803. }
  1804. }
  1805. SECTION("snapshot of Results based on Query") {
  1806. auto table = r->read_group().get_table("class_object");
  1807. Query q = table->column<Int>(0) > 0;
  1808. Results results(r, std::move(q));
  1809. {
  1810. // A newly-added row should not appear in the snapshot.
  1811. auto snapshot = results.snapshot();
  1812. REQUIRE(results.size() == 0);
  1813. REQUIRE(snapshot.size() == 0);
  1814. write([=]{
  1815. table->set_int(0, table->add_empty_row(), 1);
  1816. });
  1817. REQUIRE(results.size() == 1);
  1818. REQUIRE(snapshot.size() == 0);
  1819. }
  1820. {
  1821. // Updating a row to no longer match the query criteria should not affect the snapshot.
  1822. auto snapshot = results.snapshot();
  1823. REQUIRE(results.size() == 1);
  1824. REQUIRE(snapshot.size() == 1);
  1825. write([=]{
  1826. table->set_int(0, 0, 0);
  1827. });
  1828. REQUIRE(results.size() == 0);
  1829. REQUIRE(snapshot.size() == 1);
  1830. REQUIRE(snapshot.get(0).is_attached());
  1831. // Removing a row present in the snapshot from its table should result in the snapshot
  1832. // returning a detached row accessor.
  1833. write([=]{
  1834. table->remove(0);
  1835. });
  1836. REQUIRE(snapshot.size() == 1);
  1837. REQUIRE(!snapshot.get(0).is_attached());
  1838. // Adding a new row that matches the query criteria shouldn't affect the state of the snapshot.
  1839. write([=]{
  1840. table->set_int(0, table->add_empty_row(), 1);
  1841. });
  1842. REQUIRE(snapshot.size() == 1);
  1843. REQUIRE(!snapshot.get(0).is_attached());
  1844. }
  1845. }
  1846. SECTION("snapshot of Results based on TableView from query") {
  1847. auto table = r->read_group().get_table("class_object");
  1848. Query q = table->column<Int>(0) > 0;
  1849. Results results(r, q.find_all());
  1850. {
  1851. // A newly-added row should not appear in the snapshot.
  1852. auto snapshot = results.snapshot();
  1853. REQUIRE(results.size() == 0);
  1854. REQUIRE(snapshot.size() == 0);
  1855. write([=]{
  1856. table->set_int(0, table->add_empty_row(), 1);
  1857. });
  1858. REQUIRE(results.size() == 1);
  1859. REQUIRE(snapshot.size() == 0);
  1860. }
  1861. {
  1862. // Updating a row to no longer match the query criteria should not affect the snapshot.
  1863. auto snapshot = results.snapshot();
  1864. REQUIRE(results.size() == 1);
  1865. REQUIRE(snapshot.size() == 1);
  1866. write([=]{
  1867. table->set_int(0, 0, 0);
  1868. });
  1869. REQUIRE(results.size() == 0);
  1870. REQUIRE(snapshot.size() == 1);
  1871. REQUIRE(snapshot.get(0).is_attached());
  1872. // Removing a row present in the snapshot from its table should result in the snapshot
  1873. // returning a detached row accessor.
  1874. write([=]{
  1875. table->remove(0);
  1876. });
  1877. REQUIRE(snapshot.size() == 1);
  1878. REQUIRE(!snapshot.get(0).is_attached());
  1879. // Adding a new row that matches the query criteria shouldn't affect the state of the snapshot.
  1880. write([=]{
  1881. table->set_int(0, table->add_empty_row(), 1);
  1882. });
  1883. REQUIRE(snapshot.size() == 1);
  1884. REQUIRE(!snapshot.get(0).is_attached());
  1885. }
  1886. }
  1887. SECTION("snapshot of Results based on TableView from backlinks") {
  1888. auto object = r->read_group().get_table("class_object");
  1889. auto linked_to = r->read_group().get_table("class_linked to object");
  1890. write([=]{
  1891. linked_to->add_empty_row();
  1892. });
  1893. TableView backlinks = linked_to->get_backlink_view(0, object.get(), 1);
  1894. Results results(r, std::move(backlinks));
  1895. auto lv = object->get_linklist(1, object->add_empty_row());
  1896. {
  1897. // A newly-added row should not appear in the snapshot.
  1898. auto snapshot = results.snapshot();
  1899. REQUIRE(results.size() == 0);
  1900. REQUIRE(snapshot.size() == 0);
  1901. write([=]{
  1902. lv->add(0);
  1903. });
  1904. REQUIRE(results.size() == 1);
  1905. REQUIRE(snapshot.size() == 0);
  1906. }
  1907. {
  1908. // Removing the link should not affect the snapshot.
  1909. auto snapshot = results.snapshot();
  1910. REQUIRE(results.size() == 1);
  1911. REQUIRE(snapshot.size() == 1);
  1912. write([=]{
  1913. lv->remove(0);
  1914. });
  1915. REQUIRE(results.size() == 0);
  1916. REQUIRE(snapshot.size() == 1);
  1917. REQUIRE(snapshot.get(0).is_attached());
  1918. // Removing a row present in the snapshot from its table should result in the snapshot
  1919. // returning a detached row accessor.
  1920. write([=]{
  1921. object->remove(0);
  1922. });
  1923. REQUIRE(snapshot.size() == 1);
  1924. REQUIRE(!snapshot.get(0).is_attached());
  1925. // Adding a new link shouldn't affect the state of the snapshot.
  1926. write([=]{
  1927. object->add_empty_row();
  1928. auto lv = object->get_linklist(1, object->add_empty_row());
  1929. lv->add(0);
  1930. });
  1931. REQUIRE(snapshot.size() == 1);
  1932. REQUIRE(!snapshot.get(0).is_attached());
  1933. }
  1934. }
  1935. SECTION("snapshot of Results with notification callback registered") {
  1936. auto table = r->read_group().get_table("class_object");
  1937. Query q = table->column<Int>(0) > 0;
  1938. Results results(r, q.find_all());
  1939. auto token = results.add_notification_callback([&](CollectionChangeSet, std::exception_ptr err) {
  1940. REQUIRE_FALSE(err);
  1941. });
  1942. advance_and_notify(*r);
  1943. SECTION("snapshot of lvalue") {
  1944. auto snapshot = results.snapshot();
  1945. write([=] {
  1946. table->set_int(0, table->add_empty_row(), 1);
  1947. });
  1948. REQUIRE(snapshot.size() == 0);
  1949. }
  1950. SECTION("snapshot of rvalue") {
  1951. auto snapshot = std::move(results).snapshot();
  1952. write([=] {
  1953. table->set_int(0, table->add_empty_row(), 1);
  1954. });
  1955. REQUIRE(snapshot.size() == 0);
  1956. }
  1957. }
  1958. SECTION("adding notification callback to snapshot throws") {
  1959. auto table = r->read_group().get_table("class_object");
  1960. Query q = table->column<Int>(0) > 0;
  1961. Results results(r, q.find_all());
  1962. auto snapshot = results.snapshot();
  1963. CHECK_THROWS(snapshot.add_notification_callback([](CollectionChangeSet, std::exception_ptr) {}));
  1964. }
  1965. SECTION("accessors should return none for detached row") {
  1966. auto table = r->read_group().get_table("class_object");
  1967. write([=] {
  1968. table->add_empty_row();
  1969. });
  1970. Results results(r, *table);
  1971. auto snapshot = results.snapshot();
  1972. write([=] {;
  1973. table->clear();
  1974. });
  1975. REQUIRE_FALSE(snapshot.get(0).is_attached());
  1976. REQUIRE_FALSE(snapshot.first()->is_attached());
  1977. REQUIRE_FALSE(snapshot.last()->is_attached());
  1978. }
  1979. }
  1980. TEST_CASE("results: distinct") {
  1981. const int N = 10;
  1982. InMemoryTestFile config;
  1983. config.cache = false;
  1984. config.automatic_change_notifications = false;
  1985. auto r = Realm::get_shared_realm(config);
  1986. r->update_schema({
  1987. {"object", {
  1988. {"num1", PropertyType::Int},
  1989. {"string", PropertyType::String},
  1990. {"num2", PropertyType::Int},
  1991. {"num3", PropertyType::Int}
  1992. }},
  1993. });
  1994. auto table = r->read_group().get_table("class_object");
  1995. r->begin_transaction();
  1996. table->add_empty_row(N);
  1997. for (int i = 0; i < N; ++i) {
  1998. table->set_int(0, i, i % 3);
  1999. table->set_string(1, i, util::format("Foo_%1", i % 3).c_str());
  2000. table->set_int(2, i, N - i);
  2001. table->set_int(3, i, i % 2);
  2002. }
  2003. // table:
  2004. // 0, Foo_0, 10, 0
  2005. // 1, Foo_1, 9, 1
  2006. // 2, Foo_2, 8, 0
  2007. // 0, Foo_0, 7, 1
  2008. // 1, Foo_1, 6, 0
  2009. // 2, Foo_2, 5, 1
  2010. // 0, Foo_0, 4, 0
  2011. // 1, Foo_1, 3, 1
  2012. // 2, Foo_2, 2, 0
  2013. // 0, Foo_0, 1, 1
  2014. r->commit_transaction();
  2015. Results results(r, table->where());
  2016. SECTION("Single integer property") {
  2017. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2018. // unique:
  2019. // 0, Foo_0, 10
  2020. // 1, Foo_1, 9
  2021. // 2, Foo_2, 8
  2022. REQUIRE(unique.size() == 3);
  2023. REQUIRE(unique.get(0).get_int(2) == 10);
  2024. REQUIRE(unique.get(1).get_int(2) == 9);
  2025. REQUIRE(unique.get(2).get_int(2) == 8);
  2026. }
  2027. SECTION("Single integer via apply_ordering") {
  2028. DescriptorOrdering ordering;
  2029. ordering.append_sort(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2030. ordering.append_distinct(DistinctDescriptor(results.get_tableview().get_parent(), {{0}}));
  2031. Results unique = results.apply_ordering(std::move(ordering));
  2032. // unique:
  2033. // 0, Foo_0, 10
  2034. // 1, Foo_1, 9
  2035. // 2, Foo_2, 8
  2036. REQUIRE(unique.size() == 3);
  2037. REQUIRE(unique.get(0).get_int(2) == 10);
  2038. REQUIRE(unique.get(1).get_int(2) == 9);
  2039. REQUIRE(unique.get(2).get_int(2) == 8);
  2040. }
  2041. SECTION("Single string property") {
  2042. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{1}}));
  2043. // unique:
  2044. // 0, Foo_0, 10
  2045. // 1, Foo_1, 9
  2046. // 2, Foo_2, 8
  2047. REQUIRE(unique.size() == 3);
  2048. REQUIRE(unique.get(0).get_int(2) == 10);
  2049. REQUIRE(unique.get(1).get_int(2) == 9);
  2050. REQUIRE(unique.get(2).get_int(2) == 8);
  2051. }
  2052. SECTION("Two integer properties combined") {
  2053. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{0}, {2}}));
  2054. // unique is the same as the table
  2055. REQUIRE(unique.size() == N);
  2056. for (int i = 0; i < N; ++i) {
  2057. REQUIRE(unique.get(i).get_string(1) == StringData(util::format("Foo_%1", i % 3).c_str()));
  2058. }
  2059. }
  2060. SECTION("String and integer combined") {
  2061. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{2}, {1}}));
  2062. // unique is the same as the table
  2063. REQUIRE(unique.size() == N);
  2064. for (int i = 0; i < N; ++i) {
  2065. REQUIRE(unique.get(i).get_string(1) == StringData(util::format("Foo_%1", i % 3).c_str()));
  2066. }
  2067. }
  2068. // This section and next section demonstrate that sort().distinct() != distinct().sort()
  2069. SECTION("Order after sort and distinct") {
  2070. Results reverse = results.sort(SortDescriptor(results.get_tableview().get_parent(), {{2}}, {true}));
  2071. // reverse:
  2072. // 0, Foo_0, 1
  2073. // ...
  2074. // 0, Foo_0, 10
  2075. REQUIRE(reverse.first()->get_int(2) == 1);
  2076. REQUIRE(reverse.last()->get_int(2) == 10);
  2077. // distinct() will be applied to the table, after sorting
  2078. Results unique = reverse.distinct(SortDescriptor(reverse.get_tableview().get_parent(), {{0}}));
  2079. // unique:
  2080. // 0, Foo_0, 1
  2081. // 2, Foo_2, 2
  2082. // 1, Foo_1, 3
  2083. REQUIRE(unique.size() == 3);
  2084. REQUIRE(unique.get(0).get_int(2) == 1);
  2085. REQUIRE(unique.get(1).get_int(2) == 2);
  2086. REQUIRE(unique.get(2).get_int(2) == 3);
  2087. }
  2088. SECTION("Order after distinct and sort") {
  2089. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2090. // unique:
  2091. // 0, Foo_0, 10
  2092. // 1, Foo_1, 9
  2093. // 2, Foo_2, 8
  2094. REQUIRE(unique.size() == 3);
  2095. REQUIRE(unique.first()->get_int(2) == 10);
  2096. REQUIRE(unique.last()->get_int(2) == 8);
  2097. // sort() is only applied to unique
  2098. Results reverse = unique.sort(SortDescriptor(unique.get_tableview().get_parent(), {{2}}, {true}));
  2099. // reversed:
  2100. // 2, Foo_2, 8
  2101. // 1, Foo_1, 9
  2102. // 0, Foo_0, 10
  2103. REQUIRE(reverse.size() == 3);
  2104. REQUIRE(reverse.get(0).get_int(2) == 8);
  2105. REQUIRE(reverse.get(1).get_int(2) == 9);
  2106. REQUIRE(reverse.get(2).get_int(2) == 10);
  2107. }
  2108. SECTION("Chaining distinct") {
  2109. Results first = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2110. REQUIRE(first.size() == 3);
  2111. // distinct() will not discard the previous applied distinct() calls
  2112. Results second = first.distinct(SortDescriptor(first.get_tableview().get_parent(), {{3}}));
  2113. REQUIRE(second.size() == 2);
  2114. }
  2115. SECTION("Chaining sort") {
  2116. using cols_0_3 = std::pair<size_t, size_t>;
  2117. Results first = results.sort(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2118. Results second = first.sort(SortDescriptor(first.get_tableview().get_parent(), {{3}}));
  2119. REQUIRE(second.size() == 10);
  2120. // results are ordered first by the last sorted column
  2121. // if any duplicates exist in that column, they are resolved by sorting the
  2122. // previously sorted column. Eg. sort(a).sort(b) == sort(b, a)
  2123. std::vector<cols_0_3> results
  2124. = {{0, 0}, {0, 0}, {1, 0}, {2, 0}, {2, 0}, {0, 1}, {0, 1}, {1, 1}, {1, 1}, {2, 1}};
  2125. for (size_t i = 0; i < results.size(); ++i) {
  2126. REQUIRE(second.get(i).get_int(0) == results[i].first);
  2127. REQUIRE(second.get(i).get_int(3) == results[i].second);
  2128. }
  2129. }
  2130. SECTION("Distinct is carried over to new queries") {
  2131. Results unique = results.distinct(SortDescriptor(results.get_tableview().get_parent(), {{0}}));
  2132. // unique:
  2133. // 0, Foo_0, 10
  2134. // 1, Foo_1, 9
  2135. // 2, Foo_2, 8
  2136. REQUIRE(unique.size() == 3);
  2137. Results filtered = unique.filter(Query(table->where().less(0, 2)));
  2138. // filtered:
  2139. // 0, Foo_0, 10
  2140. // 1, Foo_1, 9
  2141. REQUIRE(filtered.size() == 2);
  2142. REQUIRE(filtered.get(0).get_int(2) == 10);
  2143. REQUIRE(filtered.get(1).get_int(2) == 9);
  2144. }
  2145. SECTION("Distinct will not forget previous query") {
  2146. Results filtered = results.filter(Query(table->where().greater(2, 5)));
  2147. // filtered:
  2148. // 0, Foo_0, 10
  2149. // 1, Foo_1, 9
  2150. // 2, Foo_2, 8
  2151. // 0, Foo_0, 7
  2152. // 1, Foo_1, 6
  2153. REQUIRE(filtered.size() == 5);
  2154. Results unique = filtered.distinct(SortDescriptor(filtered.get_tableview().get_parent(), {{0}}));
  2155. // unique:
  2156. // 0, Foo_0, 10
  2157. // 1, Foo_1, 9
  2158. // 2, Foo_2, 8
  2159. REQUIRE(unique.size() == 3);
  2160. REQUIRE(unique.get(0).get_int(2) == 10);
  2161. REQUIRE(unique.get(1).get_int(2) == 9);
  2162. REQUIRE(unique.get(2).get_int(2) == 8);
  2163. Results further_filtered = unique.filter(Query(table->where().equal(2, 9)));
  2164. // further_filtered:
  2165. // 1, Foo_1, 9
  2166. REQUIRE(further_filtered.size() == 1);
  2167. REQUIRE(further_filtered.get(0).get_int(2) == 9);
  2168. }
  2169. }
  2170. TEST_CASE("results: sort") {
  2171. InMemoryTestFile config;
  2172. config.cache = false;
  2173. config.schema = Schema{
  2174. {"object", {
  2175. {"value", PropertyType::Int},
  2176. {"bool", PropertyType::Bool},
  2177. {"data prop", PropertyType::Data},
  2178. {"link", PropertyType::Object|PropertyType::Nullable, "object 2"},
  2179. {"array", PropertyType::Object|PropertyType::Array, "object 2"},
  2180. }},
  2181. {"object 2", {
  2182. {"value", PropertyType::Int},
  2183. {"link", PropertyType::Object|PropertyType::Nullable, "object"},
  2184. }},
  2185. };
  2186. auto realm = Realm::get_shared_realm(config);
  2187. auto table = realm->read_group().get_table("class_object");
  2188. auto table2 = realm->read_group().get_table("class_object 2");
  2189. Results r(realm, *table);
  2190. SECTION("invalid keypaths") {
  2191. SECTION("empty property name") {
  2192. REQUIRE_THROWS_WITH(r.sort({{"", true}}), "Cannot sort on key path '': missing property name.");
  2193. REQUIRE_THROWS_WITH(r.sort({{".", true}}), "Cannot sort on key path '.': missing property name.");
  2194. REQUIRE_THROWS_WITH(r.sort({{"link.", true}}), "Cannot sort on key path 'link.': missing property name.");
  2195. REQUIRE_THROWS_WITH(r.sort({{".value", true}}), "Cannot sort on key path '.value': missing property name.");
  2196. REQUIRE_THROWS_WITH(r.sort({{"link..value", true}}), "Cannot sort on key path 'link..value': missing property name.");
  2197. }
  2198. SECTION("bad property name") {
  2199. REQUIRE_THROWS_WITH(r.sort({{"not a property", true}}),
  2200. "Cannot sort on key path 'not a property': property 'object.not a property' does not exist.");
  2201. REQUIRE_THROWS_WITH(r.sort({{"link.not a property", true}}),
  2202. "Cannot sort on key path 'link.not a property': property 'object 2.not a property' does not exist.");
  2203. }
  2204. SECTION("subscript primitive") {
  2205. REQUIRE_THROWS_WITH(r.sort({{"value.link", true}}),
  2206. "Cannot sort on key path 'value.link': property 'object.value' of type 'int' may only be the final property in the key path.");
  2207. }
  2208. SECTION("end in link") {
  2209. REQUIRE_THROWS_WITH(r.sort({{"link", true}}),
  2210. "Cannot sort on key path 'link': property 'object.link' of type 'object' cannot be the final property in the key path.");
  2211. REQUIRE_THROWS_WITH(r.sort({{"link.link", true}}),
  2212. "Cannot sort on key path 'link.link': property 'object 2.link' of type 'object' cannot be the final property in the key path.");
  2213. }
  2214. SECTION("sort involving bad property types") {
  2215. REQUIRE_THROWS_WITH(r.sort({{"array", true}}),
  2216. "Cannot sort on key path 'array': property 'object.array' is of unsupported type 'array'.");
  2217. REQUIRE_THROWS_WITH(r.sort({{"array.value", true}}),
  2218. "Cannot sort on key path 'array.value': property 'object.array' is of unsupported type 'array'.");
  2219. REQUIRE_THROWS_WITH(r.sort({{"link.link.array.value", true}}),
  2220. "Cannot sort on key path 'link.link.array.value': property 'object.array' is of unsupported type 'array'.");
  2221. REQUIRE_THROWS_WITH(r.sort({{"data prop", true}}),
  2222. "Cannot sort on key path 'data prop': property 'object.data prop' is of unsupported type 'data'.");
  2223. }
  2224. }
  2225. realm->begin_transaction();
  2226. table->add_empty_row(4);
  2227. table2->add_empty_row(4);
  2228. for (int i = 0; i < 4; ++i) {
  2229. table->set_int(0, i, (i + 2) % 4);
  2230. table->set_bool(1, i, i % 2);
  2231. table->set_link(3, i, 3 - i);
  2232. table2->set_int(0, i, (i + 1) % 4);
  2233. table2->set_link(1, i, i);
  2234. }
  2235. realm->commit_transaction();
  2236. /*
  2237. | index | value | bool | link.value | link.link.value |
  2238. |-------|-------|------|------------|-----------------|
  2239. | 0 | 2 | 0 | 0 | 1 |
  2240. | 1 | 3 | 1 | 3 | 0 |
  2241. | 2 | 0 | 0 | 2 | 3 |
  2242. | 3 | 1 | 1 | 1 | 2 |
  2243. */
  2244. #define REQUIRE_ORDER(sort, ...) do { \
  2245. std::vector<size_t> expected = {__VA_ARGS__}; \
  2246. auto results = sort; \
  2247. REQUIRE(results.size() == expected.size()); \
  2248. for (size_t i = 0; i < expected.size(); ++i) \
  2249. REQUIRE(results.get(i).get_index() == expected[i]); \
  2250. } while (0)
  2251. SECTION("sort on single property") {
  2252. REQUIRE_ORDER((r.sort({{"value", true}})),
  2253. 2, 3, 0, 1);
  2254. REQUIRE_ORDER((r.sort({{"value", false}})),
  2255. 1, 0, 3, 2);
  2256. }
  2257. SECTION("sort on two properties") {
  2258. REQUIRE_ORDER((r.sort({{"bool", true}, {"value", true}})),
  2259. 2, 0, 3, 1);
  2260. REQUIRE_ORDER((r.sort({{"bool", false}, {"value", true}})),
  2261. 3, 1, 2, 0);
  2262. REQUIRE_ORDER((r.sort({{"bool", true}, {"value", false}})),
  2263. 0, 2, 1, 3);
  2264. REQUIRE_ORDER((r.sort({{"bool", false}, {"value", false}})),
  2265. 1, 3, 0, 2);
  2266. }
  2267. SECTION("sort over link") {
  2268. REQUIRE_ORDER((r.sort({{"link.value", true}})),
  2269. 0, 3, 2, 1);
  2270. REQUIRE_ORDER((r.sort({{"link.value", false}})),
  2271. 1, 2, 3, 0);
  2272. }
  2273. SECTION("sort over two links") {
  2274. REQUIRE_ORDER((r.sort({{"link.link.value", true}})),
  2275. 1, 0, 3, 2);
  2276. REQUIRE_ORDER((r.sort({{"link.link.value", false}})),
  2277. 2, 3, 0, 1);
  2278. }
  2279. }
  2280. struct ResultsFromTable {
  2281. static Results call(std::shared_ptr<Realm> r, Table* table) {
  2282. return Results(std::move(r), *table);
  2283. }
  2284. };
  2285. struct ResultsFromQuery {
  2286. static Results call(std::shared_ptr<Realm> r, Table* table) {
  2287. return Results(std::move(r), table->where());
  2288. }
  2289. };
  2290. struct ResultsFromTableView {
  2291. static Results call(std::shared_ptr<Realm> r, Table* table) {
  2292. return Results(std::move(r), table->where().find_all());
  2293. }
  2294. };
  2295. struct ResultsFromLinkView {
  2296. static Results call(std::shared_ptr<Realm> r, Table* table) {
  2297. r->begin_transaction();
  2298. auto link_table = r->read_group().get_table("class_linking_object");
  2299. link_table->add_empty_row(1);
  2300. auto link_view = link_table->get_linklist(0, 0);
  2301. for (size_t i = 0; i < table->size(); ++i)
  2302. link_view->add(i);
  2303. r->commit_transaction();
  2304. return Results(r, link_view);
  2305. }
  2306. };
  2307. TEMPLATE_TEST_CASE("results: aggregate", "[query][aggregate]", ResultsFromTable, ResultsFromQuery, ResultsFromTableView, ResultsFromLinkView) {
  2308. InMemoryTestFile config;
  2309. config.cache = false;
  2310. config.automatic_change_notifications = false;
  2311. auto r = Realm::get_shared_realm(config);
  2312. r->update_schema({
  2313. {"object", {
  2314. {"int", PropertyType::Int|PropertyType::Nullable},
  2315. {"float", PropertyType::Float|PropertyType::Nullable},
  2316. {"double", PropertyType::Double|PropertyType::Nullable},
  2317. {"date", PropertyType::Date|PropertyType::Nullable},
  2318. }},
  2319. {"linking_object", {
  2320. {"link", PropertyType::Array|PropertyType::Object, "object"}
  2321. }},
  2322. });
  2323. auto table = r->read_group().get_table("class_object");
  2324. SECTION("one row with null values") {
  2325. r->begin_transaction();
  2326. table->add_empty_row(3);
  2327. table->set_int(0, 1, 0);
  2328. table->set_float(1, 1, 0.f);
  2329. table->set_double(2, 1, 0.0);
  2330. table->set_timestamp(3, 1, Timestamp(0, 0));
  2331. table->set_int(0, 2, 2);
  2332. table->set_float(1, 2, 2.f);
  2333. table->set_double(2, 2, 2.0);
  2334. table->set_timestamp(3, 2, Timestamp(2, 0));
  2335. // table:
  2336. // null, null, null, null,
  2337. // 0, 0, 0, (0, 0)
  2338. // 2, 2, 2, (2, 0)
  2339. r->commit_transaction();
  2340. Results results = TestType::call(r, table.get());
  2341. SECTION("max") {
  2342. REQUIRE(results.max(0)->get_int() == 2);
  2343. REQUIRE(results.max(1)->get_float() == 2.f);
  2344. REQUIRE(results.max(2)->get_double() == 2.0);
  2345. REQUIRE(results.max(3)->get_timestamp() == Timestamp(2, 0));
  2346. }
  2347. SECTION("min") {
  2348. REQUIRE(results.min(0)->get_int() == 0);
  2349. REQUIRE(results.min(1)->get_float() == 0.f);
  2350. REQUIRE(results.min(2)->get_double() == 0.0);
  2351. REQUIRE(results.min(3)->get_timestamp() == Timestamp(0, 0));
  2352. }
  2353. SECTION("average") {
  2354. REQUIRE(results.average(0) == 1.0);
  2355. REQUIRE(results.average(1) == 1.0);
  2356. REQUIRE(results.average(2) == 1.0);
  2357. REQUIRE_THROWS_AS(results.average(3), Results::UnsupportedColumnTypeException);
  2358. }
  2359. SECTION("sum") {
  2360. REQUIRE(results.sum(0)->get_int() == 2);
  2361. REQUIRE(results.sum(1)->get_double() == 2.0);
  2362. REQUIRE(results.sum(2)->get_double() == 2.0);
  2363. REQUIRE_THROWS_AS(results.sum(3), Results::UnsupportedColumnTypeException);
  2364. }
  2365. }
  2366. SECTION("rows with all null values") {
  2367. r->begin_transaction();
  2368. table->add_empty_row(3);
  2369. // table:
  2370. // null, null, null, null, null
  2371. // null, null, null, null, null
  2372. // null, null, null, null, null
  2373. r->commit_transaction();
  2374. Results results = TestType::call(r, table.get());
  2375. SECTION("max") {
  2376. REQUIRE(!results.max(0));
  2377. REQUIRE(!results.max(1));
  2378. REQUIRE(!results.max(2));
  2379. REQUIRE(!results.max(3));
  2380. }
  2381. SECTION("min") {
  2382. REQUIRE(!results.min(0));
  2383. REQUIRE(!results.min(1));
  2384. REQUIRE(!results.min(2));
  2385. REQUIRE(!results.min(3));
  2386. }
  2387. SECTION("average") {
  2388. REQUIRE(!results.average(0));
  2389. REQUIRE(!results.average(1));
  2390. REQUIRE(!results.average(2));
  2391. REQUIRE_THROWS_AS(results.average(3), Results::UnsupportedColumnTypeException);
  2392. }
  2393. SECTION("sum") {
  2394. REQUIRE(results.sum(0)->get_int() == 0);
  2395. REQUIRE(results.sum(1)->get_double() == 0.0);
  2396. REQUIRE(results.sum(2)->get_double() == 0.0);
  2397. REQUIRE_THROWS_AS(results.sum(3), Results::UnsupportedColumnTypeException);
  2398. }
  2399. }
  2400. SECTION("empty") {
  2401. Results results = TestType::call(r, table.get());
  2402. SECTION("max") {
  2403. REQUIRE(!results.max(0));
  2404. REQUIRE(!results.max(1));
  2405. REQUIRE(!results.max(2));
  2406. REQUIRE(!results.max(3));
  2407. }
  2408. SECTION("min") {
  2409. REQUIRE(!results.min(0));
  2410. REQUIRE(!results.min(1));
  2411. REQUIRE(!results.min(2));
  2412. REQUIRE(!results.min(3));
  2413. }
  2414. SECTION("average") {
  2415. REQUIRE(!results.average(0));
  2416. REQUIRE(!results.average(1));
  2417. REQUIRE(!results.average(2));
  2418. REQUIRE_THROWS_AS(results.average(3), Results::UnsupportedColumnTypeException);
  2419. }
  2420. SECTION("sum") {
  2421. REQUIRE(results.sum(0)->get_int() == 0);
  2422. REQUIRE(results.sum(1)->get_double() == 0.0);
  2423. REQUIRE(results.sum(2)->get_double() == 0.0);
  2424. REQUIRE_THROWS_AS(results.sum(3), Results::UnsupportedColumnTypeException);
  2425. }
  2426. }
  2427. }
  2428. TEST_CASE("results: set property value on all objects", "[batch_updates]") {
  2429. InMemoryTestFile config;
  2430. config.automatic_change_notifications = false;
  2431. config.cache = false;
  2432. config.schema = Schema{
  2433. {"AllTypes", {
  2434. {"pk", PropertyType::Int, Property::IsPrimary{true}},
  2435. {"bool", PropertyType::Bool},
  2436. {"int", PropertyType::Int},
  2437. {"float", PropertyType::Float},
  2438. {"double", PropertyType::Double},
  2439. {"string", PropertyType::String},
  2440. {"data", PropertyType::Data},
  2441. {"date", PropertyType::Date},
  2442. {"object", PropertyType::Object|PropertyType::Nullable, "AllTypes"},
  2443. {"list", PropertyType::Array|PropertyType::Object, "AllTypes"},
  2444. {"bool array", PropertyType::Array|PropertyType::Bool},
  2445. {"int array", PropertyType::Array|PropertyType::Int},
  2446. {"float array", PropertyType::Array|PropertyType::Float},
  2447. {"double array", PropertyType::Array|PropertyType::Double},
  2448. {"string array", PropertyType::Array|PropertyType::String},
  2449. {"data array", PropertyType::Array|PropertyType::Data},
  2450. {"date array", PropertyType::Array|PropertyType::Date},
  2451. {"object array", PropertyType::Array|PropertyType::Object, "AllTypes"},
  2452. }, {
  2453. {"parents", PropertyType::LinkingObjects|PropertyType::Array, "AllTypes", "object"},
  2454. }}
  2455. };
  2456. config.schema_version = 0;
  2457. auto realm = Realm::get_shared_realm(config);
  2458. auto table = realm->read_group().get_table("class_AllTypes");
  2459. realm->begin_transaction();
  2460. table->add_empty_row(2);
  2461. realm->commit_transaction();
  2462. Results r(realm, *table);
  2463. TestContext ctx(realm);
  2464. SECTION("non-existing property name") {
  2465. realm->begin_transaction();
  2466. REQUIRE_THROWS_AS(r.set_property_value(ctx, "i dont exist", util::Any(false)), Results::InvalidPropertyException);
  2467. realm->cancel_transaction();
  2468. }
  2469. SECTION("readonly property") {
  2470. realm->begin_transaction();
  2471. REQUIRE_THROWS_AS(r.set_property_value(ctx, "parents", util::Any(false)), ReadOnlyPropertyException);
  2472. realm->cancel_transaction();
  2473. }
  2474. SECTION("primarykey property") {
  2475. realm->begin_transaction();
  2476. REQUIRE_THROWS_AS(r.set_property_value(ctx, "pk", util::Any(1)), std::logic_error);
  2477. realm->cancel_transaction();
  2478. }
  2479. SECTION("set property values removes object from Results") {
  2480. realm->begin_transaction();
  2481. Results results(realm, table->where().equal(2,0));
  2482. CHECK(results.size() == 2);
  2483. r.set_property_value(ctx, "int", util::Any(INT64_C(42)));
  2484. CHECK(results.size() == 0);
  2485. realm->cancel_transaction();
  2486. }
  2487. SECTION("set property value") {
  2488. realm->begin_transaction();
  2489. r.set_property_value<util::Any>(ctx, "bool", util::Any(true));
  2490. for (size_t i = 0; i < r.size(); i++) {
  2491. CHECK(r.get(i).get_bool(1) == true);
  2492. }
  2493. r.set_property_value(ctx, "int", util::Any(INT64_C(42)));
  2494. for (size_t i = 0; i < r.size(); i++) {
  2495. CHECK(r.get(i).get_int(2) == 42);
  2496. }
  2497. r.set_property_value(ctx, "float", util::Any(1.23f));
  2498. for (size_t i = 0; i < r.size(); i++) {
  2499. CHECK(r.get(i).get_float(3) == 1.23f);
  2500. }
  2501. r.set_property_value(ctx, "double", util::Any(1.234));
  2502. for (size_t i = 0; i < r.size(); i++) {
  2503. CHECK(r.get(i).get_double(4) == 1.234);
  2504. }
  2505. r.set_property_value(ctx, "string", util::Any(std::string("abc")));
  2506. for (size_t i = 0; i < r.size(); i++) {
  2507. CHECK(r.get(i).get_string(5) == "abc");
  2508. }
  2509. r.set_property_value(ctx, "data", util::Any(std::string("abc")));
  2510. for (size_t i = 0; i < r.size(); i++) {
  2511. CHECK(r.get(i).get_binary(6) == BinaryData("abc", 3));
  2512. }
  2513. util::Any timestamp = Timestamp(1, 2);
  2514. r.set_property_value(ctx, "date", timestamp);
  2515. for (size_t i = 0; i < r.size(); i++) {
  2516. CHECK(r.get(i).get_timestamp(7) == any_cast<Timestamp>(timestamp));
  2517. }
  2518. size_t object_ndx = table->add_empty_row();
  2519. Object linked_obj(realm, "AllTypes", object_ndx);
  2520. r.set_property_value(ctx, "object", util::Any(linked_obj));
  2521. for (size_t i = 0; i < r.size(); i++) {
  2522. CHECK(r.get(i).get_link(8) == object_ndx);
  2523. }
  2524. size_t list_object_ndx = table->add_empty_row();
  2525. Object list_object(realm, "AllTypes", list_object_ndx);
  2526. r.set_property_value(ctx, "list", util::Any(AnyVector{list_object, list_object}));
  2527. for (size_t i = 0; i < r.size(); i++) {
  2528. auto list = r.get(i).get_linklist(9);
  2529. CHECK(list->size() == 2);
  2530. CHECK(list->get(0).get_index() == list_object_ndx);
  2531. CHECK(list->get(1).get_index() == list_object_ndx);
  2532. }
  2533. auto check_array = [&](size_t col, auto... values) {
  2534. size_t rows = r.size();
  2535. for (size_t i = 0; i < rows; ++i) {
  2536. RowExpr row = r.get(i);
  2537. auto table = row.get_subtable(col);
  2538. size_t j = 0;
  2539. for (auto& value : {values...}) {
  2540. CAPTURE(j);
  2541. REQUIRE(j < row.get_subtable_size(col));
  2542. REQUIRE(value == table->get<typename std::decay<decltype(value)>::type>(0, j));
  2543. ++j;
  2544. }
  2545. }
  2546. };
  2547. r.set_property_value(ctx, "bool array", util::Any(AnyVec{true, false}));
  2548. check_array(10, true, false);
  2549. r.set_property_value(ctx, "int array", util::Any(AnyVec{INT64_C(5), INT64_C(6)}));
  2550. check_array(11, INT64_C(5), INT64_C(6));
  2551. r.set_property_value(ctx, "float array", util::Any(AnyVec{1.1f, 2.2f}));
  2552. check_array(12, 1.1f, 2.2f);
  2553. r.set_property_value(ctx, "double array", util::Any(AnyVec{3.3, 4.4}));
  2554. check_array(13, 3.3, 4.4);
  2555. r.set_property_value(ctx, "string array", util::Any(AnyVec{"a"s, "b"s, "c"s}));
  2556. check_array(14, StringData("a"), StringData("b"), StringData("c"));
  2557. r.set_property_value(ctx, "data array", util::Any(AnyVec{"d"s, "e"s, "f"s}));
  2558. check_array(15, BinaryData("d",1), BinaryData("e",1), BinaryData("f",1));
  2559. r.set_property_value(ctx, "date array", util::Any(AnyVec{Timestamp(10,20), Timestamp(20,30), Timestamp(30,40)}));
  2560. check_array(16, Timestamp(10,20), Timestamp(20,30), Timestamp(30,40));
  2561. }
  2562. }
  2563. TEST_CASE("results: limit", "[limit]") {
  2564. InMemoryTestFile config;
  2565. config.cache = false;
  2566. config.automatic_change_notifications = false;
  2567. config.schema = Schema{
  2568. {"object", {
  2569. {"value", PropertyType::Int},
  2570. }},
  2571. };
  2572. auto realm = Realm::get_shared_realm(config);
  2573. auto table = realm->read_group().get_table("class_object");
  2574. realm->begin_transaction();
  2575. table->add_empty_row(8);
  2576. for (int i = 0; i < 8; ++i) {
  2577. table->set_int(0, i, (i + 2) % 4);
  2578. }
  2579. realm->commit_transaction();
  2580. Results r(realm, *table);
  2581. SECTION("unsorted") {
  2582. REQUIRE(r.limit(0).size() == 0);
  2583. REQUIRE_ORDER(r.limit(1), 0);
  2584. REQUIRE_ORDER(r.limit(2), 0, 1);
  2585. REQUIRE_ORDER(r.limit(8), 0, 1, 2, 3, 4, 5, 6, 7);
  2586. REQUIRE_ORDER(r.limit(100), 0, 1, 2, 3, 4, 5, 6, 7);
  2587. }
  2588. SECTION("sorted") {
  2589. auto sorted = r.sort({{"value", true}});
  2590. REQUIRE(sorted.limit(0).size() == 0);
  2591. REQUIRE_ORDER(sorted.limit(1), 2);
  2592. REQUIRE_ORDER(sorted.limit(2), 2, 6);
  2593. REQUIRE_ORDER(sorted.limit(8), 2, 6, 3, 7, 0, 4, 1, 5);
  2594. REQUIRE_ORDER(sorted.limit(100), 2, 6, 3, 7, 0, 4, 1, 5);
  2595. }
  2596. SECTION("sort after limit") {
  2597. REQUIRE(r.limit(0).sort({{"value", true}}).size() == 0);
  2598. REQUIRE_ORDER(r.limit(1).sort({{"value", true}}), 0);
  2599. REQUIRE_ORDER(r.limit(3).sort({{"value", true}}), 2, 0, 1);
  2600. REQUIRE_ORDER(r.limit(8).sort({{"value", true}}), 2, 6, 3, 7, 0, 4, 1, 5);
  2601. REQUIRE_ORDER(r.limit(100).sort({{"value", true}}), 2, 6, 3, 7, 0, 4, 1, 5);
  2602. }
  2603. SECTION("distinct") {
  2604. auto sorted = r.distinct({"value"});
  2605. REQUIRE(sorted.limit(0).size() == 0);
  2606. REQUIRE_ORDER(sorted.limit(1), 0);
  2607. REQUIRE_ORDER(sorted.limit(2), 0, 1);
  2608. REQUIRE_ORDER(sorted.limit(8), 0, 1, 2, 3);
  2609. sorted = r.sort({{"value", true}}).distinct({"value"});
  2610. REQUIRE(sorted.limit(0).size() == 0);
  2611. REQUIRE_ORDER(sorted.limit(1), 2);
  2612. REQUIRE_ORDER(sorted.limit(2), 2, 3);
  2613. REQUIRE_ORDER(sorted.limit(8), 2, 3, 0, 1);
  2614. }
  2615. SECTION("notifications on results using all descriptor types") {
  2616. r = r.distinct({"value"}).sort({{"value", false}}).limit(2);
  2617. int notification_calls = 0;
  2618. auto token = r.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  2619. REQUIRE_FALSE(err);
  2620. if (notification_calls == 0) {
  2621. REQUIRE(c.empty());
  2622. REQUIRE(r.size() == 2);
  2623. REQUIRE(r.get(0).get_int(0) == 3);
  2624. REQUIRE(r.get(1).get_int(0) == 2);
  2625. } else if (notification_calls == 1) {
  2626. REQUIRE(!c.empty());
  2627. REQUIRE_INDICES(c.insertions, 0);
  2628. REQUIRE_INDICES(c.deletions, 1);
  2629. REQUIRE(c.moves.size() == 0);
  2630. REQUIRE(c.modifications.count() == 0);
  2631. REQUIRE(r.size() == 2);
  2632. REQUIRE(r.get(0).get_int(0) == 5);
  2633. REQUIRE(r.get(1).get_int(0) == 3);
  2634. }
  2635. ++notification_calls;
  2636. });
  2637. advance_and_notify(*realm);
  2638. REQUIRE(notification_calls == 1);
  2639. realm->begin_transaction();
  2640. table->add_empty_row(1);
  2641. table->set_int(0, 8, 5);
  2642. realm->commit_transaction();
  2643. advance_and_notify(*realm);
  2644. REQUIRE(notification_calls == 2);
  2645. }
  2646. SECTION("notifications on only limited results") {
  2647. r = r.limit(2);
  2648. int notification_calls = 0;
  2649. auto token = r.add_notification_callback([&](CollectionChangeSet c, std::exception_ptr err) {
  2650. REQUIRE_FALSE(err);
  2651. if (notification_calls == 0) {
  2652. REQUIRE(c.empty());
  2653. REQUIRE(r.size() == 2);
  2654. } else if (notification_calls == 1) {
  2655. REQUIRE(!c.empty());
  2656. REQUIRE(c.insertions.count() == 0);
  2657. REQUIRE(c.deletions.count() == 0);
  2658. REQUIRE(c.modifications.count() == 1);
  2659. REQUIRE_INDICES(c.modifications, 1);
  2660. REQUIRE(r.size() == 2);
  2661. }
  2662. ++notification_calls;
  2663. });
  2664. advance_and_notify(*realm);
  2665. REQUIRE(notification_calls == 1);
  2666. realm->begin_transaction();
  2667. table->set_int(0, 1, 5);
  2668. realm->commit_transaction();
  2669. advance_and_notify(*realm);
  2670. REQUIRE(notification_calls == 2);
  2671. }
  2672. SECTION("does not support further filtering") {
  2673. auto limited = r.limit(0);
  2674. REQUIRE_THROWS_AS(limited.filter(table->where()), Results::UnimplementedOperationException);
  2675. }
  2676. }