UNPKG

3.36 kBJavaScriptView Raw
1// Jasmine tests for servers availability related methods.
2
3var CodeGradX = require('../codegradxlib.js');
4
5describe('CodeGradX', function () {
6 it('should be loaded', function () {
7 expect(CodeGradX).toBeDefined();
8 });
9
10 it('should create a State', function () {
11 var state = new CodeGradX.State();
12 expect(state).toBeDefined();
13 expect(state instanceof CodeGradX.State).toBeTruthy();
14 expect(state.servers.names).toContain('a');
15 });
16
17 it('should check running server A0', function (done) {
18 function faildone (reason) {
19 var state = CodeGradX.getCurrentState();
20 state.debug('faildone', reason).show();
21 //console.log(reason);
22 fail(reason);
23 done();
24 }
25 var state = new CodeGradX.State();
26 var promise = state.checkServer('a', 0);
27 promise.then(function (response) {
28 //console.log(response.entity);
29 //console.log(state.servers.a);
30 expect(state.servers.a[0].enabled).toBeTruthy();
31 expect(response.entity.kind).toBe('alive');
32 done();
33 }, faildone);
34 });
35
36 it('should check new running server A1', function (done) {
37 function faildone (reason) {
38 fail(reason);
39 done();
40 }
41 var state = new CodeGradX.State();
42 var promise = state.checkServer('a', 1);
43 promise.then(function (response) {
44 //console.log(response.entity);
45 //console.log(state.servers.a);
46 expect(state.servers.a[1].enabled).toBeTruthy();
47 expect(response.entity.kind).toBe('alive');
48 done();
49 }, faildone);
50 });
51
52 it('should check new non running server A17', function (done) {
53 function faildone (reason) {
54 fail(reason);
55 done();
56 }
57 var state = new CodeGradX.State();
58 var promise = state.checkServer('a', 17);
59 promise.then(function (response) {
60 fail("should not happen!");
61 done();
62 }).catch(function (error) {
63 //console.log(error);
64 //console.log(state.servers.a);
65 expect(state.servers.a[17].enabled).toBe(false);
66 expect(state.servers.a[17].lastError).toBeTruthy();
67 done();
68 });
69 });
70
71 it('should check all servers A', function (done) {
72 function faildone (reason) {
73 fail(reason);
74 done();
75 }
76 var state = new CodeGradX.State();
77 var promise = state.checkServers('a');
78 promise.then(function (descriptions) {
79 //console.log(responses);
80 //console.log(state.servers.a);
81 expect(descriptions).toBe(state.servers.a);
82 expect(state.servers.a[0].enabled).toBeTruthy();
83 expect(state.servers.a[1].enabled).toBeTruthy();
84 done();
85 }, faildone);
86 });
87
88 it('should check twice all server A', function (done) {
89 function faildone (reason) {
90 fail(reason);
91 done();
92 }
93 var state = new CodeGradX.State();
94 // Check a0, a1 and try unavailable a2:
95 var promise1 = state.checkServers('a');
96 promise1.then(function (responses1) {
97 //console.log(state.servers.a);
98 // Check a0, a1 and try unavailable a2 but don't try a3:
99 var promise2 = state.checkServers('a');
100 promise2.then(function (responses2) {
101 //console.log(state.servers.a);
102 expect(state.servers.a.length).toBe(state.servers.a.length);
103 expect(state.servers.a.next).toBe(state.servers.a.next);
104 done();
105 }, faildone);
106 }, faildone);
107 });
108
109});
110
111// end of codegradx-spec.js