Skip to content

Commit

Permalink
FIX #714 checkAdapter does not cleanup test databases
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Aug 24, 2018
1 parent 5723514 commit d9717ff
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Breaking:
- QueryChangeDetection is not enabled in the RxDatabase-options `queryChangeDetection: true`
- Setters are only callable on temporary documents

Bugfixes:
- checkAdapter doesn't cleanup test databases [#714](https://github.com/pubkey/rxdb/issues/714)

Other:
- cross-instance communication is now done with https://github.com/pubkey/broadcast-channel (way better performance)
- Upgrade to eslint 5 (no more babel-eslint)
Expand Down
43 changes: 33 additions & 10 deletions src/plugins/adapter-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,58 @@ import {
adapterObject
} from '../util';

/**
* The same pouchdb-location is used on each run
* To ensure when this is run multiple times,
* there will not be many created databases
*/
export const POUCHDB_LOCATION = 'rxdb-adapter-check';

export async function checkAdapter(adapter) {
const id = 'rxdb-test-adapter-' + generateId();
// id of the document which is stored and removed to ensure everything works
const _id = POUCHDB_LOCATION + '-' + generateId();
let recoveredDoc = null;
let pouch;
try {
pouch = new PouchDB(
id,
const pouch = new PouchDB(
POUCHDB_LOCATION,
adapterObject(adapter), {
auto_compaction: false, // no compaction because this only stores local documents
auto_compaction: true,
revs_limit: 1
}
);
await pouch.info(); // ensure that we wait until db is useable

// ensure write works
await pouch.put({
_id: id,
value: true
_id,
value: {
ok: true,
time: new Date().getTime()
}
});
recoveredDoc = await pouch.get(id);

// cleanup
// ensure read works
recoveredDoc = await pouch.get(_id);

// ensure remove works
await pouch.remove(recoveredDoc);
} catch (err) {
return false;
}

if (recoveredDoc && recoveredDoc.value)
if (recoveredDoc && recoveredDoc.value && recoveredDoc.value.ok)
return true;
else
return false;

/**
* NOTICE:
* Do not remove the pouchdb-instance after the test
* The problem is that when this function is call in parallel,
* for example when you restore the tabs from a browser-session and open
* the same website multiple times at the same time,
* calling destroy would possibly crash the other call
*/
}

export const rxdb = true;
Expand Down
64 changes: 47 additions & 17 deletions test/unit/adapter-check.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
import assert from 'assert';
import config from './config';
import RxDB from '../../dist/lib/index';
import PouchDB from '../../dist/lib/pouch-db';
import {
POUCHDB_LOCATION
} from '../../dist/lib/plugins/adapter-check.js';
import {
adapterObject
} from '../../dist/lib/util';

import memdown from 'memdown';
if (!config.platform.isNode())
RxDB.plugin(require('pouchdb-adapter-idb'));

config.parallel('adapter-check.test.js', () => {
it('should be true on memory', async () => {
const ok = await RxDB.checkAdapter('memory');
assert.ok(ok);
describe('outcome', () => {
it('should be true on memory', async () => {
const ok = await RxDB.checkAdapter('memory');
assert.ok(ok);

const ok2 = await RxDB.checkAdapter('memory');
assert.ok(ok2);
const ok2 = await RxDB.checkAdapter('memory');
assert.ok(ok2);
});
it('should be false on invalid string', async () => {
const ok = await RxDB.checkAdapter('foobar');
assert.equal(ok, false);
});
it('should be true on memdown (leveldb-adapter)', async () => {
const ok = await RxDB.checkAdapter(memdown);
assert.ok(ok);
});
it('localstorage should be true on browser', async () => {
const should = config.platform.isNode() ? false : true;
const ok = await RxDB.checkAdapter('idb');
assert.equal(should, ok);
});
});
it('should be false on invalid string', async () => {
const ok = await RxDB.checkAdapter('foobar');
assert.equal(ok, false);
});
it('should be true on memdown (leveldb-adapter)', async () => {
const ok = await RxDB.checkAdapter(memdown);
assert.ok(ok);
});
it('localstorage should be true on browser', async () => {
const should = config.platform.isNode() ? false : true;
const ok = await RxDB.checkAdapter('idb');
assert.equal(should, ok);
describe('ISSUES', () => {
it('#715 Cleanup checkAdapter test databases after use', async () => {
const ok = await RxDB.checkAdapter(memdown);
assert.ok(ok);

// ensure the test-document is removed
const pouch = new PouchDB(
POUCHDB_LOCATION,
adapterObject(memdown), {
auto_compaction: false, // no compaction because this only stores local documents
revs_limit: 1
}
);

const found = await pouch.find({
selector: {}
});

assert.equal(found.docs.length, 0);
});
});
});

0 comments on commit d9717ff

Please sign in to comment.