grail.js
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
This file should be one
folder deeper than every
other file. It requires
mocha to run.
*/
var panic = require('panic-server');
var spawn = require('child_process').spawn;
var path = require('path');
var http = require('http');
var fs = require('fs');
var ports = require('./ports');
var wd = require('selenium-webdriver');
function open(url) {
var driver = new wd.Builder()
.forBrowser('firefox')
.build();
driver.get(url);
return driver;
}
open('http://localhost:' + ports.panic + '/index.html');
open('http://localhost:' + ports.panic + '/index.html');
var staticServer = new http.Server(function (req, res) {
if (req.url === '/') {
req.url = '/index.html';
}
var file = path.join(__dirname, req.url);
try {
var page = fs.readFileSync(file, 'utf8');
res.end(page);
} catch (e) {
// don't care
}
});
var server = panic.clients.filter('Node.js').pluck(1);
var browsers = panic.clients.excluding(server);
var alice = browsers.pluck(1);
var bob = browsers.excluding(alice).pluck(1);
var serverPath = path.join(__dirname, 'gun-server.js');
// start the server on :8765
spawn('node', [serverPath]);
function waitFor (num, list) {
return new Promise(function (res) {
function ready() {
if (list.length < num) {
return;
}
res();
list.removeListener('add', ready);
return true;
}
if (!ready()) {
list.on('add', ready);
}
});
}
before(function () {
this.timeout(1500000);
// start the panic server
panic.server(staticServer).listen(ports.panic);
return waitFor(2, browsers)
.then(function () {
return waitFor(1, server);
});
});
var scope = {
uniqueKey: Math.random().toString(16).slice(2),
file: path.join(process.cwd(), 'delete-me.json'),
'@scope': true
}
describe('The holy grail', function () {
it('should allow full recovery', function () {
this.timeout(1500000);
return browsers.run(function () {
localStorage.clear();
})
.then(function () {
return browsers.run(function () {
window.ref = gun.get(uniqueKey).put({
text: 'ignore'
});
}, scope)
})
.then(function () {
return alice.run(function (done) {
// alice saves some data
//ref.path('text').put('Initial text', done); // TODO: USE THIS LINE INSTEAD!
ref.path('text').put('Initial text');
setTimeout(done, 50);
});
})
.then(function () {
return bob.run(function (done) {
var ctx = this;
ref.path('text').on(function (data) {
if (data === 'ignore') {
return;
}
// bob sees alice's data
if (data !== 'Initial text') {
ctx.fail('Wrong data showed up: ' + JSON.stringify(data));
}
done();
});
});
})
.then(function () {
return server.run(function () {
var fs = require('fs');
// destroy the data
fs.unlinkSync(file);
// crash the server
process.exit(0);
}, scope);
})
.then(function () {
return alice.run(function (done) {
ref.path('text').put('A conflicting update');
setTimeout(done, 50);
});
})
.then(function () {
return bob.run(function () {
ref.path('text').put('B conflicting update');
});
})
.then(function () {
spawn('node', [serverPath]);
return waitFor(1, server);
})
.then(function () {
return browsers.run(function (done) {
var ctx = this;
ref.path('text').on(function (value) {
if (value === 'B conflicting update') {
done();
}
});
});
});
});
});
after(function () {
if (server.length) {
return server.run(function () {
process.exit(0);
});
}
});