|
1 |
| -import { locallyCached, redisCached, clearLocalCache, shutdownRedisPool, redisClearCacheKeyPattern } from './cache'; |
| 1 | +import { |
| 2 | + locallyCached, |
| 3 | + redisCached, |
| 4 | + clearLocalCache, |
| 5 | + shutdownRedisPool, |
| 6 | + redisClearCacheKeyPattern, |
| 7 | + getExperimentValue, |
| 8 | + getJoinedStressTestExperiment, |
| 9 | +} from './cache'; |
2 | 10 | import { mocked } from 'ts-jest/utils';
|
3 | 11 | import { v4 as uuidv4 } from 'uuid';
|
4 | 12 | import nock from 'nock';
|
@@ -131,6 +139,171 @@ describe('locallyCached', () => {
|
131 | 139 | });
|
132 | 140 | });
|
133 | 141 |
|
| 142 | +describe('experiment functions', () => { |
| 143 | + beforeEach(async () => { |
| 144 | + await shutdownRedisPool(); |
| 145 | + clearLocalCache(); |
| 146 | + jest.restoreAllMocks(); |
| 147 | + jest.clearAllMocks(); |
| 148 | + }); |
| 149 | + |
| 150 | + afterEach(async () => { |
| 151 | + await shutdownRedisPool(); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('getExperimentValue', () => { |
| 155 | + it('returns the value from redis when it exists', async () => { |
| 156 | + const experimentKey = 'test-experiment'; |
| 157 | + const experimentValue = '42'; |
| 158 | + const defaultValue = 10; |
| 159 | + |
| 160 | + mockedRedisClient.get.mockResolvedValueOnce(experimentValue); |
| 161 | + |
| 162 | + const result = await getExperimentValue(experimentKey, defaultValue); |
| 163 | + |
| 164 | + expect(result).toBe(42); |
| 165 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 166 | + expect(mockedRedisClient.get).toBeCalledWith('gh-ci.EXPERIMENT.test-experiment'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('returns default value when key does not exist', async () => { |
| 170 | + const experimentKey = 'missing-experiment'; |
| 171 | + const defaultValue = 10; |
| 172 | + |
| 173 | + mockedRedisClient.get.mockResolvedValueOnce(null); |
| 174 | + |
| 175 | + const result = await getExperimentValue(experimentKey, defaultValue); |
| 176 | + |
| 177 | + expect(result).toBe(defaultValue); |
| 178 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 179 | + expect(mockedRedisClient.get).toBeCalledWith('gh-ci.EXPERIMENT.missing-experiment'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('returns default value when redis throws an error', async () => { |
| 183 | + const experimentKey = 'error-experiment'; |
| 184 | + const defaultValue = 10; |
| 185 | + |
| 186 | + mockedRedisClient.get.mockRejectedValueOnce(new Error('Redis error')); |
| 187 | + |
| 188 | + const result = await getExperimentValue(experimentKey, defaultValue); |
| 189 | + |
| 190 | + expect(result).toBe(defaultValue); |
| 191 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 192 | + }); |
| 193 | + |
| 194 | + it('returns default value when value is not a valid number', async () => { |
| 195 | + const experimentKey = 'invalid-experiment'; |
| 196 | + const defaultValue = 10; |
| 197 | + |
| 198 | + mockedRedisClient.get.mockResolvedValueOnce('not-a-number'); |
| 199 | + |
| 200 | + const result = await getExperimentValue(experimentKey, defaultValue); |
| 201 | + |
| 202 | + expect(result).toBe(defaultValue); |
| 203 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('getJoinedStressTestExperiment', () => { |
| 208 | + it('returns false when RUNNER_NAME_SUFFIX is not set', async () => { |
| 209 | + mockedRedisClient.get.mockResolvedValueOnce(null); |
| 210 | + |
| 211 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name'); |
| 212 | + |
| 213 | + expect(result).toBe(false); |
| 214 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 215 | + expect(mockedRedisClient.get).toBeCalledWith('gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 216 | + }); |
| 217 | + |
| 218 | + it('returns false when runner name does not match suffix', async () => { |
| 219 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 220 | + |
| 221 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-without-match'); |
| 222 | + |
| 223 | + expect(result).toBe(false); |
| 224 | + expect(mockedRedisClient.get).toBeCalledTimes(1); |
| 225 | + expect(mockedRedisClient.get).toBeCalledWith('gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 226 | + }); |
| 227 | + |
| 228 | + it('returns false when probability is less than random value', async () => { |
| 229 | + jest.spyOn(global.Math, 'random').mockReturnValueOnce(0.6); |
| 230 | + |
| 231 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 232 | + mockedRedisClient.get.mockResolvedValueOnce('50'); |
| 233 | + |
| 234 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 235 | + |
| 236 | + expect(result).toBe(false); |
| 237 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 238 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 239 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(2, 'gh-ci.EXPERIMENT.TEST_EXPERIMENT'); |
| 240 | + }); |
| 241 | + |
| 242 | + it('returns true when probability is greater than random value', async () => { |
| 243 | + jest.spyOn(global.Math, 'random').mockReturnValueOnce(0.4); |
| 244 | + |
| 245 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 246 | + mockedRedisClient.get.mockResolvedValueOnce('50'); |
| 247 | + |
| 248 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 249 | + |
| 250 | + expect(result).toBe(true); |
| 251 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 252 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 253 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(2, 'gh-ci.EXPERIMENT.TEST_EXPERIMENT'); |
| 254 | + }); |
| 255 | + |
| 256 | + it('returns false when experiment value is zero', async () => { |
| 257 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 258 | + mockedRedisClient.get.mockResolvedValueOnce('0'); |
| 259 | + |
| 260 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 261 | + |
| 262 | + expect(result).toBe(false); |
| 263 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 264 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 265 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(2, 'gh-ci.EXPERIMENT.TEST_EXPERIMENT'); |
| 266 | + }); |
| 267 | + |
| 268 | + it('returns true when experiment value is 100', async () => { |
| 269 | + jest.spyOn(global.Math, 'random').mockReturnValueOnce(0.99); |
| 270 | + |
| 271 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 272 | + mockedRedisClient.get.mockResolvedValueOnce('100'); |
| 273 | + |
| 274 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 275 | + |
| 276 | + expect(result).toBe(true); |
| 277 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 278 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 279 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(2, 'gh-ci.EXPERIMENT.TEST_EXPERIMENT'); |
| 280 | + }); |
| 281 | + |
| 282 | + it('returns false when experiment value is not a valid number', async () => { |
| 283 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 284 | + mockedRedisClient.get.mockResolvedValueOnce('not-a-number'); |
| 285 | + |
| 286 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 287 | + |
| 288 | + expect(result).toBe(false); |
| 289 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 290 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 291 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(2, 'gh-ci.EXPERIMENT.TEST_EXPERIMENT'); |
| 292 | + }); |
| 293 | + |
| 294 | + it('returns false when experiment query throws an error', async () => { |
| 295 | + mockedRedisClient.get.mockResolvedValueOnce('-suffix'); |
| 296 | + mockedRedisClient.get.mockRejectedValueOnce(new Error('Redis error')); |
| 297 | + |
| 298 | + const result = await getJoinedStressTestExperiment('TEST_EXPERIMENT', 'runner-name-suffix'); |
| 299 | + |
| 300 | + expect(result).toBe(false); |
| 301 | + expect(mockedRedisClient.get).toBeCalledTimes(2); |
| 302 | + expect(mockedRedisClient.get).toHaveBeenNthCalledWith(1, 'gh-ci.EXPERIMENT.RUNNER_NAME_SUFFIX'); |
| 303 | + }); |
| 304 | + }); |
| 305 | +}); |
| 306 | + |
134 | 307 | describe('redisCached', () => {
|
135 | 308 | beforeEach(async () => {
|
136 | 309 | await shutdownRedisPool();
|
|
0 commit comments