Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50664 views
1
###
2
TESTING of server-side synctable
3
4
COPYRIGHT : (c) 2017 SageMath, Inc.
5
LICENSE : AGPLv3
6
###
7
8
async = require('async')
9
expect = require('expect')
10
11
pgtest = require('./pgtest')
12
db = undefined
13
setup = (cb) -> (pgtest.setup (err) -> db=pgtest.db; cb(err))
14
teardown = pgtest.teardown
15
16
describe 'test storage_server synctable -- ', ->
17
@timeout(5000)
18
before(setup)
19
after(teardown)
20
21
synctable = undefined
22
it 'creates a synctable on the storage_servers', (done) ->
23
db.synctable
24
table : 'storage_servers'
25
cb : (err, x) ->
26
synctable = x; done(err)
27
28
it 'adds a storage server and notices this via wait', (done) ->
29
synctable.wait
30
until : (x) ->
31
return x.get().size > 0
32
timeout : 2
33
cb : done
34
# Now do the write, which will trigger the above wait to call done.
35
db._query
36
query : "INSERT INTO storage_servers"
37
values : {host:'storage0'}
38
39
it 'adds another storage server and notices this via change notification', (done) ->
40
new_host = 'storage1'
41
f = (host) ->
42
synctable.removeListener('change', f)
43
if host == new_host
44
done()
45
else
46
done("wrong host - #{host}")
47
synctable.on 'change', f
48
db._query
49
query : "INSERT INTO storage_servers"
50
values : {host:new_host}
51
cb : (err) -> expect(err).toEqual(undefined)
52
53
it 'adds 2 storage servers and notices when the last is added', (done) ->
54
hosts = ['storage2', 'storage3']
55
f = (host) ->
56
if host == hosts[hosts.length - 1]
57
synctable.removeListener('change', f)
58
done()
59
synctable.on('change', f)
60
db._query
61
query : "INSERT INTO storage_servers"
62
values : ({host:h} for h in hosts)
63
cb : (err) -> expect(err).toEqual(undefined)
64
65
it 'closes the synctable, makes some writes and does not get a notification', (done) ->
66
synctable.on 'change', ->
67
done("this should never be called!")
68
synctable.close (err) ->
69
expect(err).toEqual(undefined)
70
db._query
71
query : "INSERT INTO storage_servers"
72
values : {host:'storage389'}
73
cb : (err) ->
74
expect(err).toEqual(undefined)
75
setTimeout(done, 250) # wait short time to NOT have the above done get called
76
77
78
describe 'test accounts synctable', ->
79
before(setup)
80
after(teardown)
81
82
synctable = undefined
83
it 'creates a synctable on accounts', (done) ->
84
db.synctable
85
table : 'accounts'
86
columns : ['account_id', 'email_address', 'first_name', 'last_name']
87
where : {first_name:'Will'}
88
cb : (err, x) ->
89
synctable = x; done(err)
90
91
it 'adds a user and notices via wait', (done) ->
92
synctable.wait
93
until : (x) ->
94
return x.get().size > 0
95
timeout : 2
96
cb : done
97
# Now do the write, which will trigger the above wait to call done.
98
db.create_account first_name:'Will', last_name:'Sage', email_address:'[email protected]', cb:(e)->expect(e).toEqual(undefined)
99
100
it 'creates a user that does not match our condition and does not get notified', (done) ->
101
# First we setup a listener, which will get one notification with the *SECOND* user we add below
102
# (so we know the first did not yield a notification).
103
synctable.once 'change', (user) ->
104
expect(synctable.getIn([user, 'last_name'])).toEqual('YES')
105
done()
106
db.create_account first_name:'Dennis', last_name:'Sage', email_address:'[email protected]', cb:(err)->
107
expect(err).toEqual(undefined)
108
db.create_account first_name:'Will', last_name:'YES', email_address:'[email protected]', cb:(err)->
109
expect(err).toEqual(undefined)
110
111
it 'deletes an account and gets notified', (done) ->
112
id0 = id1 = undefined
113
synctable.once 'change', (id) ->
114
expect(id).toEqual(id1) # i.e., delete the one that should be deleted
115
done()
116
async.series([
117
(cb) ->
118
db.create_account first_name:'X', last_name:'Y', email_address:'[email protected]', cb: (err, id) ->
119
id0 = id
120
cb(err)
121
(cb) ->
122
db.create_account first_name:'Will', last_name:'Y', email_address:'[email protected]', cb: (err, id) ->
123
id1 = id
124
cb(err)
125
(cb) ->
126
db.delete_account(account_id: id0, cb: cb)
127
(cb) ->
128
db.delete_account(account_id: id1, cb: cb)
129
])
130
131
132
133
134