/* Ċ Ġ* ĠCopyright Ġ 2 0 2 0 ĠGoogle ĠLLC . Ċ Ġ* Ċ Ġ* ĠThis Ġcode Ġmay Ġonly Ġbe Ġused Ġunder Ġthe ĠBSD Ġstyle Ġlicense Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / LICENSE . txt Ċ Ġ* Ċ Ġ* ĠCode Ġdistributed Ġby ĠGoogle Ġas Ġpart Ġof Ġthis Ġproject Ġis Ġalso Ġsubject Ġto Ġan Ġadditional ĠIP Ġrights Ċ Ġ* Ġgrant Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / PAT ENTS . txt Ċ Ġ*/ Ċ Ċ package Ġar cs . core . storage Ċ Ċ import Ġar cs . core . common . Refer enc able Ċ import Ġar cs . core . common . Reference Id Ċ import Ġar cs . core . cr dt . Cr dt Entity Ċ import Ġar cs . core . cr dt . Cr dt Exception Ċ import Ġar cs . core . cr dt . Cr dt Set Ċ import Ġar cs . core . cr dt . Version Map Ċ import Ġar cs . core . cr dt . testing . Cr dt Entity Helper Ċ import Ġar cs . core . cr dt . testing . Cr dt Set Helper Ċ import Ġar cs . core . data . Collection Type Ċ import Ġar cs . core . data . EntityType Ċ import Ġar cs . core . data . FieldType Ċ import Ġar cs . core . data . Raw Entity Ċ import Ġar cs . core . data . Schema Ċ import Ġar cs . core . data . Schema Fields Ċ import Ġar cs . core . data . Schema Name Ċ import Ġar cs . core . data . Singleton Type Ċ import Ġar cs . core . data . util . to Refer enc able Ċ import Ġar cs . core . storage . reference mode . Ref Mode Store Data Ċ import Ġar cs . core . storage . reference mode . Ref Mode Store Op Ċ import Ġar cs . core . storage . reference mode . Ref Mode Store Output Ċ import Ġar cs . core . storage . reference mode . Reference Mode Storage Key Ċ import Ġar cs . core . storage . testutil . Fake Driver Ċ import Ġar cs . core . storage . testutil . Fake Driver Vendor Ċ import Ġar cs . core . storage . testutil . Ref Mode Store Helper Ċ import Ġar cs . core . storage . testutil . getEntity Driver Ċ import Ġar cs . core . storage . testutil . get Stored Data ForTesting Ċ import Ġar cs . core . storage . testutil . test Write Back Provider Ċ import Ġar cs . core . util . testutil . Log Rule Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġkotlin . test . assert Fails With Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . Job Ċ import Ġkotlinx . coroutines . delay Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlinx . coroutines . run Blocking Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Ignore Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ Ċ /** ĠTests Ġfor Ġthe Ġ[ Reference Mode Store ]. Ġ*/ Ċ @ Suppress (" UN CHECK ED _ CAST ") Ċ @ RunWith ( JUnit 4 :: class ) Ċ @ Opt In ( Experimental Cor outines Api :: class ) Ċ class ĠReference Mode Store Test Ġ{ ĊĠ Ġ@ get : Rule ĊĠ Ġval Ġlog Ġ= ĠLog Rule () ĊĊĠ Ġprivate Ġlateinit Ġvar Ġtest Key : ĠReference Mode Storage Key ĊĠ Ġprivate Ġlateinit Ġvar Ġschema : ĠSchema ĊĊĠ Ġ@ Before ĊĠ Ġfun Ġsetup () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġtest Key Ġ= ĠReference Mode Storage Key ( ĊĠĠĠĠĠ ĠMock Hierarchical Storage Key (), ĊĠĠĠĠĠ ĠMock Hierarchical Storage Key () ĊĠĠĠ Ġ) ĊĠĠĠ Ġschema Ġ= ĠSchema ( ĊĠĠĠĠĠ Ġset Of ( Schema Name (" person ")), ĊĠĠĠĠĠ ĠSchema Fields ( ĊĠĠĠĠĠĠĠ Ġsing letons Ġ= Ġmap Of (" name " Ġto ĠFieldType . Text , Ġ" age " Ġto ĠFieldType . Number ), ĊĠĠĠĠĠĠĠ Ġcollections Ġ= Ġempty Map () ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġ" hash " ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġdriver Factory Ġ= ĠFixed Driver Factory ( Fake Driver Vendor ()) ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġthrows Exception _ if Appro priate Driver C ant Be Found () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġassert Fails With < Cr dt Exception > Ġ{ ĊĠĠĠĠĠ ĠActive Store < Ref Mode Store Data , ĠRef Mode Store Op , ĠRef Mode Store Output >( ĊĠĠĠĠĠĠĠ ĠStore Options ( ĊĠĠĠĠĠĠĠĠĠ Ġtest Key , ĊĠĠĠĠĠĠĠĠĠ ĠSingleton Type ( EntityType ( schema )) ĊĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠ Ġthis , ĊĠĠĠĠĠĠĠ ĠFixed Driver Factory (), ĊĠĠĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠĠĠ Ġnull ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġconstructs Reference Mode Stores _ when Required () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġstore Ġ= ĠActive Store < Ref Mode Store Data , ĠRef Mode Store Op , ĠRef Mode Store Output >( ĊĠĠĠĠĠ ĠStore Options ( ĊĠĠĠĠĠĠĠ Ġtest Key , ĊĠĠĠĠĠĠĠ ĠCollection Type ( EntityType ( schema )) ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġthis , ĊĠĠĠĠĠ Ġdriver Factory , ĊĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĊĠĠĠ ĠassertThat ( store ). isInstanceOf ( Reference Mode Store :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġpropag ates Model Updates _ from Prox ies _ to Drivers () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġ( collection , Ġcollection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġcollection Helper . add ( create Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 )) ĊĊĠĠĠ Ġactive Store . on Proxy Message ( ĊĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( collection . data ), Ġ 1 ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĊĠĠĠ Ġval Ġcontainer Store Driver Ġ= Ġrequire NotNull ( ĊĠĠĠĠĠ Ġactive Store . container Store . driver Ġas ? ĠFake Driver < Cr dt Set . Data < Reference >> ĊĠĠĠ Ġ) Ġ{ Ġ" Container Store ĠDriver Ġis Ġnot Ġof Ġexpected Ġtype : ĠCr dt Set . Data < Reference >" Ġ} ĊĊĠĠĠ Ġval Ġcaptured Collection Ġ= Ġcontainer Store Driver . last Data ĊĊĠĠĠ ĠassertThat ( captured Collection !!. values ) ĊĠĠĠĠĠ Ġ. contains Exactly ( ĊĠĠĠĠĠĠĠ Ġ" an - id ", ĊĠĠĠĠĠĠĠ ĠCr dt Set . Data Value ( ĊĠĠĠĠĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 ), ĊĠĠĠĠĠĠĠĠĠ ĠReference (" an - id ", Ġactive Store . back ing Store . storage Key , ĠVersion Map ( actor Ġto Ġ 1 )) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġbob Driver Ġ= Ġactive Store . back ing Store . getEntity Driver (" an - id ") ĊĠĠĠ Ġval Ġcaptured Bob Ġ= Ġbob Driver . get Stored Data ForTesting () ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠTODO ( b / 1 7 5 5 0 5 6 2 9 ): ĠThe Ġversions Ġhere Ġare Ġ 2 Ġrather Ġthan Ġ 1 Ġas ĠReference Mode Store Ġwrites ĊĠĠĠĠ Ġ* Ġnew Ġentities Ġtwice Ġwhen Ġthe Ġentities Ġare Ġadded Ġvia Ġa ĠModel Update . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ ĠassertThat ( captured Bob . sing letons [" name "] ?. data ?. version Map ) ĊĠĠĠĠĠ Ġ. isEqualTo ( Version Map ( actor Ġto Ġ 2 )) ĊĠĠĠ ĠassertThat ( captured Bob . sing letons [" age "] ?. data ?. version Map ) ĊĠĠĠĠĠ Ġ. isEqualTo ( Version Map ( actor Ġto Ġ 2 )) ĊĠĠĠ ĠassertThat ( captured Bob . to Raw Entity (). sing letons ) ĊĠĠĠĠĠ Ġ. contains Exactly ( ĊĠĠĠĠĠĠĠ Ġ" name ", Ġ" bob ". to Refer enc able (), ĊĠĠĠĠĠĠĠ Ġ" age ", Ġ 4 2 . 0 . to Refer enc able () ĊĠĠĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġapplies And Propag ates Operation Update _ from Prox ies _ to Drivers () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper ( actor , Ġactive Store ) ĊĠĠĠ Ġval Ġ(_, Ġperson Collection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġ( reference Collection , Ġreference Collection Helper ) Ġ= Ġcreate Cr dt Set < Reference >( actor ) ĊĠĠĠ Ġval Ġbob Entity Ġ= Ġcreate Person Entity Cr dt () ĊĠĠĠ Ġval Ġbob Entity Helper Ġ= ĠCr dt Entity Helper ( active Store . cr dt Key , Ġbob Entity ) ĊĊĠĠĠ Ġ// ĠApply Ġto ĠRef Mode Ġstore . ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġstore Helper . send Add Op ( bob ) ĊĊĠĠĠ Ġ// ĠApply Ġto Ġexpected Ġcollection Ġrepresentation ĊĠĠĠ Ġperson Collection Helper . add ( bob ) ĊĠĠĠ Ġ// ĠApply Ġto Ġexpected Ġref Mode Ġcollection Store Ġdata . ĊĠĠĠ Ġval Ġbob Ref Ġ= ĠReference ( ĊĠĠĠĠĠ Ġ" an - id ", ĊĠĠĠĠĠ Ġactive Store . back ing Store . storage Key , ĊĠĠĠĠĠ ĠVersion Map ( actor Ġto Ġ 1 ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġreference Collection Helper . add ( bob Ref ) ĊĠĠĠ Ġ// ĠApply Ġto Ġexpected Ġref Mode Ġbacking Store Ġdata . ĊĠĠĠ Ġbob Entity Helper . update (" name ", ĠCr dt Entity . Reference Impl (" bob ". to Refer enc able (). id )) ĊĠĠĠ Ġbob Entity Helper . update (" age ", ĠCr dt Entity . Reference Impl ( 4 2 . 0 . to Refer enc able (). id )) ĊĊĠĠĠ Ġval Ġcontainer Driver Ġ= ĊĠĠĠĠĠ Ġactive Store . container Store . driver Ġas ĠFake Driver < Cr dt Set . Data < Reference >> ĊĊĠĠĠ Ġval Ġcaptured People Ġ= Ġcontainer Driver . last Data ĊĠĠĠ ĠassertThat ( captured People ). isEqualTo ( reference Collection . data ) ĊĠĠĠ Ġval Ġstored Bob Ġ= Ġactive Store . getLocal Data (" an - id ") ĊĠĠĠ Ġ// ĠCheck Ġthat Ġthe Ġstored Ġbob 's Ġsingleton Ġdata Ġis Ġequal Ġto Ġthe Ġexpected Ġbob 's Ġsingleton Ġdata ĊĠĠĠ ĠassertThat ( stored Bob . sing letons . map Values Ġ{ Ġit . value . data Ġ}) ĊĠĠĠĠĠ Ġ. isEqualTo ( bob Entity . data . sing letons . map Values Ġ{ Ġit . value . data Ġ}) ĊĠĠĠ Ġ// ĠCheck Ġthat Ġthe Ġstored Ġbob 's Ġcollection Ġdata Ġis Ġequal Ġto Ġthe Ġexpected Ġbob 's Ġcollection ĊĠĠĠ Ġ// Ġdata Ġ( empty ) ĊĠĠĠ ĠassertThat ( stored Bob . collections . map Values Ġ{ Ġit . value . data Ġ}) ĊĠĠĠĠĠ Ġ. isEqualTo ( bob Entity . data . collections . map Values Ġ{ Ġit . value . data Ġ}) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġremove Op Clear s Backing Entity () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper ( actor , Ġactive Store ) ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĊĠĠĠ Ġ// ĠAdd ĠBob Ġto Ġcollection . ĊĠĠĠ Ġstore Helper . send Add Op ( bob ) ĊĊĠĠĠ Ġ// ĠBob Ġwas Ġadded Ġto Ġthe Ġbacking Ġstore . ĊĠĠĠ Ġval Ġstored Bob Ġ= Ġactive Store . getLocal Data (" an - id ") ĊĠĠĠ ĠassertThat ( stored Bob . to Raw Entity (" an - id ")). isEqualTo ( bob ) ĊĊĠĠĠ Ġ// ĠRemove ĠBob Ġfrom Ġthe Ġcollection . ĊĠĠĠ Ġstore Helper . send Remove Op ( bob . id ) ĊĊĠĠĠ Ġ// ĠCheck Ġthe Ġbacking Ġstore ĠBob Ġhas Ġbeen Ġcleared . ĊĠĠĠ Ġval Ġstored Bob 2 Ġ= Ġactive Store . getLocal Data (" an - id ") ĊĠĠĠ ĠassertThat ( stored Bob 2 . to Raw Entity (" an - id ")). isEqualTo ( create Empty Person Entity (" an - id ")) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġclear Op Clear s Backing Entities () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper ( actor , Ġactive Store ) ĊĊĠĠĠ Ġ// ĠAdd Ġa Ġcouple Ġof Ġpeople . ĊĠĠĠ Ġval Ġalice Ġ= Ġcreate Person Entity (" id 1 ", Ġ" alice ", Ġ 1 0 ) ĊĠĠĠ Ġstore Helper . send Add Op ( alice ) ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" id 2 ", Ġ" bob ", Ġ 2 0 ) ĊĠĠĠ Ġstore Helper . send Add Op ( bob ) ĊĊĠĠĠ Ġ// ĠVerify Ġthat Ġthey 've Ġbeen Ġstored . ĊĠĠĠ Ġval Ġstored Refs Ġ= Ġactive Store . container Store . getLocal Data () Ġas ĠCr dt Set . Data < Reference > ĊĠĠĠ ĠassertThat ( stored Refs . values . keys ). contains Exactly (" id 1 ", Ġ" id 2 ") ĊĊĠĠĠ Ġval Ġstored Alice Ġ= Ġactive Store . getLocal Data (" id 1 ") ĊĠĠĠ ĠassertThat ( stored Alice . to Raw Entity (" id 1 ")). isEqualTo ( alice ) ĊĊĠĠĠ Ġval Ġstored Bob Ġ= Ġactive Store . getLocal Data (" id 2 ") ĊĠĠĠ ĠassertThat ( stored Bob . to Raw Entity (" id 2 ")). isEqualTo ( bob ) ĊĊĠĠĠ Ġ// ĠClear ! ĊĠĠĠ Ġstore Helper . send Collection Clear Op () ĊĊĠĠĠ Ġval Ġcleared Refs Ġ= Ġactive Store . container Store . getLocal Data () Ġas ĠCr dt Set . Data < Reference > ĊĠĠĠ ĠassertThat ( cleared Refs . values . keys ). isEmpty () ĊĊĠĠĠ Ġval Ġcleared Alice Ġ= Ġactive Store . getLocal Data (" id 1 ") ĊĠĠĠ ĠassertThat ( cleared Alice . to Raw Entity (" id 1 ")). isEqualTo ( create Empty Person Entity (" id 1 ")) ĊĊĠĠĠ Ġval Ġcleared Bob Ġ= Ġactive Store . getLocal Data (" id 2 ") ĊĠĠĠ ĠassertThat ( cleared Bob . to Raw Entity (" id 2 ")). isEqualTo ( create Empty Person Entity (" id 2 ")) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsingleton Clear Fre es Backing Store Copy () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Singleton Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĊĠĠĠ Ġ// ĠSet Ġsingleton Ġto ĠBob . ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper ( actor , Ġactive Store ) ĊĠĠĠ Ġstore Helper . send Update Op ( bob ) ĊĊĠĠĠ Ġ// ĠBob Ġwas Ġadded Ġto Ġthe Ġbacking Ġstore . ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores . keys ). contains Exactly (" an - id ") ĊĊĠĠĠ Ġ// ĠRemove ĠBob Ġfrom Ġthe Ġcollection . ĊĠĠĠ Ġstore Helper . send Singleton Clear Op () ĊĊĠĠĠ Ġ// ĠCheck Ġmemory Ġcopy Ġhas Ġbeen Ġfreed . ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores . keys ). isEmpty () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsingleton Update Fre es Backing Store Copy () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Singleton Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper ( actor , Ġactive Store ) ĊĠĠĠ Ġval Ġalice Ġ= Ġcreate Person Entity (" a - id ", Ġ" alice ", Ġ 4 1 ) ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" b - id ", Ġ" bob ", Ġ 4 2 ) ĊĊĠĠĠ Ġ// ĠSet Ġsingleton Ġto ĠBob . ĊĠĠĠ Ġstore Helper . send Update Op ( bob ) ĊĊĠĠĠ Ġ// ĠBob Ġwas Ġadded Ġto Ġthe Ġbacking Ġstore . ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores . keys ). contains Exactly (" b - id ") ĊĊĠĠĠ Ġ// ĠSet Ġsingleton Ġto ĠAlice . ĊĠĠĠ Ġstore Helper . send Update Op ( alice ) ĊĊĠĠĠ Ġ// ĠCheck ĠBob 's Ġmemory Ġcopy Ġhas Ġbeen Ġfreed . ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores . keys ). contains Exactly (" a - id ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġrespon ds To A Model Request _ from Proxy _ with Model () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġ// ĠUse Ġa Ġlarger Ġcallback Ġtoken Ġso Ġthat Ġthe Ġevents Ġwe Ġsend Ġaren 't Ġfiltered Ġfrom Ġthe Ġlistener Ġwe ĊĠĠĠ Ġ// Ġattach Ġbelow . ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper (" me ", Ġactive Store , Ġcallback Token Ġ= Ġ 1 1 1 ) ĊĊĠĠĠ Ġval Ġ( entity Collection , Ġentity Collection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġentity Collection Helper . add ( bob ) ĊĊĠĠĠ Ġvar Ġsent Sync Request Ġ= Ġfalse ĊĠĠĠ Ġval Ġjob Ġ= ĠJob ( coroutine Context [ Job . Key ]) ĊĠĠĠ Ġvar Ġid : ĠInt Ġ= Ġ- 1 ĊĠĠĠ Ġid Ġ= Ġactive Store . on Ġ{ ĊĠĠĠĠĠ Ġif Ġ( it Ġis ĠProxy Message . Operations ) Ġ{ ĊĠĠĠĠĠĠĠ ĠassertThat ( sent Sync Request ). isFalse () ĊĠĠĠĠĠĠĠ Ġsent Sync Request Ġ= Ġtrue ĊĠĠĠĠĠĠĠ Ġactive Store . on Proxy Message ( Proxy Message . Sync Request ( id )) ĊĠĠĠĠĠĠĠ Ġreturn @ on ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ ĠassertThat ( sent Sync Request ). isTrue () ĊĠĠĠĠĠ Ġif Ġ( it Ġis ĠProxy Message . Model Update ) Ġ{ ĊĠĠĠĠĠĠĠ Ġit . model . values . assertEquals ( entity Collection . data . values ) ĊĠĠĠĠĠĠĠ Ġjob . complete () ĊĠĠĠĠĠĠĠ Ġreturn @ on ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġstore Helper . send Add Op ( bob ) ĊĠĠĠ Ġjob . join () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġonly S ends Model Response _ to Request ing Proxy () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġjob Ġ= ĠJob ( coroutine Context [ Job . Key ]) ĊĠĠĠ Ġ// Ġrequesting Ġstore ĊĠĠĠ Ġval Ġid 1 Ġ= Ġactive Store . on Ġ{ ĊĠĠĠĠĠ ĠassertThat ( it Ġis ĠProxy Message . Model Update ). isTrue () ĊĠĠĠĠĠ Ġjob . complete () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Ġanother Ġstore ĊĠĠĠ Ġvar Ġcalled Store 2 Ġ= Ġfalse ĊĠĠĠ Ġactive Store . on Ġ{ ĊĠĠĠĠĠ Ġcalled Store 2 Ġ= Ġtrue ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġactive Store . on Proxy Message ( Proxy Message . Sync Request ( id Ġ= Ġid 1 )) ĊĠĠĠ Ġjob . join () ĊĠĠĠ ĠassertThat ( called Store 2 ). isFalse () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġpropag ates Updates _ from Drivers _ to Prox ies () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġ( bob Collection , Ġbob Collection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġbob Collection Helper . add ( bob ) ĊĊĠĠĠ Ġval Ġ( reference Collection , Ġreference Collection Helper ) Ġ= Ġcreate Cr dt Set < Reference >(" me ") ĊĠĠĠ Ġval Ġbob Ref Ġ= Ġbob . to Reference ( ĊĠĠĠĠĠ Ġactive Store . back ing Store . storage Key , ĊĠĠĠĠĠ Ġbob Collection Helper . version Map ĊĠĠĠ Ġ) ĊĠĠĠ Ġreference Collection Helper . add ( bob Ref ) ĊĊĠĠĠ Ġval Ġbob Cr dt Ġ= Ġcreate Person Entity Cr dt () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġbob Cr dt Helper Ġ= ĠCr dt Entity Helper ( actor , Ġbob Cr dt ) ĊĠĠĠ Ġbob Cr dt Helper . update (" name ", ĠCr dt Entity . Reference . build Reference (" bob ". to Refer enc able ())) ĊĠĠĠ Ġbob Cr dt Helper . update (" age ", ĠCr dt Entity . Reference . build Reference ( 4 2 . 0 . to Refer enc able ())) ĊĊĠĠĠ Ġactive Store . back ing Store ĊĠĠĠĠĠ Ġ. on Proxy Message ( ĊĠĠĠĠĠĠĠ ĠM ux ed Proxy Message ( ĊĠĠĠĠĠĠĠĠĠ Ġ" an - id ", ĊĠĠĠĠĠĠĠĠĠ ĠProxy Message . Model Update ( bob Cr dt . data , Ġid Ġ= Ġactive Store . back ing Store Id ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġjob Ġ= ĠJob ( coroutine Context [ Job . Key ]) ĊĠĠĠ Ġactive Store . on Ġ{ ĊĠĠĠĠĠ Ġif Ġ( it Ġis ĠProxy Message . Model Update ) Ġ{ ĊĠĠĠĠĠĠĠ Ġit . model . values . assertEquals ( bob Collection . data . values ) ĊĠĠĠĠĠĠĠ Ġjob . complete () ĊĠĠĠĠĠĠĠ Ġreturn @ on ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġjob . complete Exception ally ( AssertionError (" Should Ġhave Ġreceived Ġmodel Ġupdate .")) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġdriver Ġ= Ġactive Store . container Store . driver Ġas ĠFake Driver < Cr dt Set . Data < Reference >> ĊĠĠĠ Ġdriver . last Receiver !! ( reference Collection . data , Ġ 1 ) ĊĠĠĠ Ġjob . join () ĊĠ Ġ} ĊĊĠ Ġ@ Ignore (" This Ġtest Ġcan Ġbe Ġenabled Ġwhen Ġwe Ġoutput Ġoperations Ġfrom Ġcollection Ġmodel Ġmerge s ") ĊĠ Ġ@ Test ĊĠ Ġfun Ġw ont Send An Update _ to Driver _ after Driver Origin ated Messages () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġreference Collection Ġ= ĠCr dt Set < Reference >() ĊĠĠĠ Ġval Ġreference Ġ= ĠReference (" an - id ", ĠMock Hierarchical Storage Key (), ĠVersion Map (" me " Ġto Ġ 1 )) ĊĠĠĠ Ġreference Collection . apply Operation ( ĊĠĠĠĠĠ ĠCr dt Set . Operation . Add (" me ", ĠVersion Map (" me " Ġto Ġ 1 ), Ġreference ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġdriver Ġ= Ġactive Store . container Store . driver Ġas ĠFake Driver < Cr dt Set . Data < Reference >> ĊĊĠĠĠ Ġdriver . last Receiver !! ( reference Collection . data , Ġ 1 ) ĊĊĠĠĠ ĠassertThat ( driver . last Data ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġres ends Failed Driver Updates _ after Mer ging () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġ// Ġlocal Ġmodel Ġfrom Ġproxy . ĊĠĠĠ Ġval Ġ( bob Collection , Ġbob Collection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġbob Collection Helper . add ( bob ) ĊĊĠĠĠ Ġ// Ġconflicting Ġremote Ġcount Ġfrom Ġstore ĊĠĠĠ Ġval Ġ( remote Collection , Ġremote Collection Helper ) Ġ= Ġcreate Cr dt Set < Reference >(" them ") ĊĠĠĠ Ġval Ġreference Ġ= ĊĠĠĠĠĠ ĠReference (" another - id ", ĠMock Hierarchical Storage Key (), ĠVersion Map (" them " Ġto Ġ 1 )) ĊĠĠĠ Ġremote Collection Helper . add ( reference ) ĊĊĠĠĠ Ġ// ĠEnsure Ġremote Ġentity Ġis Ġstored Ġin Ġbacking Ġstore . ĊĠĠĠ Ġactive Store . back ing Store . on Proxy Message ( ĊĠĠĠĠĠ ĠM ux ed Proxy Message ( ĊĠĠĠĠĠĠĠ Ġ" another - id ", ĊĠĠĠĠĠĠĠ ĠProxy Message . Model Update ( ĊĠĠĠĠĠĠĠĠĠ Ġcreate Person Entity Cr dt (). data , ĊĠĠĠĠĠĠĠĠĠ Ġid Ġ= Ġactive Store . back ing Store Id ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġdriver Ġ= Ġactive Store . container Store . driver Ġas ĠFake Driver < Cr dt Set . Data < Reference >> ĊĠĠĠ Ġdriver . send ReturnValue Ġ= Ġfalse Ġ// Ġmake Ġsending Ġreturn Ġfalse ĊĊĠĠĠ Ġactive Store . on Proxy Message ( ĊĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( bob Collection . data ), Ġid Ġ= Ġ 1 ) ĊĠĠĠ Ġ) ĊĠĠĠ ĠassertThat ( driver . last Data ). isNotNull () Ġ// Ġsend Ġshould 've Ġbeen Ġcalled . ĊĊĠĠĠ Ġdriver . last Data Ġ= Ġnull ĊĠĠĠ Ġdriver . send ReturnValue Ġ= Ġtrue Ġ// Ġmake Ġsending Ġwork . ĊĊĠĠĠ Ġdriver . last Receiver !! ( remote Collection . data , Ġ 1 ) ĊĠĠĠ ĠassertThat ( driver . last Data ). isNotNull () Ġ// Ġsend Ġshould 've Ġbeen Ġcalled Ġagain ĊĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĠĠĠ Ġval Ġref 2 Ġ= ĠReference (" an - id ", ĠMock Hierarchical Storage Key (), ĠVersion Map ( actor Ġto Ġ 1 )) ĊĠĠĠ Ġremote Collection . apply Operation ( Cr dt Set . Operation . Add (" me ", ĠVersion Map (" me " Ġto Ġ 1 ), Ġref 2 )) ĊĠĠĠ ĠassertThat ( driver . last Data ). isEqualTo ( remote Collection . data ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġresolves AC om bin ation _ of Messages _ from Proxy _ and Driver () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper (" me ", Ġactive Store ) ĊĊĠĠĠ Ġval Ġdriver Ġ= Ġactive Store . container Store . driver Ġas ĠFake Driver < Cr dt Set . Data < Reference >> ĊĊĠĠĠ Ġval Ġe 1 Ġ= Ġcreate Person Entity (" e 1 ", Ġ" e 1 ", Ġ 1 ) ĊĠĠĠ Ġval Ġe 2 Ġ= Ġcreate Person Entity (" e 2 ", Ġ" e 2 ", Ġ 2 ) ĊĠĠĠ Ġval Ġe 3 Ġ= Ġcreate Person Entity (" e 3 ", Ġ" e 3 ", Ġ 3 ) ĊĠĠĠ Ġstore Helper . send Add Op ( e 1 ) ĊĠĠĠ Ġstore Helper . send Add Op ( e 2 ) ĊĠĠĠ Ġstore Helper . send Add Op ( e 3 ) ĊĊĠĠĠ Ġval Ġe 1 Ref Ġ= ĠCr dt Set . Data Value ( ĊĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 ), ĊĠĠĠĠĠ ĠReference (" e 1 ", ĠMock Hierarchical Storage Key (), ĠVersion Map ()) ĊĠĠĠ Ġ) ĊĠĠĠ Ġval Ġt 1 Ref Ġ= ĠCr dt Set . Data Value ( ĊĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 , Ġ" them " Ġto Ġ 1 ), ĊĠĠĠĠĠ ĠReference (" t 1 ", ĠMock Hierarchical Storage Key (), ĠVersion Map ()) ĊĠĠĠ Ġ) ĊĠĠĠ Ġval Ġt 2 Ref Ġ= ĠCr dt Set . Data Value ( ĊĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 , Ġ" them " Ġto Ġ 2 ), ĊĠĠĠĠĠ ĠReference (" t 2 ", ĠMock Hierarchical Storage Key (), ĠVersion Map ()) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġdriver . last Receiver !! ( ĊĠĠĠĠĠ ĠCr dt Set . Data Impl ( ĊĠĠĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 , Ġ" them " Ġto Ġ 1 ), ĊĠĠĠĠĠĠĠ Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" e 1 " Ġto Ġe 1 Ref , ĊĠĠĠĠĠĠĠĠĠ Ġ" t 1 " Ġto Ġt 1 Ref ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠ 1 ĊĠĠĠ Ġ) ĊĠĠĠ Ġdriver . last Receiver !! ( ĊĠĠĠĠĠ ĠCr dt Set . Data Impl ( ĊĠĠĠĠĠĠĠ ĠVersion Map (" me " Ġto Ġ 1 , Ġ" them " Ġto Ġ 2 ), ĊĠĠĠĠĠĠĠ Ġmutable Map Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" e 1 " Ġto Ġe 1 Ref , ĊĠĠĠĠĠĠĠĠĠ Ġ" t 1 " Ġto Ġt 1 Ref , ĊĠĠĠĠĠĠĠĠĠ Ġ" t 2 " Ġto Ġt 2 Ref ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠ 2 ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġactive Store . idle () ĊĊĠĠĠ ĠassertThat ( active Store . container Store . getLocal Data ()) ĊĠĠĠĠĠ Ġ. isEqualTo ( driver . last Data ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġholds On to _ container Update _ until Backing Data Ar rives () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Reference Mode Store () ĊĠĠĠ Ġval Ġactor Ġ= Ġactive Store . cr dt Key ĊĊĠĠĠ Ġval Ġ( reference Collection , Ġreference Collection Helper ) Ġ= Ġcreate Cr dt Set < Reference >(" me ") ĊĠĠĠ Ġreference Collection Helper . add ( ĊĠĠĠĠĠ ĠReference (" an - id ", Ġactive Store . back ing Store . storage Key , ĠVersion Map ( actor Ġto Ġ 1 )) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġjob Ġ= ĠJob ( coroutine Context [ Job . Key ]) ĊĠĠĠ Ġvar Ġbacking Store Sent Ġ= Ġfalse ĊĠĠĠ Ġval Ġid Ġ= Ġactive Store . on Ġ{ ĊĠĠĠĠĠ Ġif Ġ(! back ing Store Sent ) Ġ{ ĊĠĠĠĠĠĠĠ Ġjob . complete Exception ally ( ĊĠĠĠĠĠĠĠĠĠ ĠAssertionError (" Backing Ġstore Ġdata Ġshould 've Ġbeen Ġsent Ġfirst .") ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġif Ġ( it Ġis ĠProxy Message . Model Update ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġentity Record Ġ= Ġrequire NotNull ( it . model . values [" an - id "] ?. value ) ĊĠĠĠĠĠĠĠ ĠassertThat ( entity Record . sing letons [" name "] ?. id ) ĊĠĠĠĠĠĠĠĠĠ Ġ. isEqualTo (" bob ". to Refer enc able (). id ) ĊĠĠĠĠĠĠĠ ĠassertThat ( entity Record . sing letons [" age "] ?. id ) ĊĠĠĠĠĠĠĠĠĠ Ġ. isEqualTo ( 4 2 . 0 . to Refer enc able (). id ) ĊĠĠĠĠĠĠĠ Ġjob . complete () ĊĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġjob . complete Exception ally ( AssertionError (" Invalid ĠProxy Message Ġtype Ġreceived ")) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġcontainer Job Ġ= Ġlaunch Ġ{ ĊĠĠĠĠĠ Ġactive Store . container Store . on Receive ( reference Collection . data , Ġid Ġ+ Ġ 1 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġcontainer Job . join () ĊĊĠĠĠ Ġbacking Store Sent Ġ= Ġtrue ĊĊĠĠĠ Ġval Ġentity Cr dt Ġ= Ġcreate Person Entity Cr dt () ĊĠĠĠ Ġval Ġcr dt Entity Helper Ġ= ĠCr dt Entity Helper ( actor , Ġentity Cr dt ) ĊĠĠĠ Ġcr dt Entity Helper . update (" name ", ĠCr dt Entity . Reference . build Reference (" bob ". to Refer enc able ())) ĊĠĠĠ Ġcr dt Entity Helper . update (" age ", ĠCr dt Entity . Reference . build Reference ( 4 2 . 0 . to Refer enc able ())) ĊĊĠĠĠ Ġval Ġbacking Store Ġ= Ġactive Store . back ing Store . get Store (" an - id ", Ġactive Store . back ing Store Id ) ĊĠĠĠ Ġbacking Store . store . on Receive ( entity Cr dt . data , Ġid Ġ+ Ġ 2 ) ĊĊĠĠĠ Ġactive Store . idle () ĊĊĠĠĠ Ġjob . join () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġbacking Stores Clean ed Up When Last Callback Removed () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ Ġval Ġstore Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġtoken Ġ= Ġstore . on Ġ{} ĊĊĠĠĠ Ġval Ġ( collection , Ġcollection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġentity Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġcollection Helper . add ( entity ) ĊĊĠĠĠ Ġstore . on Proxy Message ( ĊĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( collection . data ), Ġ 1 ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġstore . off ( token ) ĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 0 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġbacking Stores Clean ed Up When Last Callback Removed Tw ice () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ Ġval Ġstore Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġpre Token Ġ= Ġstore . on Ġ{} ĊĠĠĠ Ġstore . off ( pre Token ) ĊĊĠĠĠ Ġval Ġtoken Ġ= Ġstore . on Ġ{} ĊĠĠĠ Ġval Ġ( collection , Ġcollection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠ Ġval Ġentity Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġcollection Helper . add ( entity ) ĊĊĠĠĠ Ġstore . on Proxy Message ( ĊĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( collection . data ), Ġ 1 ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 1 ) ĊĊĠĠĠ Ġstore . off ( token ) ĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 0 ) ĊĊĠĠĠ Ġval Ġtoken 2 Ġ= Ġstore . on Ġ{} ĊĠĠĠ Ġstore . on Proxy Message ( ĊĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( collection . data ), Ġ 1 ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 1 ) ĊĊĠĠĠ Ġstore . off ( token 2 ) ĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 0 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġclose _ clo ses Backing And Container Stores () Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠ Ġval Ġactive Store Ġ= Ġcreate Singleton Reference Mode Store () ĊĠĠĠ Ġval Ġstore Helper Ġ= ĠRef Mode Store Helper (" me ", Ġactive Store ) ĊĊĠĠĠ Ġ// ĠSet Ġsingleton Ġto ĠBob . ĊĠĠĠ Ġval Ġbob Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob ", Ġ 4 2 ) ĊĠĠĠ Ġstore Helper . send Update Op ( bob ) ĊĊĠĠĠ ĠassertThat ( active Store . container Store . closed ). isFalse () ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores ). has Size ( 1 ) ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores . values . single (). store . closed ). isFalse () ĊĊĠĠĠ Ġactive Store . close () ĊĊĠĠĠ ĠassertThat ( active Store . container Store . closed ). isTrue () ĊĠĠĠ ĠassertThat ( active Store . back ing Store . stores ). isEmpty () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġbacking Stores Clean ed Up When Last Callback Removed R aces () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ Ġval Ġstore Ġ= Ġcreate Reference Mode Store () ĊĊĠĠĠ Ġval Ġcallback Job Ġ= Ġlaunch Ġ{ ĊĠĠĠĠĠ Ġfor Ġ( i Ġin Ġ 0 .. 1 0 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġpre Token Ġ= Ġstore . on Ġ{} ĊĠĠĠĠĠĠĠ Ġdelay ( 1 ) ĊĠĠĠĠĠĠĠ Ġstore . off ( pre Token ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġdata Job Ġ= Ġlaunch Ġ{ ĊĠĠĠĠĠ Ġfor Ġ( i Ġin Ġ 0 .. 1 0 0 ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġ( collection , Ġcollection Helper ) Ġ= Ġcreate Cr dt Set < Raw Entity >(" me ") ĊĠĠĠĠĠĠĠ Ġval Ġentity Ġ= Ġcreate Person Entity (" an - id ", Ġ" bob -$ i ", Ġ 4 2 ) ĊĠĠĠĠĠĠĠ Ġcollection Helper . add ( entity ) ĊĊĠĠĠĠĠĠĠ Ġstore . on Proxy Message ( ĊĠĠĠĠĠĠĠĠĠ ĠProxy Message . Model Update ( Ref Mode Store Data . Set ( collection . data ), Ġ 1 ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġcallback Job . join () ĊĠĠĠ Ġdata Job . join () ĊĊĠĠĠ Ġstore . idle () ĊĠĠĠ ĠassertThat ( store . back ing Store . stores . size ). isEqualTo ( 0 ) ĊĠ Ġ} ĊĊĠ Ġ// Ġregion ĠHelpers ĊĊĠ Ġprivate Ġsuspend Ġfun ĠCor outine Scope . create Reference Mode Store (): ĠReference Mode Store Ġ{ ĊĠĠĠ Ġreturn ĠReference Mode Store . create ( ĊĠĠĠĠĠ ĠStore Options ( ĊĠĠĠĠĠĠĠ Ġtest Key , ĊĠĠĠĠĠĠĠ ĠCollection Type ( EntityType ( schema )) ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġthis , ĊĠĠĠĠĠ Ġdriver Factory , ĊĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġsuspend Ġfun ĠCor outine Scope . create Singleton Reference Mode Store (): ĠReference Mode Store Ġ{ ĊĠĠĠ Ġreturn ĠReference Mode Store . create ( ĊĠĠĠĠĠ ĠStore Options ( ĊĠĠĠĠĠĠĠ Ġtest Key , ĊĠĠĠĠĠĠĠ ĠSingleton Type ( EntityType ( schema )) ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġthis , ĊĠĠĠĠĠ Ġdriver Factory , ĊĠĠĠĠĠ Ġ:: test Write Back Provider , ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcreate Person Entity ( id : ĠReference Id , Ġname : ĠString , Ġage : ĠInt ): ĠRaw Entity Ġ= ĠRaw Entity ( ĊĠĠĠ Ġid Ġ= Ġid , ĊĠĠĠ Ġsing letons Ġ= Ġmap Of ( ĊĠĠĠĠĠ Ġ" name " Ġto Ġname . to Refer enc able (), ĊĠĠĠĠĠ Ġ" age " Ġto Ġage . toDouble (). to Refer enc able () ĊĠĠĠ Ġ) ĊĠ Ġ) ĊĊĠ Ġprivate Ġfun Ġcreate Empty Person Entity ( id : ĠReference Id ): ĠRaw Entity Ġ= ĠRaw Entity ( ĊĠĠĠ Ġid Ġ= Ġid , ĊĠĠĠ Ġsing letons Ġ= Ġmap Of ( ĊĠĠĠĠĠ Ġ" name " Ġto Ġnull , ĊĠĠĠĠĠ Ġ" age " Ġto Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ) ĊĊĠ Ġprivate Ġfun Ġcreate Person Entity Cr dt (): ĠCr dt Entity Ġ= ĠCr dt Entity ( ĊĠĠĠ ĠVersion Map (), ĊĠĠĠ ĠRaw Entity ( singleton Fields Ġ= Ġset Of (" name ", Ġ" age ")) ĊĠ Ġ) ĊĊĠ Ġ/** ĠConstructs Ġa Ġnew Ġ[ Cr dt Set ] Ġpaired Ġwith Ġa Ġ[ Cr dt Set Helper ]. Ġ*/ ĊĠ Ġprivate Ġfun Ġ< T Ġ: ĠRefer enc able > Ġcreate Cr dt Set ( actor : ĠString ): ĠPair < Cr dt Set < T >, ĠCr dt Set Helper < T >> Ġ{ ĊĠĠĠ Ġval Ġcollection Ġ= ĠCr dt Set < T >() ĊĠĠĠ Ġval Ġcollection Helper Ġ= ĠCr dt Set Helper ( actor , Ġcollection ) ĊĠĠĠ Ġreturn Ġcollection Ġto Ġcollection Helper ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠAssert s Ġthat Ġthe Ġreceiving Ġmap Ġof Ġentities Ġ( values Ġfrom Ġa ĠCr dt Set / Cr dt Singleton ) Ġare Ġequal Ġto ĊĠĠ Ġ* Ġthe Ġ[ other ] Ġmap Ġof Ġentities , Ġon Ġan ĠID - basis . ĊĠĠ Ġ*/ ĊĠ Ġprivate Ġfun ĠMap < Reference Id , ĠCr dt Set . Data Value < Raw Entity >> . assertEquals ( ĊĠĠĠ Ġother : ĠMap < Reference Id , ĠCr dt Set . Data Value < Raw Entity >> ĊĠ Ġ) Ġ{ ĊĠĠĠ ĠassertThat ( keys ). isEqualTo ( other . keys ) ĊĠĠĠ ĠforEach Ġ{ Ġ( ref Id , Ġmy Entity ) Ġ-> ĊĠĠĠĠĠ Ġval Ġother Entity Ġ= Ġrequire NotNull ( other [ ref Id ]) ĊĠĠĠĠĠ Ġ// ĠShould Ġhave Ġsame Ġfields . ĊĠĠĠĠĠ ĠassertThat ( my Entity . value . sing letons . keys ) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( other Entity . value . sing letons . keys ) ĊĠĠĠĠĠ ĠassertThat ( my Entity . value . collections . keys ) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( other Entity . value . collections . keys ) ĊĊĠĠĠĠĠ Ġmy Entity . value . sing letons . forEach Ġ{ Ġ( field , Ġvalue ) Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġother Value Ġ= Ġother Entity . value . sing letons [ field ] ĊĠĠĠĠĠĠĠ ĠassertThat ( value ?. id ). isEqualTo ( other Value ?. id ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġmy Entity . value . collections . forEach Ġ{ Ġ( field , Ġvalue ) Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġother Value Ġ= Ġother Entity . value . collections [ field ] ĊĠĠĠĠĠĠĠ ĠassertThat ( value . size ). isEqualTo ( other Value ?. size ) ĊĠĠĠĠĠĠĠ ĠassertThat ( value . map Ġ{ Ġit . id Ġ}. toSet ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. isEqualTo ( other Value ?. map Ġ{ Ġit . id Ġ} ?. toSet ()) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ// Ġend region ĊĊĠ Ġ// Ġregion ĠMock s ĊĊĠ Ġprivate Ġdata Ġclass ĠMock Hierarchical Storage Key ( ĊĠĠĠ Ġprivate Ġval Ġsegment : ĠString Ġ= Ġ"" ĊĠ Ġ) Ġ: ĠStorage Key (" testing - hierarchy ") Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġto Key String (): ĠString Ġ= Ġsegment ĊĊĠĠĠ Ġoverride Ġfun Ġchild Key With Component ( component : ĠString ): ĠStorage Key Ġ= ĊĠĠĠĠĠ ĠMock Hierarchical Storage Key ("$ segment $ component ") ĊĠ Ġ} ĊĊĠ Ġ// Ġend region Ċ } Ċ