forked from unshiftio/liferaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
1043 lines (820 loc) · 27.2 KB
/
test.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* istanbul ignore next */
describe('liferaft', function () {
'use strict';
var assume = require('assume')
, Raft = require('./')
, raft;
beforeEach(function each() {
raft = new Raft({
'heartbeat min': 4000, // Bump timeout to ensure no false positive
'heartbeat max': 6000 // so we don't trigger before test timeout
});
});
afterEach(function each() {
raft.end();
});
it('is exposed a function', function () {
assume(Raft).is.a('function');
});
it('is extendible', function () {
assume(Raft.extend).is.a('function');
});
describe('initialization', function () {
it('can be constructed without `new`', function () {
raft.end();
raft = Raft();
assume(raft).is.instanceOf(Raft);
});
it('accepts strings for election and heartbeat', function () {
raft.end();
raft = new Raft({
'election min': '100 ms',
'election max': '150 ms',
'heartbeat': '600 ms'
});
assume(raft.beat).equals(600);
assume(raft.election.max).equals(150);
assume(raft.election.min).equals(100);
raft.end();
raft = new Raft({
'election min': 100,
'election max': 150,
'heartbeat': 600
});
assume(raft.beat).equals(600);
assume(raft.election.max).equals(150);
assume(raft.election.min).equals(100);
});
it('sets a unique address by default', function () {
var another = new Raft();
assume(raft.address).does.not.equal(another.address);
another.end();
});
it('can set a custom address', function () {
raft.end();
raft = new Raft({ address: 'foo' });
assume(raft.address).equals('foo');
});
it('accepts the address as first argument', function () {
raft.end();
raft = new Raft('foo');
assume(raft.address).equals('foo');
});
it('will call the initialization function if exists', function (next) {
var MyRaft = Raft.extend({
initialize: function () {
var node = this;
setTimeout(function () {
node.end();
next();
}, 0);
}
});
new MyRaft();
});
it('async emits the initialize event once the initialize method is done', function (next) {
var ready = false;
var MyRaft = Raft.extend({
initialize: function initialize(options, init) {
assume(options.custom).equals('options');
assume(ready).is.false();
setTimeout(function () {
ready = true;
init();
}, 100);
}
});
var raft = new MyRaft('foobar', { custom: 'options' });
raft.on('initialize', function () {
assume(ready).is.true();
next();
});
});
it('emits error when the initialize fails', function (next) {
var MyRaft = Raft.extend({
initialize: function initialize(options, init) {
setTimeout(function () {
init(new Error('Failure'));
}, 100);
}
});
var raft = new MyRaft();
raft.on('error', function (err) {
assume(err.message).equals('Failure');
next();
});
});
});
describe('#indefinitely', function () {
it('it runs until the supplied callback is called', function (next) {
var attempts = 0;
raft.indefinitely(function attempt(done) {
attempts++;
if (attempts === 5) done();
}, next, 10);
});
it('it runs until the supplied callback is called without err', function (next) {
var attempts = 0;
raft.indefinitely(function attempt(done) {
attempts++;
if (attempts === 5) done();
else done(new Error('failure'));
}, next, 10);
});
});
describe('#message', function () {
it('calls all joined nodes', function (next) {
var pattern = '';
raft.join(function () { pattern += 'a'; });
raft.join(function () { pattern += 'b'; });
raft.join(function () { pattern += 'c'; });
raft.message(Raft.FOLLOWER, raft.packet('foo'));
setTimeout(function () {
assume(pattern).equals('abc');
next();
}, 20);
});
it('emits the `data` event with response', function (next) {
var node = raft.join(function (data, fn) {
fn(undefined, node.packet('external'));
});
raft.on('rpc', function (packet) {
assume(packet.type).equals('external');
assume(packet.address).equals(node.address);
assume(raft.address).does.not.equal(node.address);
next();
});
raft.message(Raft.FOLLOWER, raft.packet('foo'));
});
it('sends message to cluster leader', function (next) {
var leader = raft.join(function (packet) {
assume(packet.leader).equals(this.address);
assume(packet.type).equals('leader');
next();
});
raft.join(function () { throw new Error('We are followers, not leader'); });
raft.join(function () { throw new Error('We are followers, not leader'); });
raft.join(function () { throw new Error('We are followers, not leader'); });
raft.change({ leader: leader.address });
raft.message(Raft.LEADER, raft.packet('leader'));
});
it('sends a node specified by address', function (next) {
raft.join(function () { throw new Error('You sir, msg the wrong node'); });
var node = raft.join(function (packet) {
assume(packet.type).equals('address');
next();
});
raft.join(function () { throw new Error('You sir, msg the wrong node'); });
raft.join(function () { throw new Error('You sir, msg the wrong node'); });
raft.message(node.address, raft.packet('address'));
});
it('throws an error on undefined message', function () {
assume(function () {
raft.message(undefined, raft.packet('foo'));
}).throws('Cannot send message to `undefined`');
});
it('runs the `when` callback with no errors', function (next) {
var node = raft.join(function (data, callback) {
callback(undefined, 'foo');
});
node.address = 'addr';
raft.message(Raft.FOLLOWER, raft.packet('foo'), function (err, data) {
assume(err).equals(undefined);
assume(data).deep.equals({ addr: 'foo' });
next();
});
});
it('runs the `when` callback with no errors', function (next) {
var node = raft.join(function (data, callback) {
callback('bar');
});
node.address = 'addr';
raft.message(Raft.FOLLOWER, raft.packet('foo'), function (err, data) {
assume(err).deep.equals({ addr: 'bar' });
assume(data).deep.equals({});
next();
});
});
});
describe('#timeout', function () {
it('generates a random timeout between min/max', function () {
var timeouts = []
, times = 100
, same = {};
for (var i = 0; i < times; i++) {
timeouts.push(raft.timeout());
}
timeouts.forEach(function (timeout, i) {
assume(timeout).is.a('number');
assume(timeout).is.least(raft.election.min);
assume(timeout).is.most(raft.election.max);
same[timeout] = same[timeout] || 0;
same[timeout]++;
});
//
// Ensure that our random generation isn't to damn sucky and can be
// considered random enough to be workable. This isn't a hard requirement
// of Raft but still something we need to assert.
//
// assume(Object.keys(same).length).is.above(70);
});
it('uses user supplied timeouts', function () {
raft.end();
raft = new Raft({
'election min': '300ms',
'election max': '1s'
});
var timeouts = []
, times = 100;
for (var i = 0; i < times; i++) {
timeouts.push(raft.timeout('election'));
}
timeouts.forEach(function (timeout, i) {
assume(timeout).is.a('number');
assume(timeout).is.least(300);
assume(timeout).is.most(1000);
});
});
});
describe('#end', function () {
function listeners(ee) {
var amount = 0;
if (!ee._events) return amount;
for (var key in ee._events) {
amount += ee.listeners(key);
}
return amount;
}
it('returns `true` when destroyed for the first time', function () {
assume(raft.end()).is.true();
});
it('returns `false` when destroyed for the second time', function () {
assume(raft.end()).is.true();
assume(raft.end()).is.false();
assume(raft.end()).is.false();
});
it('removes all listeners', function () {
assume(listeners(raft)).is.above(0);
assume(raft.end()).to.equal(true);
assume(listeners(raft)).equals(0);
});
it('emits an end event', function (next) {
raft.on('end', next);
//
// Double end is here to check if the event is not executed multiple
// times.
//
raft.end();
raft.end();
});
});
describe('#change', function () {
it('updates the term and emits a change', function (next) {
raft.once('term change', function (currently, previously) {
assume(currently).equals(raft.term);
assume(previously).equals(0);
assume(raft.term).equals(3);
next();
});
raft.change({ term: 3 });
});
it('updates the leader and emits a change', function (next) {
raft.once('leader change', function (currently, previously) {
assume(currently).equals(raft.leader);
assume(raft.leader).equals('foo');
assume(previously).equals('');
next();
});
raft.change({ leader: 'foo' });
});
it('updates the state and emits a change', function (next) {
raft.once('state change', function (currently, previously) {
assume(previously).equals(Raft.FOLLOWER);
assume(raft.state).equals(Raft.LEADER);
assume(currently).equals(raft.state);
next();
});
raft.change({ state: Raft.LEADER });
});
it('returns this', function () {
assume(raft.change()).equals(raft);
});
it('only a emits change if something changed', function () {
function heded() { throw new Error('I failed'); }
raft.once('term change', heded)
.once('state change', heded)
.once('leader change', heded);
raft.change({
term: raft.term,
state: raft.state,
leader: raft.leader
});
});
});
describe('heartbeat', function () {
it('increments the heartbeat if set before', function (next) {
raft.end();
raft = new Raft({ 'heartbeat min': 100, 'heartbeat max': 110 });
raft.heartbeat();
setTimeout(function () {
raft.heartbeat();
setTimeout(next, 90);
}, 90);
});
it('emits a heartbeat timeout', function (next) {
raft.end();
raft = new Raft({ 'heartbeat min': 10, 'heartbeat max': 40 });
raft.once('heartbeat timeout', next);
raft.heartbeat();
});
it('promotes to candidate', function (next) {
raft.end();
raft = new Raft({ 'heartbeat min': 10, 'heartbeat max': 40 });
raft.once('state change', function () {
assume(raft.state).equals(Raft.CANDIDATE);
next();
});
raft.heartbeat();
});
it('returns this', function () {
assume(raft.heartbeat()).equals(raft);
assume(raft.heartbeat()).equals(raft);
});
});
describe('#promote', function () {
it('changes state to candidate', function () {
assume(raft.state).does.not.equal(Raft.CANDIDATE);
raft.promote();
assume(raft.state).equals(Raft.CANDIDATE);
});
it('resets the leader', function () {
raft.leader = raft.address;
raft.promote();
assume(raft.leader).equals('');
});
it('increments term', function () {
raft.term = 40;
raft.promote();
assume(raft.term).equals(41);
});
it('votes for self', function () {
assume(raft.votes.for).equals(null);
assume(raft.votes.granted).equals(0);
raft.promote();
assume(raft.votes.for).equals(raft.address);
assume(raft.votes.granted).equals(1);
});
});
describe('#packet', function () {
it('wraps the object with common but required data', function () {
var obj = raft.packet('vote', 'data packet');
assume(obj).is.a('object');
assume(obj.state).is.a('number');
assume(obj.state).equals(Raft.FOLLOWER);
assume(obj.term).is.a('number');
assume(obj.term).equals(raft.term);
assume(obj.address).is.a('string');
assume(obj.address).equals(raft.address);
assume(obj.leader).equals(raft.leader);
assume(obj.type).equals('vote');
assume(obj.data).equals('data packet');
});
});
describe('#quorum', function () {
it('needs 7 votes in a cluster of 11', function () {
raft.nodes.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
assume(raft.quorum(7)).is.true();
assume(raft.quorum(5)).is.false();
});
it('return false when there are no nodes', function () {
assume(raft.quorum(999)).is.false();
raft.nodes.push(1, 2, 3, 4, 5);
assume(raft.quorum(0)).is.false();
assume(raft.quorum(50)).is.true();
assume(raft.quorum(5)).is.true();
});
});
describe('#majority', function () {
it('generates an int', function () {
for (var i = 0; i < 13; i++) {
raft.nodes.push(i);
assume(raft.majority() % 1 === 0).is.true();
}
});
it('needs 7 votes in a cluster of 11', function () {
raft.nodes.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
assume(raft.majority()).equals(7);
});
});
describe('#clone', function () {
it('returns the created instance', function () {
var x = raft.clone();
assume(x).is.instanceOf(Raft);
});
it('returns instance of a custom instance', function () {
raft.end();
var Draft = Raft.extend({ write: function () { } })
, draft = new Draft();
assume(draft.clone()).is.instanceOf(Draft);
assume(draft.clone()).is.instanceOf(Raft);
draft.end();
});
it('inherits the options', function () {
raft.end();
var Draft = Raft.extend({ write: function () { } })
, draft = new Draft({ threshold: 99 })
, punk = draft.clone();
assume(punk.threshold).equals(99);
punk.end();
draft.end();
});
it('allows overriding of config through options', function () {
raft.end();
var Draft = Raft.extend({ write: function () { } })
, draft = new Draft({ threshold: 99 })
, punk = draft.clone({ threshold: 9 });
assume(punk.threshold).equals(9);
punk.end();
draft.end();
});
});
describe('#join', function () {
it('returns the node we added', function () {
assume(raft.nodes.length).equals(0);
var node = raft.join();
assume(node.address).does.not.equal(raft.address);
assume(raft.nodes.length).equals(1);
assume(node).does.not.equal(raft);
assume(node).is.instanceOf(Raft);
node.end();
});
it('cannot add a server with the same address as it self', function () {
assume(raft.nodes.length).equals(0);
var node = raft.join(raft.address);
assume(node).is.a('undefined');
assume(raft.nodes.length).equals(0);
});
it('emits an `join` event when a new ode is added', function (next) {
raft.once('join', function (node) {
assume(raft.nodes.length).equal(1);
assume(node).is.instanceOf(Raft);
node.end();
next();
});
raft.join();
});
it('returns the same instance as the node', function () {
raft.end();
var Draft = Raft.extend({ write: function () { } })
, draft = new Draft({ threshold: 99 })
, punk = draft.join('foo');
assume(punk).is.instanceOf(Draft);
assume(punk).is.instanceOf(Raft);
assume(punk).does.not.equal(draft);
punk.end();
draft.end();
});
it('allows setting of node with a custom address', function () {
var node = raft.join('foo');
assume(node).does.not.equal(raft);
assume(node).is.instanceOf(Raft);
assume(node.address).equals('foo');
node.end();
});
it('will leave the cluster when ended', function (next) {
var node = raft.join();
raft.once('leave', function (left) {
assume(raft.nodes.length).equals(0);
assume(node).equals(left);
next();
});
node.end();
});
});
describe('event', function () {
describe('term change', function () {
it('resets the votes', function (next) {
raft.on('term change', function () {
assume(raft.term).equals(2);
assume(raft.votes.granted).equals(0);
assume(!raft.votes.for).is.true();
next();
});
raft.votes.for = raft.address;
raft.votes.granted++;
raft.change({ term: 2 });
});
});
describe('data', function () {
it('calls the callback for unknown messages', function (next) {
raft.emit('data', { type: 'bar' }, function (err) {
assume(err).is.not.instanceOf(Error);
assume(err).is.a('object');
assume(err.type).equals('error');
assume(err.data).includes('Unknown');
next();
});
});
it('calls with an error when invalid data is send', function (next) {
raft.emit('data', 1, function (err) {
assume(err).is.not.instanceOf(Error);
assume(err).is.a('object');
assume(err.type).equals('error');
assume(err.data).includes('Invalid');
next();
});
});
it('updates to FOLLOWER as CANDIDATE when msg by LEADER', function (next) {
raft.promote();
raft.once('state change', function () {
assume(this.state).equals(Raft.FOLLOWER);
next();
});
raft.emit('data', {
state: Raft.LEADER,
term: raft.term
});
});
it('automatically update term when ours is out of date', function (next) {
raft.change({ term : 40 });
raft.once('term change', function () {
assume(this.term).equals(41);
next();
});
raft.emit('data', {
state: Raft.LEADER,
term: 41
});
});
});
describe('state events', function () {
it('should emit a `leader` event', function (next) {
raft.once('leader', function () {
next();
});
raft.change({ state: Raft.LEADER });
});
it('should emit a `follower` event', function (next) {
raft.once('follower', function () {
next();
});
raft.change({ state: Raft.LEADER }); // Default is follower, so change first
raft.change({ state: Raft.FOLLOWER });
});
it('should emit a `candidate` event', function (next) {
raft.once('candidate', function () {
next();
});
raft.change({ state: Raft.CANDIDATE });
});
it('should emit a `stopped` event', function (next) {
raft.once('stopped', function () {
next();
});
raft.change({ state: Raft.STOPPED });
});
it('should emit a `child` event', function (next) {
raft.once('child', function () {
next();
});
raft.change({ state: Raft.CHILD });
});
});
describe('rpc', function () {
it('should emit an rpc event when an unknown package arrives', function (next) {
raft.once('rpc', function (packet) {
assume(packet.type).equals('shizzle');
next();
});
raft.emit('data', raft.packet('shizzle'));
});
});
describe('heartbeat', function () {
it('emits a heartbeat event when we are a leader', function (next) {
raft.once('heartbeat', function (packet) {
assume(packet.type).equals('append');
next();
});
raft.change({ state: Raft.LEADER });
raft.heartbeat();
});
});
});
//
// The following set of tests asserts if we correctly follow the Raft white
// paper and that our module does what is expected of it.
//
describe('Raft Consensus Algorithm', function () {
describe('election', function () {
describe('vote', function () {
it('ignores stale votes when term is out of date', function () {
raft.once('vote', function (packet, accepted) {
throw new Error('Broken');
});
raft.change({ term: 139 });
raft.emit('data', {
address: 'vladimir',
type: 'vote',
term: 138
});
});
it('votes on first come first serve basis', function (next) {
raft.once('vote', function (packet, accepted) {
assume(accepted).is.true();
raft.once('vote', function (packet, accepted) {
assume(accepted).is.false();
next();
});
});
raft.change({ term: 139 });
raft.emit('data', { address: 'vladimir', term: raft.term, type: 'vote' });
raft.emit('data', { address: 'anatoly', term: raft.term, type: 'vote' });
});
});
describe('voted', function () {
it('only increments votes when being a CANDIDATE', function () {
assume(raft.votes.granted).equals(0);
assume(raft.state).equals(Raft.FOLLOWER);
raft.emit('data', {
data: { granted: true },
term: raft.term,
state: Raft.FOLLOWER,
address: 'foobar',
type: 'voted'
});
assume(raft.votes.granted).equals(0);
//
// Promote our selfs to candidate.
//
raft.promote();
assume(raft.state).equals(Raft.CANDIDATE);
assume(raft.votes.granted).equals(1);
raft.emit('data', {
data: { granted: true },
term: raft.term,
state: Raft.FOLLOWER,
address: 'foobar',
type: 'voted'
});
assume(raft.votes.granted).equals(2);
assume(raft.state).equals(Raft.CANDIDATE);
});
it('only accepts granted votes', function () {
raft.promote();
assume(raft.state).equals(Raft.CANDIDATE);
assume(raft.votes.granted).equals(1);
raft.emit('data', {
data: { granted: false },
term: raft.term,
state: Raft.FOLLOWER,
address: 'foobar',
type: 'voted'
});
assume(raft.votes.granted).equals(1);
assume(raft.state).equals(Raft.CANDIDATE);
});
it('changes to leader when majority has voted', function (next) {
//
// Feed raft some "nodes" so it can actually reach a consensus when it
// received a majority of the votes.
//
raft.nodes.push(
{ write: function () {} },
{ write: function () {} },
{ write: function () {} },
{ write: function () {} },
{ write: function () {} }
);
raft.promote();
raft.once('state change', function (currently, previously) {
assume(previously).equals(Raft.CANDIDATE);
assume(raft.state).equals(Raft.LEADER);
assume(currently).equals(Raft.LEADER);
next();
});
for (var i = 0; i < 4; i++) {
raft.emit('data', {
data: { granted: true },
term: raft.term,
state: Raft.FOLLOWER,
address: 'foobar',
type: 'voted'
});
}
assume(raft.state).equals(Raft.LEADER);
});
});
});
});
//
// Batch of tests which tests the clustering capabilities of liferaft as
// everything works different when you start working with massive clusters.
//
describe('cluster', function () {
var port = 8088
, net = require('net')
, debug = require('diagnostics')('cluster');
var Paddle = Raft.extend({
/**
* Initialize the server so we can receive connections.
*
* @param {Object} options Received optiosn when constructing the client.
* @api private
*/
initialize: function initialize(options) {
var raft = this;
var server = net.createServer(function incoming(socket) {
socket.on('data', function (buff) {
var data = JSON.parse(buff.toString());
debug(raft.address +':packet#data', data);
raft.emit('data', data, function reply(data) {
debug(raft.address +':packet#reply', data);
socket.write(JSON.stringify(data));
socket.end();
});
});
}).listen(this.address);
this.once('end', function enc() {
server.close();
});
},
/**
* Write to the connection.
*
* @param {Object} packet Data to be transfered.
* @param {Function} fn Completion callback.
* @api public
*/
write: function write(packet, fn) {
var socket = net.connect(this.address)
, raft = this;
debug(raft.address +':packet#write', packet);
socket.on('error', fn);
socket.on('data', function (buff) {
var data;
try { data = JSON.parse(buff.toString()); }
catch (e) { return fn(e); }
debug(raft.address +':packet#callback', packet);
fn(undefined, data);
});
socket.setNoDelay(true);
socket.write(JSON.stringify(packet));
}
});
it('reaches consensus about leader election', function (next) {
var ports = [port++, port++, port++, port++]
, nodes = []
, node
, i
, j;
for (i = 0; i < ports.length; i++) {
node = new Paddle(ports[i]);
nodes.push(node);
for (j = 0; j < ports.length; j++) {
if (ports[j] === ports[i]) continue;
node.join(ports[j]);
}
}
for (i = 0; i < nodes.length; i++) {
if (nodes[i] === node) continue;
nodes[i].once('state change', function (to, from) {
throw new Error('I should not change state, im a follower');
});
nodes[i].on('leader change', function (to, from) {
assume(to).equals(node.address);
});