6464import java .util .concurrent .atomic .AtomicLong ;
6565import java .util .concurrent .atomic .AtomicReference ;
6666
67+ import javax .annotation .Nullable ;
68+
6769import io .realm .entities .AllJavaTypes ;
6870import io .realm .entities .AllTypes ;
6971import io .realm .entities .AllTypesPrimaryKey ;
@@ -1060,15 +1062,24 @@ public void compactRealm_onExternalStorage() {
10601062 Realm .deleteRealm (config );
10611063 }
10621064
1065+ private void populateTestRealmForCompact (Realm realm , int sizeInMB ) {
1066+ byte [] oneMBData = new byte [1024 * 1024 ];
1067+ realm .beginTransaction ();
1068+ for (int i = 0 ; i < sizeInMB ; i ++) {
1069+ realm .createObject (AllTypes .class ).setColumnBinary (oneMBData );
1070+ }
1071+ realm .commitTransaction ();
1072+ }
1073+
10631074 private Pair <Long , Long > populateTestRealmAndCompactOnLaunch (CompactOnLaunchCallback compactOnLaunch ) {
1064- return populateTestRealmAndCompactOnLaunch (compactOnLaunch , 100 );
1075+ return populateTestRealmAndCompactOnLaunch (compactOnLaunch , 1 );
10651076 }
10661077
1067- private Pair <Long , Long > populateTestRealmAndCompactOnLaunch (CompactOnLaunchCallback compactOnLaunch , int objects ) {
1078+ private Pair <Long , Long > populateTestRealmAndCompactOnLaunch (CompactOnLaunchCallback compactOnLaunch , int sizeInMB ) {
10681079 final String REALM_NAME = "test.realm" ;
10691080 RealmConfiguration realmConfig = configFactory .createConfiguration (REALM_NAME );
10701081 Realm realm = Realm .getInstance (realmConfig );
1071- populateTestRealm (realm , objects );
1082+ populateTestRealmForCompact (realm , sizeInMB );
10721083 realm .beginTransaction ();
10731084 realm .deleteAll ();
10741085 realm .commitTransaction ();
@@ -1131,12 +1142,23 @@ public boolean shouldCompact(long totalBytes, long usedBytes) {
11311142 })
11321143 .build ();
11331144 Realm realm = Realm .getInstance (realmConfig );
1145+ realm .close ();
1146+ // WARNING: We need to init the schema first and close the Realm to make sure the relevant logic works in Object
1147+ // Store. See https://github.com/realm/realm-object-store/blob/master/src/shared_realm.cpp#L58
1148+ // Called once.
1149+ assertEquals (1 , compactOnLaunchCount .get ());
1150+
1151+ realm = Realm .getInstance (realmConfig );
1152+ // Called 2 more times. The PK table migration logic (the old PK bug) needs to open/close the Realm once.
1153+ assertEquals (3 , compactOnLaunchCount .get ());
11341154
11351155 Thread thread = new Thread (new Runnable () {
11361156 @ Override
11371157 public void run () {
11381158 Realm bgRealm = Realm .getInstance (realmConfig );
11391159 bgRealm .close ();
1160+ // compactOnLaunch should not be called anymore!
1161+ assertEquals (3 , compactOnLaunchCount .get ());
11401162 }
11411163 });
11421164 thread .start ();
@@ -1149,8 +1171,6 @@ public void run() {
11491171
11501172 realm .close ();
11511173
1152- // FIXME: It should be 1. Current compactOnLaunch is called each time a Realm is opened on a new thread.
1153- assertNotEquals (1 , compactOnLaunchCount .get ());
11541174 assertEquals (3 , compactOnLaunchCount .get ());
11551175 }
11561176
@@ -1162,7 +1182,7 @@ public boolean shouldCompact(long totalBytes, long usedBytes) {
11621182 final long thresholdSize = 50 * 1024 * 1024 ;
11631183 return (totalBytes > thresholdSize ) && (((double ) usedBytes / (double ) totalBytes ) < 0.5 );
11641184 }
1165- }, 100 );
1185+ }, 1 );
11661186 final long thresholdSize = 50 * 1024 * 1024 ;
11671187 assertTrue (results .first < thresholdSize );
11681188 assertEquals (results .first , results .second );
@@ -1195,7 +1215,7 @@ public boolean shouldCompact(long totalBytes, long usedBytes) {
11951215
11961216 @ Test
11971217 public void defaultCompactOnLaunch () throws IOException {
1198- Pair <Long , Long > results = populateTestRealmAndCompactOnLaunch (null , 30000 );
1218+ Pair <Long , Long > results = populateTestRealmAndCompactOnLaunch (null , 50 );
11991219 final long thresholdSize = 50 * 1024 * 1024 ;
12001220 assertTrue (results .first > thresholdSize );
12011221 assertTrue (results .first > results .second );
@@ -1215,7 +1235,7 @@ public void defaultCompactOnLaunch_onlyCallback() {
12151235
12161236 @ Test
12171237 public void defaultCompactOnLaunch_insufficientAmount () throws IOException {
1218- Pair <Long , Long > results = populateTestRealmAndCompactOnLaunch (null , 100 );
1238+ Pair <Long , Long > results = populateTestRealmAndCompactOnLaunch (null , 1 );
12191239 final long thresholdSize = 50 * 1024 * 1024 ;
12201240 assertTrue (results .first < thresholdSize );
12211241 assertEquals (results .first , results .second );
0 commit comments