diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index 6d9a7a6e..bf1e753f 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -74,6 +74,23 @@ function promisifiedLogin(server, username, password) { }); } +function runOutOfProcess(nodeJsFilePath) { + var nodeArgs = Array.prototype.slice.call(arguments); + let tmpDir = tmp.dirSync(); + let content = fs.readFileSync(nodeJsFilePath, 'utf8'); + let tmpFile = tmp.fileSync({ dir: tmpDir.name }); + fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); + nodeArgs[0] = tmpFile.name; + return new Promise((resolve, reject) => { + const child = execFile('node', nodeArgs, { cwd: tmpDir.name }, (error, stdout, stderr) => { + if (error) { + reject(new Error(`Error executing ${nodeJsFilePath} Error: ${error}`)); + } + resolve(); + }); + }) +} + module.exports = { testLocalRealmHasNoSession() { @@ -131,44 +148,31 @@ module.exports = { const realmName = uuid(); const expectedObjectsCount = 3; - let tmpDir = tmp.dirSync(); - let content = fs.readFileSync(__dirname + '/download-api-helper.js', 'utf8'); - let tmpFile = tmp.fileSync({ dir: tmpDir.name }); - fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); + runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) + .then(() => { + return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { + const accessTokenRefreshed = this; + let successCounter = 0; - return new Promise((resolve, reject) => { - //execute download-api-helper which inserts predefined number of objects into the synced realm. - const child = execFile('node', [tmpFile.name, username, realmName, REALM_MODULE_PATH], { cwd: tmpDir.name }, (error, stdout, stderr) => { - if (error) { - reject(new Error('Error executing download api helper' + error)); - } - resolve(); + let config = { + sync: { user, url: `realm://localhost:9080/~/${realmName}` }, + schema: [{ name: 'Dog', properties: { name: 'string' } }], + }; + + 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'); + }); + }); }); - }) - .then(() => { - return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { - const accessTokenRefreshed = this; - let successCounter = 0; - - let config = { - sync: { user, url: `realm://localhost:9080/~/${realmName}` }, - schema: [{ name: 'Dog', properties: { name: 'string' } }], - }; - - 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'); - }); - }); - }); }, testRealmOpenAsync() { @@ -180,20 +184,7 @@ module.exports = { const realmName = uuid(); const expectedObjectsCount = 3; - let tmpDir = tmp.dirSync(); - let content = fs.readFileSync(__dirname + '/download-api-helper.js', 'utf8'); - let tmpFile = tmp.fileSync({ dir: tmpDir.name }); - fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); - - return new Promise((resolve, reject) => { - //execute download-api-helper which inserts predefined number of objects into the synced realm. - const child = execFile('node', [tmpFile.name, username, realmName, REALM_MODULE_PATH], { cwd: tmpDir.name }, (error, stdout, stderr) => { - if (error) { - reject(new Error('Error executing download api helper' + error)); - } - resolve(); - }); - }) + runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) .then(() => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return new Promise((resolve, reject) => { @@ -246,43 +237,30 @@ module.exports = { const realmName = uuid(); const expectedObjectsCount = 3; - let tmpDir = tmp.dirSync(); - let content = fs.readFileSync(__dirname + '/download-api-helper.js', 'utf8'); - let tmpFile = tmp.fileSync({ dir: tmpDir.name }); - fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); - - return new Promise((resolve, reject) => { - //execute download-api-helper which inserts predefined number of objects into the synced realm. - const child = execFile('node', [tmpFile.name, username, realmName, REALM_MODULE_PATH], { cwd: tmpDir.name }, (error, stdout, stderr) => { - if (error) { - reject(new Error('testProgressNotifications: Error executing download api helper for Realm.open ' + error)); - } - resolve(); - }); - }) - .then(() => { - return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { - const accessTokenRefreshed = this; - let successCounter = 0; - let progressNotificationCalled = false; - let config = { - sync: { - user, - url: `realm://localhost:9080/~/${realmName}`, - _onProgress: (transferred, remaining) => { - progressNotificationCalled = true + runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) + .then(() => { + return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { + const accessTokenRefreshed = this; + let successCounter = 0; + let progressNotificationCalled = false; + let config = { + sync: { + user, + url: `realm://localhost:9080/~/${realmName}`, + _onProgress: (transferred, remaining) => { + progressNotificationCalled = true + }, }, - }, - schema: [{ name: 'Dog', properties: { name: 'string' } }], - }; + schema: [{ name: 'Dog', properties: { name: 'string' } }], + }; - return Realm.open(config) - .then(realm => { - }).then(session => { - TestCase.assertTrue(progressNotificationCalled, "Progress notification not called for Realm.open"); - }); + return Realm.open(config) + .then(realm => { + }).then(session => { + TestCase.assertTrue(progressNotificationCalled, "Progress notification not called for Realm.open"); + }); + }); }); - }); }, testProgressNotificationsForRealmOpenAsync() { @@ -294,20 +272,7 @@ module.exports = { const realmName = uuid(); const expectedObjectsCount = 3; - let tmpDir = tmp.dirSync(); - let content = fs.readFileSync(__dirname + '/download-api-helper.js', 'utf8'); - let tmpFile = tmp.fileSync({ dir: tmpDir.name }); - fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); - - return new Promise((resolve, reject) => { - //execute download-api-helper which inserts predefined number of objects into the synced realm. - const child = execFile('node', [tmpFile.name, username, realmName, REALM_MODULE_PATH], { cwd: tmpDir.name }, (error, stdout, stderr) => { - if (error) { - reject(new Error('testProgressNotifications: Error executing download api helper for Realm.openAsync ' + error)); - } - resolve(); - }); - }) + runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) .then(() => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return new Promise((resolve, reject) => { @@ -354,20 +319,7 @@ module.exports = { const realmName = uuid(); const expectedObjectsCount = 3; - let tmpDir = tmp.dirSync(); - let content = fs.readFileSync(__dirname + '/download-api-helper.js', 'utf8'); - let tmpFile = tmp.fileSync({ dir: tmpDir.name }); - fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); - - return new Promise((resolve, reject) => { - //execute download-api-helper which inserts predefined number of objects into the synced realm. - const child = execFile('node', [tmpFile.name, username, realmName, REALM_MODULE_PATH], { cwd: tmpDir.name }, (error, stdout, stderr) => { - if (error) { - reject(new Error('Error executing download api helper' + error)); - } - resolve(); - }); - }) + runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) .then(() => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return new Promise((resolve, reject) => {