Skip to content

Commit

Permalink
(#1166) - web workers
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Dec 22, 2013
1 parent c2cb80c commit 8f73143
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bin/test-node.js
Expand Up @@ -8,6 +8,8 @@ var excludedTests = [
// auth_replication and cors need admin access (#1030)
'test.auth_replication.js',
'test.cors.js',
//no workers in node
'test.worker.js',
// Plugins currnetly arent tested (#1031)
'test.gql.js',
'test.spatial.js'
Expand Down
2 changes: 1 addition & 1 deletion lib/deps/ajax.js
Expand Up @@ -78,7 +78,7 @@ function ajax(options, callback) {
call(cb, errObj);
};

if (typeof window !== 'undefined' && window.XMLHttpRequest) {
if (process.browser && typeof XMLHttpRequest !== 'undefined') {
var timer, timedout = false;
var xhr = new XMLHttpRequest();

Expand Down
32 changes: 32 additions & 0 deletions tests/test.worker.js
@@ -0,0 +1,32 @@
QUnit.module('worker');

asyncTest('create it',1,function(){
var worker = new Worker('worker.js');
worker.addEventListener('message',function(e){
ok('pong',e.data);
worker.terminate();
start();
});
worker.postMessage('ping');
});
asyncTest('check pouch version',1,function(){
var worker = new Worker('worker.js');
worker.addEventListener('message',function(e){
ok(PouchDB.version,e.data);
worker.terminate();
start();
});
worker.postMessage('version');
});
asyncTest('create db',1,function(){
var worker = new Worker('worker.js');
worker.addEventListener('error',function(e){
throw e;
});
worker.addEventListener('message',function(e){
ok('lala',e.data);
worker.terminate();
start();
});
worker.postMessage(['create',testUtils.generateAdapterUrl(adapter)]);
});
3 changes: 2 additions & 1 deletion tests/webrunner.js
Expand Up @@ -14,7 +14,8 @@ if (!testFiles.length) {
'test.replication.js', 'test.views.js', 'test.taskqueue.js',
'test.design_docs.js', 'test.issue221.js', 'test.http.js',
'test.compaction.js', 'test.get.js',
'test.attachments.js', 'test.uuids.js', 'test.slash_id.js'
'test.attachments.js', 'test.uuids.js', 'test.slash_id.js',
'test.worker.js'
];
}

Expand Down
31 changes: 31 additions & 0 deletions tests/worker.js
@@ -0,0 +1,31 @@
importScripts('../dist/pouchdb-nightly.js');
function bigTest(name){
PouchDB(name,function(err,db){
if(err){
throw err;
}
db.post({_id:"blablah",key:'lala'},function(err){
if(err){
throw err;
}
db.get('blablah',function(err,doc){
if(err){
throw err;
}
self.postMessage(doc.key);
PouchDB.destroy(name);
});
});
});
}
self.addEventListener('message',function(e){
if(e.data==='ping'){
self.postMessage('pong');
}
if(e.data==='version'){
self.postMessage(PouchDB.version);
}
if(Array.isArray(e.data)&&e.data[0]==='create'){
bigTest(e.data[1]);
}
});

0 comments on commit 8f73143

Please sign in to comment.