From 28fe678a1b2a98f44de7bbefc09bdab2fedc2fd3 Mon Sep 17 00:00:00 2001 From: Yavor Georgiev Date: Mon, 19 Jun 2017 13:58:19 +0200 Subject: [PATCH] Use an uncached realm instance for _waitForDownload (#1083) Fixes #1061 --- src/js_realm.hpp | 3 +- tests/js/session-tests.js | 59 ++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 17a57b7c..cf051fac 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -550,6 +550,7 @@ void RealmClass::wait_for_download_completion(ContextType ctx, FunctionType, ValueType sync_config_value = Object::get_property(ctx, config_object, "sync"); if (!Value::is_undefined(ctx, sync_config_value)) { realm::Realm::Config config; + config.cache = false; static const String encryption_key_string = "encryptionKey"; ValueType encryption_key_value = Object::get_property(ctx, config_object, encryption_key_string); if (!Value::is_undefined(ctx, encryption_key_value)) { @@ -590,7 +591,7 @@ void RealmClass::wait_for_download_completion(ContextType ctx, FunctionType, if (user && user->state() != SyncUser::State::Error) { if (auto session = user->session_for_on_disk_path(config.path)) { session->wait_for_download_completion([=](std::error_code error_code) { - realm->config(); //capture and keep realm instance for till here + realm->close(); //capture and keep realm instance for till here waitFunc(error_code); }); return; diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index e36cfe3f..cccbf48f 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -147,29 +147,26 @@ module.exports = { }) .then(() => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { - return new Promise((resolve, reject) => { - const accessTokenRefreshed = this; - let successCounter = 0; + const accessTokenRefreshed = this; + let successCounter = 0; - let config = { - sync: { user, url: `realm://localhost:9080/~/${realmName}` }, - schema: [{ name: 'Dog', properties: { name: 'string' } }], - }; + let config = { + sync: { user, url: `realm://localhost:9080/~/${realmName}` }, + schema: [{ name: 'Dog', properties: { name: 'string' } }], + }; - Realm.open(config) - .then(realm => { - let actualObjectsCount = realm.objects('Dog').length; - TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count"); - - const session = realm.syncSession; - TestCase.assertInstanceOf(session, Realm.Sync.Session); - TestCase.assertEqual(session.user.identity, user.identity); - TestCase.assertEqual(session.config.url, config.sync.url); - TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); - TestCase.assertEqual(session.state, 'active'); - resolve(); - }).catch(e => { reject(e) }); - }); + return Realm.open(config) + .then(realm => { + let actualObjectsCount = realm.objects('Dog').length; + TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count"); + return realm.syncSession; + }).then(session => { + TestCase.assertInstanceOf(session, Realm.Sync.Session); + TestCase.assertEqual(session.user.identity, user.identity); + TestCase.assertEqual(session.config.url, config.sync.url); + TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); + TestCase.assertEqual(session.state, 'active'); + }); }); }); }, @@ -217,13 +214,19 @@ module.exports = { let actualObjectsCount = realm.objects('Dog').length; TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count"); - const session = realm.syncSession; - TestCase.assertInstanceOf(session, Realm.Sync.Session); - TestCase.assertEqual(session.user.identity, user.identity); - TestCase.assertEqual(session.config.url, config.sync.url); - TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); - TestCase.assertEqual(session.state, 'active'); - resolve(); + setTimeout(() => { + try { + const session = realm.syncSession; + TestCase.assertInstanceOf(session, Realm.Sync.Session); + TestCase.assertEqual(session.user.identity, user.identity); + TestCase.assertEqual(session.config.url, config.sync.url); + TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); + TestCase.assertEqual(session.state, 'active'); + resolve(); + } catch (e) { + reject(e); + } + }, 50); } catch (e) { reject(e);