smc-hub
Version:
CoCalc: Backend webserver component
245 lines (233 loc) • 6.88 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
//########################################################################
// This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
// License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
//########################################################################
/*
TESTING of server-side synctable
COPYRIGHT : (c) 2017 SageMath, Inc.
LICENSE : AGPLv3
*/
var async, db, expect, pgtest, setup, teardown;
async = require('async');
expect = require('expect');
pgtest = require('./pgtest');
db = void 0;
setup = function(cb) {
return pgtest.setup(function(err) {
db = pgtest.db;
return cb(err);
});
};
teardown = pgtest.teardown;
describe('test storage_server synctable -- ', function() {
var synctable;
this.timeout(10000);
before(setup);
after(teardown);
synctable = void 0;
it('creates a synctable on the storage_servers', function(done) {
return db.synctable({
table: 'storage_servers',
cb: function(err, x) {
synctable = x;
return done(err);
}
});
});
it('adds a storage server and notices this via wait', function(done) {
synctable.wait({
until: function(x) {
return x.get().size > 0;
},
timeout: 2,
cb: done
});
// Now do the write, which will trigger the above wait to call done.
return db._query({
query: "INSERT INTO storage_servers",
values: {
host: 'storage0'
}
});
});
it('adds another storage server and notices this via change notification', function(done) {
var f, new_host;
new_host = 'storage1';
f = function(host) {
synctable.removeListener('change', f);
if (host === new_host) {
return done();
} else {
return done(`wrong host - ${host}`);
}
};
synctable.on('change', f);
return db._query({
query: "INSERT INTO storage_servers",
values: {
host: new_host
},
cb: function(err) {
return expect(err).toEqual(void 0);
}
});
});
it('adds 2 storage servers and notices when the last is added', function(done) {
var f, h, hosts;
hosts = ['storage2', 'storage3'];
f = function(host) {
if (host === hosts[hosts.length - 1]) {
synctable.removeListener('change', f);
return done();
}
};
synctable.on('change', f);
return db._query({
query: "INSERT INTO storage_servers",
values: (function() {
var i, len, results;
results = [];
for (i = 0, len = hosts.length; i < len; i++) {
h = hosts[i];
results.push({
host: h
});
}
return results;
})(),
cb: function(err) {
return expect(err).toEqual(void 0);
}
});
});
return it('closes the synctable, makes some writes and does not get a notification', function(done) {
synctable.on('change', function() {
return done("this should never be called!");
});
return synctable.close(function(err) {
expect(err).toEqual(void 0);
return db._query({
query: "INSERT INTO storage_servers",
values: {
host: 'storage389'
},
cb: function(err) {
expect(err).toEqual(void 0);
return setTimeout(done, 250); // wait short time to NOT have the above done get called
}
});
});
});
});
describe('test accounts synctable', function() {
var synctable;
this.timeout(10000);
before(setup);
after(teardown);
synctable = void 0;
it('creates a synctable on accounts', function(done) {
return db.synctable({
table: 'accounts',
columns: ['account_id', 'email_address', 'first_name', 'last_name'],
where: {
first_name: 'Will'
},
cb: function(err, x) {
synctable = x;
return done(err);
}
});
});
it('adds a user and notices via wait', function(done) {
synctable.wait({
until: function(x) {
return x.get().size > 0;
},
timeout: 2,
cb: done
});
// Now do the write, which will trigger the above wait to call done.
return db.create_account({
first_name: 'Will',
last_name: 'Sage',
email_address: 'a@sagemath.com',
cb: function(e) {
return expect(e).toEqual(void 0);
}
});
});
it('creates a user that does not match our condition and does not get notified', function(done) {
// First we setup a listener, which will get one notification with the *SECOND* user we add below
// (so we know the first did not yield a notification).
synctable.once('change', function(user) {
expect(synctable.getIn([user, 'last_name'])).toEqual('YES');
return done();
});
return db.create_account({
first_name: 'Dennis',
last_name: 'Sage',
email_address: 'b@sagemath.com',
cb: function(err) {
expect(err).toEqual(void 0);
return db.create_account({
first_name: 'Will',
last_name: 'YES',
email_address: 'c@sagemath.com',
cb: function(err) {
return expect(err).toEqual(void 0);
}
});
}
});
});
return it('deletes an account and gets notified', function(done) {
var id0, id1;
id0 = id1 = void 0;
synctable.once('change', function(id) {
expect(id).toEqual(id1); // i.e., delete the one that should be deleted
return done();
});
return async.series([
function(cb) {
return db.create_account({
first_name: 'X',
last_name: 'Y',
email_address: 'd@sagemath.com',
cb: function(err,
id) {
id0 = id;
return cb(err);
}
});
},
function(cb) {
return db.create_account({
first_name: 'Will',
last_name: 'Y',
email_address: 'e@sagemath.com',
cb: function(err,
id) {
id1 = id;
return cb(err);
}
});
},
function(cb) {
return db.delete_account({
account_id: id0,
cb: cb
});
},
function(cb) {
return db.delete_account({
account_id: id1,
cb: cb
});
}
]);
});
});
}).call(this);
//# sourceMappingURL=postgres-synctable.js.map