|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module Dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +const HealthMonitor = require('../../src/health-monitor'); |
| 8 | +const log = require('debugnyan')('process-manager:health-monitor'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Instances. |
| 12 | + */ |
| 13 | + |
| 14 | +const healthMonitor = new HealthMonitor(); |
| 15 | + |
| 16 | +/** |
| 17 | + * Test `HealthMonitor`. |
| 18 | + */ |
| 19 | + |
| 20 | +describe('HealthMonitor', () => { |
| 21 | + afterEach(() => { |
| 22 | + healthMonitor.cleanup(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('constructor()', () => { |
| 26 | + it('should set up the default values', () => { |
| 27 | + const healthMonitor = new HealthMonitor(); |
| 28 | + |
| 29 | + expect(healthMonitor.checks).toEqual([]); |
| 30 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.UNKNOWN); |
| 31 | + expect(healthMonitor.states).toEqual({}); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('addCheck()', () => { |
| 36 | + it('should throw an error if the `id` is already in use', () => { |
| 37 | + healthMonitor.addCheck({ handler: () => {}, id: 'foo' }); |
| 38 | + |
| 39 | + try { |
| 40 | + healthMonitor.addCheck({ handler: () => {}, id: 'foo' }); |
| 41 | + |
| 42 | + fail(); |
| 43 | + } catch (e) { |
| 44 | + expect(e).toBeInstanceOf(Error); |
| 45 | + expect(e.message).toBe('Cannot add handler since it would overwrite an existing one'); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it('should add the check and setup the state', () => { |
| 50 | + healthMonitor.addCheck({ handler: () => {}, id: 'foo' }); |
| 51 | + |
| 52 | + expect(healthMonitor.checks).toHaveLength(1); |
| 53 | + expect(healthMonitor.states.foo).toBe(HealthMonitor.states.UNKNOWN); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('when running the check', () => { |
| 57 | + it('should call `healthMonitor.updateState()` with `UNHEALTHY` status if the check throws an error', done => { |
| 58 | + const handler = jest.fn(() => { |
| 59 | + throw new Error(); |
| 60 | + }); |
| 61 | + |
| 62 | + jest.spyOn(healthMonitor, 'updateState').mockImplementation(({ id, state }) => { |
| 63 | + expect(handler).toHaveBeenCalledTimes(1); |
| 64 | + expect(id).toBe('foo'); |
| 65 | + expect(state).toBe(HealthMonitor.states.UNHEALTHY); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + |
| 69 | + healthMonitor.addCheck({ handler, id: 'foo', interval: 0 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should call `healthMonitor.updateState()` with `UNHEALTHY` status if the check returns a falsy value', done => { |
| 73 | + const handler = jest.fn(() => {}); |
| 74 | + |
| 75 | + jest.spyOn(healthMonitor, 'updateState').mockImplementation(({ id, state }) => { |
| 76 | + expect(handler).toHaveBeenCalledTimes(1); |
| 77 | + expect(id).toBe('foo'); |
| 78 | + expect(state).toBe(HealthMonitor.states.UNHEALTHY); |
| 79 | + done(); |
| 80 | + }); |
| 81 | + |
| 82 | + healthMonitor.addCheck({ handler, id: 'foo', interval: 0 }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should call `healthMonitor.updateState()` with `HEALTHY` status if the check returns a truthy value', done => { |
| 86 | + const handler = jest.fn(() => true); |
| 87 | + |
| 88 | + jest.spyOn(healthMonitor, 'updateState').mockImplementation(({ id, state }) => { |
| 89 | + expect(handler).toHaveBeenCalledTimes(1); |
| 90 | + expect(id).toBe('foo'); |
| 91 | + expect(state).toBe(HealthMonitor.states.HEALTHY); |
| 92 | + done(); |
| 93 | + }); |
| 94 | + |
| 95 | + healthMonitor.addCheck({ handler, id: 'foo', interval: 0 }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should call handle asynchronous checks', done => { |
| 99 | + const handler = jest.fn(() => Promise.resolve(true)); |
| 100 | + |
| 101 | + jest.spyOn(healthMonitor, 'updateState').mockImplementation(({ id, state }) => { |
| 102 | + expect(handler).toHaveBeenCalledTimes(1); |
| 103 | + expect(id).toBe('foo'); |
| 104 | + expect(state).toBe(HealthMonitor.states.HEALTHY); |
| 105 | + done(); |
| 106 | + }); |
| 107 | + |
| 108 | + healthMonitor.addCheck({ handler, id: 'foo', interval: 0 }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('cleanup()', () => { |
| 114 | + it('should clear all checks and states currently running', async () => { |
| 115 | + healthMonitor.checks.push(setInterval(() => {}, 5000)); |
| 116 | + healthMonitor.states.foo = HealthMonitor.states.HEALTHY; |
| 117 | + healthMonitor.globalState = HealthMonitor.states.HEALTHY; |
| 118 | + |
| 119 | + healthMonitor.cleanup(); |
| 120 | + |
| 121 | + expect(healthMonitor.checks).toHaveLength(0); |
| 122 | + expect(healthMonitor.states).toEqual({}); |
| 123 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.UNKNOWN); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('updateState()', () => { |
| 128 | + it('should not update the component state if it has not changed', () => { |
| 129 | + healthMonitor.states.foo = HealthMonitor.states.HEALTHY; |
| 130 | + |
| 131 | + jest.spyOn(log, 'info'); |
| 132 | + |
| 133 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.HEALTHY }); |
| 134 | + |
| 135 | + expect(log.info).not.toHaveBeenCalled(); |
| 136 | + |
| 137 | + expect(healthMonitor.states.foo).toBe(HealthMonitor.states.HEALTHY); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should update the component state if it has changed', () => { |
| 141 | + healthMonitor.states.foo = HealthMonitor.states.HEALTHY; |
| 142 | + healthMonitor.globalState = HealthMonitor.states.UNHEALTHY; |
| 143 | + |
| 144 | + jest.spyOn(log, 'info'); |
| 145 | + |
| 146 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.UNHEALTHY }); |
| 147 | + |
| 148 | + expect(log.info).toHaveBeenCalledTimes(1); |
| 149 | + expect(log.info).toHaveBeenCalledWith({ |
| 150 | + id: 'foo', |
| 151 | + newState: HealthMonitor.states.UNHEALTHY, |
| 152 | + oldState: HealthMonitor.states.HEALTHY |
| 153 | + }, 'Component health status has changed'); |
| 154 | + |
| 155 | + expect(healthMonitor.states.foo).toBe(HealthMonitor.states.UNHEALTHY); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should not update the global state if it has not changed', () => { |
| 159 | + healthMonitor.states.foo = HealthMonitor.states.UNHEALTHY; |
| 160 | + healthMonitor.globalState = HealthMonitor.states.HEALTHY; |
| 161 | + |
| 162 | + jest.spyOn(log, 'info'); |
| 163 | + |
| 164 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.HEALTHY }); |
| 165 | + |
| 166 | + expect(log.info).toHaveBeenCalledTimes(1); |
| 167 | + |
| 168 | + expect(healthMonitor.states.foo).toBe(HealthMonitor.states.HEALTHY); |
| 169 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.HEALTHY); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should update the global state if it has changed', () => { |
| 173 | + healthMonitor.states.foo = HealthMonitor.states.HEALTHY; |
| 174 | + healthMonitor.globalState = HealthMonitor.states.HEALTHY; |
| 175 | + |
| 176 | + jest.spyOn(log, 'info'); |
| 177 | + |
| 178 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.UNHEALTHY }); |
| 179 | + |
| 180 | + expect(log.info).toHaveBeenCalledTimes(2); |
| 181 | + expect(log.info).toHaveBeenLastCalledWith({ |
| 182 | + newState: HealthMonitor.states.UNHEALTHY, |
| 183 | + oldState: HealthMonitor.states.HEALTHY |
| 184 | + }, 'Global health status has changed'); |
| 185 | + |
| 186 | + expect(healthMonitor.states.foo).toBe(HealthMonitor.states.UNHEALTHY); |
| 187 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.UNHEALTHY); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('global state', () => { |
| 191 | + it('should be UNKNOWN if at least one of the components is in the UNKNOWN state', () => { |
| 192 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.HEALTHY }); |
| 193 | + healthMonitor.updateState({ id: 'bar', state: HealthMonitor.states.UNHEALTHY }); |
| 194 | + healthMonitor.updateState({ id: 'biz', state: HealthMonitor.states.UNKNOWN }); |
| 195 | + |
| 196 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.UNKNOWN); |
| 197 | + expect(healthMonitor.states).toEqual({ |
| 198 | + bar: HealthMonitor.states.UNHEALTHY, |
| 199 | + biz: HealthMonitor.states.UNKNOWN, |
| 200 | + foo: HealthMonitor.states.HEALTHY |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should be UNHEALTHY if no component is in the UNKNOWN state and at least one of the components is in the UNHEALTHY state', () => { |
| 205 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.HEALTHY }); |
| 206 | + healthMonitor.updateState({ id: 'bar', state: HealthMonitor.states.UNHEALTHY }); |
| 207 | + healthMonitor.updateState({ id: 'biz', state: HealthMonitor.states.HEALTHY }); |
| 208 | + |
| 209 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.UNHEALTHY); |
| 210 | + expect(healthMonitor.states).toEqual({ |
| 211 | + bar: HealthMonitor.states.UNHEALTHY, |
| 212 | + biz: HealthMonitor.states.HEALTHY, |
| 213 | + foo: HealthMonitor.states.HEALTHY |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should be HEALTHY if all components are in the HEALTHY state', () => { |
| 218 | + healthMonitor.updateState({ id: 'foo', state: HealthMonitor.states.HEALTHY }); |
| 219 | + healthMonitor.updateState({ id: 'bar', state: HealthMonitor.states.HEALTHY }); |
| 220 | + healthMonitor.updateState({ id: 'biz', state: HealthMonitor.states.HEALTHY }); |
| 221 | + |
| 222 | + expect(healthMonitor.globalState).toBe(HealthMonitor.states.HEALTHY); |
| 223 | + expect(healthMonitor.states).toEqual({ |
| 224 | + bar: HealthMonitor.states.HEALTHY, |
| 225 | + biz: HealthMonitor.states.HEALTHY, |
| 226 | + foo: HealthMonitor.states.HEALTHY |
| 227 | + }); |
| 228 | + }); |
| 229 | + }); |
| 230 | + }); |
| 231 | +}); |
0 commit comments