s2s-all-delayed-peer-add.js
3.73 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
var config = {
IP: require('ip').address(),
port: 8765,
servers: 2,
browsers: 0,
dir: __dirname,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js'
}
}
var panic = require('panic-server');
panic.server().on('request', function(req, res){
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
//var servers = clients.filter('Node.js');
var servers = clients;
var alice = servers.pluck(1);
var bob = clients.excluding(alice).pluck(1);
describe("Sync all data from one server to another one added as peer after initialisation!", function(){
//this.timeout(5 * 60 * 1000);
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("Alice GUN has spawned!", function(){
return alice.run(function(test){
var env = test.props;
test.async();
global.ALICE = true;
try{ require('fs').unlinkSync(env.i+'alldata') }catch(e){}
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var port = env.config.port + env.i;
var Gun = require(env.config.dir + '/../../');
var peers = [], i = env.config.servers;
var gun = Gun({file: env.i+'alldata', peers: peers, web: server});
gun.get('a1').put({a:1});
gun.get('b2').put({b:2});
gun.get('c3').put({c:3});
gun.get('d4').put({d:4});
server.listen(port, function(){
setTimeout(function(){
test.done();
},1000);
});
}, {i: 1, config: config});
});
it("Bob GUN has spawned!", function(){
return bob.run(function(test){
var env = test.props;
test.async();
try{ require('fs').unlinkSync(env.i+'alldata') }catch(e){}
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var port = env.config.port + env.i;
var Gun = require(env.config.dir + '/../../');
// Initialise gun with no peers
var peers = [];
var gun = Gun({file: env.i+'alldata', peers: peers, web: server});
server.listen(port);
console.log("IGNORE 'invalid' WARNINGS!");
gun.on('out', {'#': 'loadthemall', get: {'#': {'*': ''}}});
// Add the other server as a peer
var i = env.config.servers;
while(i--){
var tmp = (env.config.port + (i + 1));
if(port != tmp){ // ignore ourselves
peers.push('http://'+ env.config.IP + ':' + tmp + '/gun');
}
}
console.log(port, " connect to ", peers);
gun.opt({peers: peers});
setTimeout(function(){
test.done();
}, 2000);
}, {i: 2, config: config});
});
it("Bob checked his file!", function(){
return bob.run(function(test){
var env = test.props;
test.async();
var raw = require('fs').readFileSync(env.i+'alldata');
var json = JSON.parse(raw);
var graph = json.graph;
console.log("Bob's graph on disk:", json);
if(!graph.a1 || graph.a1.a !== 1){
throw "a1 was not synced!";
}
if(!graph.b2 || graph.b2.b !== 2){
throw "b2 was not synced!";
}
if(!graph.c3 || graph.c3.c !== 3){
throw "c3 was not synced!";
}
if(!graph.d4 || graph.d4.d !== 4){
throw "b4 was not synced!";
}
test.done();
}, {i: 2, config: config});
});
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1000);
});
after("Everything shut down.", function(){
return servers.run(function(){
process.exit();
});
});
});