tinyraft/etctree.spec.js

60 lines
3.8 KiB
JavaScript

const EtcTree = require('./etctree.js');
describe('EtcTree', () =>
{
it('should return the inserted value', () =>
{
const t = new EtcTree();
expect(t.api_txn({ success: [ { request_put: { key: '/vitastor//config/global', value: { hello: 'world' } } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ {} ] });
expect(t.api_txn({ success: [ { request_range: { key: '/vitastor/config/global' } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ { kvs: [ { key: 'vitastor/config/global', mod_revision: 1, value: { hello: 'world' } } ] } ] });
expect(t.api_txn({ success: [ { request_range: { key: '/vitastor/config/', range_end: '/vitastor/config0' } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ { kvs: [ { key: 'vitastor/config/global', mod_revision: 1, value: { hello: 'world' } } ] } ] });
expect(t.api_txn({ success: [ { request_range: { key: '/vitasto/', range_end: '/vitasto0' } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ { kvs: [] } ] });
expect(t.api_txn({
compare: [ { key: '/vitastor/config/global', target: 'MOD', mod_revision: 1, result: 'LESS' } ],
success: [ { request_put: { key: '/vitastor//config/global', value: { hello: 'world' } } } ],
failure: [ { request_range: { key: 'vitastor/config/global' } } ],
})).toEqual({ succeeded: false, revision: 1, responses: [ { kvs: [ { key: 'vitastor/config/global', mod_revision: 1, value: { hello: 'world' } } ] } ] });
expect(t.api_txn({
compare: [ { key: '/vitastor/config/global', target: 'MOD', mod_revision: 2, result: 'LESS' } ],
success: [ { request_put: { key: '/vitastor//config/global', value: { hello: 'world2' } } } ]
})).toEqual({ succeeded: true, revision: 2, responses: [ {} ] });
expect(t.api_txn({ success: [ { request_range: { key: '/vitastor/config/', range_end: '/vitastor/config0' } } ] }))
.toEqual({ succeeded: true, revision: 2, responses: [ { kvs: [ { key: 'vitastor/config/global', mod_revision: 2, value: { hello: 'world2' } } ] } ] });
});
it('should watch', () =>
{
const t = new EtcTree();
const sent = [];
const send = (event) => sent.push(event);
expect(t.api_txn({ success: [ { request_put: { key: '/vitastor//config/global', value: { hello: 'world' } } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ {} ] });
expect(t.api_create_watch({ watch_id: 1, key: '/vitastor/', range_end: '/vitastor0' }, send))
.toEqual({ watch_id: 1, created: true });
expect(sent).toEqual([]);
expect(t.api_txn({ success: [ { request_put: { key: '/vitastor/osd/state/1', value: { ip: '1.2.3.4' } } } ] }))
.toEqual({ succeeded: true, revision: 2, responses: [ {} ] });
expect(sent).toEqual([ { header: { revision: 2 }, events: [ { key: 'vitastor/osd/state/1', mod_revision: 2, value: { ip: '1.2.3.4' } } ] } ]);
});
it('should lease', async () =>
{
const t = new EtcTree();
const sent = [];
const send = (event) => sent.push(event);
const leaseID = t.api_grant_lease({ TTL: 0.5 }).ID;
expect(leaseID).not.toBeNull();
expect(t.api_txn({ success: [ { request_put: { key: '/vitastor/osd/state/1', lease: leaseID, value: { ip: '1.2.3.4' } } } ] }))
.toEqual({ succeeded: true, revision: 1, responses: [ {} ] });
expect(t.api_create_watch({ watch_id: 1, key: '/vitastor/', range_end: '/vitastor0' }, send))
.toEqual({ watch_id: 1, created: true });
expect(sent).toEqual([]);
await new Promise(ok => setTimeout(ok, 600));
expect(sent).toEqual([ { header: { revision: 2 }, events: [ { key: 'vitastor/osd/state/1', mod_revision: 2 } ] } ]);
});
});