|
| 1 | +const { expect, AssertionError } = require('chai'); |
| 2 | +const { Thrift } = require('thrift'); |
| 3 | +const HiveDriverError = require('../../../../dist/errors/HiveDriverError').default; |
| 4 | +const BaseCommand = require('../../../../dist/hive/Commands/BaseCommand').default; |
| 5 | +const globalConfig = require('../../../../dist/globalConfig').default; |
| 6 | + |
| 7 | +const savedGlobalConfig = { ...globalConfig }; |
| 8 | + |
| 9 | +class ThriftClientMock { |
| 10 | + constructor(methodHandler) { |
| 11 | + this.methodHandler = methodHandler; |
| 12 | + } |
| 13 | + |
| 14 | + CustomMethod(request, callback) { |
| 15 | + try { |
| 16 | + const response = this.methodHandler(); |
| 17 | + return callback(undefined, response !== undefined ? response : ThriftClientMock.defaultResponse); |
| 18 | + } catch (error) { |
| 19 | + return callback(error); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +ThriftClientMock.defaultResponse = { |
| 25 | + status: { statusCode: 0 }, |
| 26 | +}; |
| 27 | + |
| 28 | +class CustomCommand extends BaseCommand { |
| 29 | + execute(request) { |
| 30 | + return this.executeCommand(request, this.client.CustomMethod); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('BaseCommand', () => { |
| 35 | + afterEach(() => { |
| 36 | + Object.assign(globalConfig, savedGlobalConfig); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should fail if trying to invoke non-existing command', async () => { |
| 40 | + const command = new CustomCommand({}); |
| 41 | + |
| 42 | + try { |
| 43 | + await command.execute(); |
| 44 | + expect.fail('It should throw an error'); |
| 45 | + } catch (error) { |
| 46 | + if (error instanceof AssertionError) { |
| 47 | + throw error; |
| 48 | + } |
| 49 | + expect(error).to.be.instanceof(HiveDriverError); |
| 50 | + expect(error.message).to.contain('the operation does not exist'); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle exceptions thrown by command', async () => { |
| 55 | + const errorMessage = 'Unexpected error'; |
| 56 | + |
| 57 | + const command = new CustomCommand({ |
| 58 | + CustomMethod() { |
| 59 | + throw new Error(errorMessage); |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + try { |
| 64 | + await command.execute(); |
| 65 | + expect.fail('It should throw an error'); |
| 66 | + } catch (error) { |
| 67 | + if (error instanceof AssertionError) { |
| 68 | + throw error; |
| 69 | + } |
| 70 | + expect(error).to.be.instanceof(Error); |
| 71 | + expect(error.message).to.contain(errorMessage); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + [429, 503].forEach((statusCode) => { |
| 76 | + describe(`HTTP ${statusCode} error`, () => { |
| 77 | + it('should fail on max retry attempts exceeded', async () => { |
| 78 | + globalConfig.retriesTimeout = 200; // ms |
| 79 | + globalConfig.retryDelayMin = 5; // ms |
| 80 | + globalConfig.retryDelayMax = 20; // ms |
| 81 | + globalConfig.retryMaxAttempts = 3; |
| 82 | + |
| 83 | + let methodCallCount = 0; |
| 84 | + const command = new CustomCommand( |
| 85 | + new ThriftClientMock(() => { |
| 86 | + methodCallCount += 1; |
| 87 | + const error = new Thrift.TApplicationException(); |
| 88 | + error.statusCode = statusCode; |
| 89 | + throw error; |
| 90 | + }), |
| 91 | + ); |
| 92 | + |
| 93 | + try { |
| 94 | + await command.execute(); |
| 95 | + expect.fail('It should throw an error'); |
| 96 | + } catch (error) { |
| 97 | + if (error instanceof AssertionError) { |
| 98 | + throw error; |
| 99 | + } |
| 100 | + expect(error).to.be.instanceof(HiveDriverError); |
| 101 | + expect(error.message).to.contain(`${statusCode} when connecting to resource`); |
| 102 | + expect(error.message).to.contain('Max retry count exceeded'); |
| 103 | + expect(methodCallCount).to.equal(globalConfig.retryMaxAttempts); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it('should fail on retry timeout exceeded', async () => { |
| 108 | + globalConfig.retriesTimeout = 200; // ms |
| 109 | + globalConfig.retryDelayMin = 5; // ms |
| 110 | + globalConfig.retryDelayMax = 20; // ms |
| 111 | + globalConfig.retryMaxAttempts = 50; |
| 112 | + |
| 113 | + let methodCallCount = 0; |
| 114 | + const command = new CustomCommand( |
| 115 | + new ThriftClientMock(() => { |
| 116 | + methodCallCount += 1; |
| 117 | + const error = new Thrift.TApplicationException(); |
| 118 | + error.statusCode = statusCode; |
| 119 | + throw error; |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + try { |
| 124 | + await command.execute(); |
| 125 | + expect.fail('It should throw an error'); |
| 126 | + } catch (error) { |
| 127 | + if (error instanceof AssertionError) { |
| 128 | + throw error; |
| 129 | + } |
| 130 | + expect(error).to.be.instanceof(HiveDriverError); |
| 131 | + expect(error.message).to.contain(`${statusCode} when connecting to resource`); |
| 132 | + expect(error.message).to.contain('Retry timeout exceeded'); |
| 133 | + // We set pretty low intervals/timeouts to make this test pass faster, but it also means |
| 134 | + // that it's harder to predict how much times command will be invoked. So we check that |
| 135 | + // it is within some meaningful range to reduce false-negatives |
| 136 | + expect(methodCallCount).to.be.within(10, 20); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it('should succeed after few attempts', async () => { |
| 141 | + globalConfig.retriesTimeout = 200; // ms |
| 142 | + globalConfig.retryDelayMin = 5; // ms |
| 143 | + globalConfig.retryDelayMax = 20; // ms |
| 144 | + globalConfig.retryMaxAttempts = 5; |
| 145 | + |
| 146 | + let methodCallCount = 0; |
| 147 | + const command = new CustomCommand( |
| 148 | + new ThriftClientMock(() => { |
| 149 | + methodCallCount += 1; |
| 150 | + if (methodCallCount <= 3) { |
| 151 | + const error = new Thrift.TApplicationException(); |
| 152 | + error.statusCode = statusCode; |
| 153 | + throw error; |
| 154 | + } |
| 155 | + return ThriftClientMock.defaultResponse; |
| 156 | + }), |
| 157 | + ); |
| 158 | + |
| 159 | + const response = await command.execute(); |
| 160 | + expect(response).to.deep.equal(ThriftClientMock.defaultResponse); |
| 161 | + expect(methodCallCount).to.equal(4); // 3 failed attempts + 1 succeeded |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it(`should re-throw unrecognized HTTP errors`, async () => { |
| 167 | + const errorMessage = 'Unrecognized HTTP error'; |
| 168 | + |
| 169 | + const command = new CustomCommand( |
| 170 | + new ThriftClientMock(() => { |
| 171 | + const error = new Thrift.TApplicationException(undefined, errorMessage); |
| 172 | + error.statusCode = 500; |
| 173 | + throw error; |
| 174 | + }), |
| 175 | + ); |
| 176 | + |
| 177 | + try { |
| 178 | + await command.execute(); |
| 179 | + expect.fail('It should throw an error'); |
| 180 | + } catch (error) { |
| 181 | + if (error instanceof AssertionError) { |
| 182 | + throw error; |
| 183 | + } |
| 184 | + expect(error).to.be.instanceof(Thrift.TApplicationException); |
| 185 | + expect(error.message).to.contain(errorMessage); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + it(`should re-throw unrecognized Thrift errors`, async () => { |
| 190 | + const errorMessage = 'Unrecognized HTTP error'; |
| 191 | + |
| 192 | + const command = new CustomCommand( |
| 193 | + new ThriftClientMock(() => { |
| 194 | + throw new Thrift.TApplicationException(undefined, errorMessage); |
| 195 | + }), |
| 196 | + ); |
| 197 | + |
| 198 | + try { |
| 199 | + await command.execute(); |
| 200 | + expect.fail('It should throw an error'); |
| 201 | + } catch (error) { |
| 202 | + if (error instanceof AssertionError) { |
| 203 | + throw error; |
| 204 | + } |
| 205 | + expect(error).to.be.instanceof(Thrift.TApplicationException); |
| 206 | + expect(error.message).to.contain(errorMessage); |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + it(`should re-throw unrecognized errors`, async () => { |
| 211 | + const errorMessage = 'Unrecognized error'; |
| 212 | + |
| 213 | + const command = new CustomCommand( |
| 214 | + new ThriftClientMock(() => { |
| 215 | + throw new Error(errorMessage); |
| 216 | + }), |
| 217 | + ); |
| 218 | + |
| 219 | + try { |
| 220 | + await command.execute(); |
| 221 | + expect.fail('It should throw an error'); |
| 222 | + } catch (error) { |
| 223 | + if (error instanceof AssertionError) { |
| 224 | + throw error; |
| 225 | + } |
| 226 | + expect(error).to.be.instanceof(Error); |
| 227 | + expect(error.message).to.contain(errorMessage); |
| 228 | + } |
| 229 | + }); |
| 230 | +}); |
0 commit comments