|
2 | 2 |
|
3 | 3 | const Logger = require('../../lib/core').Logger; |
4 | 4 | const deprecateOptions = require('../../lib/utils').deprecateOptions; |
| 5 | +const arrayStrictEqual = require('../../lib/core/utils').arrayStrictEqual; |
| 6 | +const chalk = require('chalk'); |
5 | 7 | const chai = require('chai'); |
6 | 8 | const expect = chai.expect; |
7 | 9 | const sinonChai = require('sinon-chai'); |
@@ -55,10 +57,128 @@ ClassWithUndefinedLogger.prototype.getLogger = function() { |
55 | 57 | return undefined; |
56 | 58 | }; |
57 | 59 |
|
| 60 | +function diff(lhs, rhs, fields, comparator) { |
| 61 | + return fields.reduce((diff, field) => { |
| 62 | + if (lhs[field] == null || rhs[field] == null) { |
| 63 | + return diff; |
| 64 | + } |
| 65 | + |
| 66 | + if (!comparator(lhs[field], rhs[field])) { |
| 67 | + diff.push( |
| 68 | + ` ${field}: ${chalk.green(`[${lhs[field]}]`)} => ${chalk.green(`[${rhs[field]}]`)}` |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return diff; |
| 73 | + }, []); |
| 74 | +} |
| 75 | + |
| 76 | +function serverDescriptionDiff(lhs, rhs) { |
| 77 | + const objectIdFields = ['electionId']; |
| 78 | + const arrayFields = ['hosts', 'tags']; |
| 79 | + const simpleFields = [ |
| 80 | + 'type', |
| 81 | + 'minWireVersion', |
| 82 | + 'me', |
| 83 | + 'setName', |
| 84 | + 'setVersion', |
| 85 | + 'electionId', |
| 86 | + 'primary', |
| 87 | + 'logicalSessionTimeoutMinutes' |
| 88 | + ]; |
| 89 | + |
| 90 | + return diff(lhs, rhs, simpleFields, (x, y) => x === y) |
| 91 | + .concat(diff(lhs, rhs, arrayFields, (x, y) => arrayStrictEqual(x, y))) |
| 92 | + .concat(diff(lhs, rhs, objectIdFields, (x, y) => x.equals(y))) |
| 93 | + .join(',\n'); |
| 94 | +} |
| 95 | + |
| 96 | +function topologyDescriptionDiff(lhs, rhs) { |
| 97 | + const simpleFields = [ |
| 98 | + 'type', |
| 99 | + 'setName', |
| 100 | + 'maxSetVersion', |
| 101 | + 'stale', |
| 102 | + 'compatible', |
| 103 | + 'compatibilityError', |
| 104 | + 'logicalSessionTimeoutMinutes', |
| 105 | + 'error', |
| 106 | + 'commonWireVersion' |
| 107 | + ]; |
| 108 | + |
| 109 | + return diff(lhs, rhs, simpleFields, (x, y) => x === y).join(',\n'); |
| 110 | +} |
| 111 | + |
| 112 | +function visualizeMonitoringEvents(client) { |
| 113 | + function print(msg) { |
| 114 | + console.log(`${chalk.white(new Date().toISOString())} ${msg}`); |
| 115 | + } |
| 116 | + |
| 117 | + client.on('serverHeartbeatStarted', event => |
| 118 | + print(`${chalk.yellow('heartbeat')} ${chalk.bold('started')} host: '${event.connectionId}`) |
| 119 | + ); |
| 120 | + |
| 121 | + client.on('serverHeartbeatSucceeded', event => |
| 122 | + print( |
| 123 | + `${chalk.yellow('heartbeat')} ${chalk.green('succeeded')} host: '${ |
| 124 | + event.connectionId |
| 125 | + }' ${chalk.gray(`(${event.duration} ms)`)}` |
| 126 | + ) |
| 127 | + ); |
| 128 | + |
| 129 | + client.on('serverHeartbeatFailed', event => |
| 130 | + print( |
| 131 | + `${chalk.yellow('heartbeat')} ${chalk.red('failed')} host: '${ |
| 132 | + event.connectionId |
| 133 | + }' ${chalk.gray(`(${event.duration} ms)`)}` |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + // server information |
| 138 | + client.on('serverOpening', event => { |
| 139 | + print( |
| 140 | + `${chalk.cyan('server')} [${event.address}] ${chalk.bold('opening')} in topology#${ |
| 141 | + event.topologyId |
| 142 | + }` |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + client.on('serverClosed', event => { |
| 147 | + print( |
| 148 | + `${chalk.cyan('server')} [${event.address}] ${chalk.bold('closed')} in topology#${ |
| 149 | + event.topologyId |
| 150 | + }` |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + client.on('serverDescriptionChanged', event => { |
| 155 | + print(`${chalk.cyan('server')} [${event.address}] changed:`); |
| 156 | + console.log(serverDescriptionDiff(event.previousDescription, event.newDescription)); |
| 157 | + }); |
| 158 | + |
| 159 | + // topology information |
| 160 | + client.on('topologyOpening', event => { |
| 161 | + print(`${chalk.magenta('topology')} adding topology#${event.topologyId}`); |
| 162 | + }); |
| 163 | + |
| 164 | + client.on('topologyClosed', event => { |
| 165 | + print(`${chalk.magenta('topology')} removing topology#${event.topologyId}`); |
| 166 | + }); |
| 167 | + |
| 168 | + client.on('topologyDescriptionChanged', event => { |
| 169 | + const diff = topologyDescriptionDiff(event.previousDescription, event.newDescription); |
| 170 | + if (diff !== '') { |
| 171 | + print(`${chalk.magenta('topology')} [topology#${event.topologyId}] changed:`); |
| 172 | + console.log(diff); |
| 173 | + } |
| 174 | + }); |
| 175 | +} |
| 176 | + |
58 | 177 | module.exports = { |
59 | 178 | makeTestFunction, |
60 | 179 | ensureCalledWith, |
61 | 180 | ClassWithLogger, |
62 | 181 | ClassWithoutLogger, |
63 | | - ClassWithUndefinedLogger |
| 182 | + ClassWithUndefinedLogger, |
| 183 | + visualizeMonitoringEvents |
64 | 184 | }; |
0 commit comments