Very basic multiprocess change notification test

This commit is contained in:
Scott Kyle 2016-04-26 02:04:13 -07:00
parent b42f5ab285
commit 17e7153a59
4 changed files with 181 additions and 0 deletions

View File

@ -28,6 +28,11 @@ const mockery = require('mockery');
function runTests() { function runTests() {
const Realm = require('realm'); const Realm = require('realm');
const RealmTests = require('./js'); const RealmTests = require('./js');
RealmTests.registerTests({
WorkerTests: require('./js/worker-tests'),
});
const testNames = RealmTests.getTestNames(); const testNames = RealmTests.getTestNames();
let passed = true; let passed = true;

View File

@ -0,0 +1,56 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
/* eslint-env es6, node */
/* eslint-disable no-console */
'use strict';
const Realm = require('../..');
const handlers = {
create(options) {
let realm = new Realm(options.config);
realm.write(() => {
realm.create(options.type, options.properties);
});
}
};
process.on('message', (message) => {
process.send(handleMessage(message));
});
function handleMessage(message) {
let error, result;
try {
let handler = handlers[message.action];
if (handler) {
result = handler(message);
} else {
throw new Error('Unknown worker action: ' + message.action);
}
} catch (e) {
console.warn(e);
error = e.message;
}
return {error, result};
}

76
tests/js/worker-tests.js Normal file
View File

@ -0,0 +1,76 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
/* eslint-env es6, node */
'use strict';
const Realm = require('realm');
const TestCase = require('./asserts');
const schemas = require('./schemas');
const Worker = require('./worker');
module.exports = {
testChangeNotifications() {
return new Promise((resolve, reject) => {
let config = {schema: [schemas.TestObject]};
let realm = new Realm(config);
let objects = realm.objects('TestObject');
let worker = new Worker(__dirname + '/worker-tests-script.js');
// Test will fail if it does not receive a change event within a second.
let timer = setTimeout(() => {
reject(new Error('Timed out waiting for change notification'));
}, 1000);
let cleanup = () => {
clearTimeout(timer);
worker.terminate();
};
// Test will pass if it receives a change event and the Realm changed.
realm.addListener('change', () => {
try {
TestCase.assertEqual(objects.length, 1);
TestCase.assertEqual(objects[0].doubleCol, 42);
resolve();
} catch (e) {
reject(e);
} finally {
cleanup();
}
});
worker.onmessage = (message) => {
if (message.error) {
cleanup();
reject(message.error);
}
};
worker.postMessage({
action: 'create',
config: config,
type: 'TestObject',
properties: {
doubleCol: 42,
}
});
});
}
};

44
tests/js/worker.js Normal file
View File

@ -0,0 +1,44 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
/* eslint-env es6, node */
'use strict';
class Worker {
constructor(script) {
this._process = require('child_process').fork(script);
this._process.on('message', (message) => {
if (this.onmessage) {
this.onmessage(message);
}
});
}
postMessage(message) {
this._process.send(message);
}
terminate() {
if (this._process) {
this._process.kill();
delete this._process;
}
}
}
module.exports = Worker;