forked from rjrodger/visigoth
-
Notifications
You must be signed in to change notification settings - Fork 14
/
tests.js
105 lines (90 loc) · 3.57 KB
/
tests.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
var expect = require("chai").expect;
var sinon = require("sinon");
describe('add targets', function() {
it("should add a target with status 'CLOSED' and set the 'statusTimestamp'", function() {
var visigoth = require('./visigoth')();
var clock = sinon.useFakeTimers();
// Tick some msecs just to avoid 0 which could be a default value.
clock.tick(110);
visigoth.add("I am a node");
expect(visigoth.upstreams$[0].target).to.equal("I am a node");
expect(visigoth.upstreams$.length).to.equal(1);
expect(visigoth.upstreams$[0].meta$.status).to.equal("CLOSED");
expect(visigoth.upstreams$[0].meta$.statusTimestamp).to.equal(clock.now);
clock.restore();
});
it("lastChoosenTimestamp should be null if the node was never choosen (just added)", function(){
var visigoth = require('./visigoth')();
visigoth.add("I am a node");
expect(visigoth.upstreams$[0].meta$.lastChoosenTimestamp).to.equal(null);
});
});
describe('remove targets', function() {
it('removes a string target', function () {
var visigoth = require('./visigoth')();
visigoth.add("target 1");
visigoth.add("target 2");
visigoth.remove("target 1");
expect(visigoth.upstreams$.length).to.equal(1);
expect(visigoth.upstreams$[0].target).to.equal("target 2");
});
it('removes a function target', function () {
var visigoth = require('./visigoth')();
var a = new function() {return "a";};
var b = new function() {return "b";};
visigoth.add(a);
visigoth.add(b);
visigoth.remove(a);
expect(visigoth.upstreams$[0].target).to.equal(b);
});
it('removes an object target', function () {
var visigoth = require('./visigoth')();
var a = {a: "a"};
var b = {b: "b"};
visigoth.add(a);
visigoth.add(b);
visigoth.remove(b);
expect(visigoth.upstreams$[0].target).to.equal(a);
});
});
describe('choose target', function () {
it('chooses a target when there is at least one available', function(done) {
var visigoth = require('./visigoth')();
visigoth.add("node one");
visigoth.choose(function(error, target) {
expect(error).to.be.null;
expect(target).to.equal("node one");
done();
});
});
it('fails when there are no targets', function(done) {
var visigoth = require('./visigoth')();
visigoth.choose(function(error, target, errored, stats) {
expect(error).to.not.be.null;
expect(target).to.be.undefined;
expect(errored).to.be.undefined;
expect(stats).to.be.undefined;
done();
});
});
it('marks the node as "HALF-OPEN" once the timeout has expired', function(done) {
var clock = sinon.useFakeTimers();
var visigoth = require('./visigoth')({closingTimeout: 300});
visigoth.add("test target");
visigoth.add("test target 2");
visigoth.choose(function(err, target, errored) {
errored();
});
clock.tick(300);
visigoth.choose(function(err, target) {
expect(target).to.equal('test target 2');
expect(visigoth.upstreams$[0].meta$.status).to.equal('OPEN');
clock.tick(1);
visigoth.choose(function(err, target) {
expect(target).to.equal('test target');
expect(visigoth.upstreams$[0].meta$.status).to.equal('HALF-OPEN');
done();
});
});
});
});