|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { expect } = require('chai') |
| 4 | +const { describe, it, beforeEach, afterEach } = require('mocha') |
| 5 | +const sinon = require('sinon') |
| 6 | +const proxyquire = require('proxyquire') |
| 7 | +const { ProviderEvents } = require('@openfeature/server-sdk') |
| 8 | + |
| 9 | +require('../setup/mocha') |
| 10 | + |
| 11 | +describe('FlaggingProvider Initialization Timeout', () => { |
| 12 | + let FlaggingProvider |
| 13 | + let mockTracer |
| 14 | + let mockConfig |
| 15 | + let mockChannel |
| 16 | + let log |
| 17 | + let channelStub |
| 18 | + let clock |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + // Use fake timers to control setTimeout |
| 22 | + clock = sinon.useFakeTimers() |
| 23 | + |
| 24 | + mockTracer = { |
| 25 | + _config: { service: 'test-service' } |
| 26 | + } |
| 27 | + |
| 28 | + mockConfig = { |
| 29 | + service: 'test-service', |
| 30 | + version: '1.0.0', |
| 31 | + env: 'test' |
| 32 | + } |
| 33 | + |
| 34 | + mockChannel = { |
| 35 | + publish: sinon.spy(), |
| 36 | + hasSubscribers: false |
| 37 | + } |
| 38 | + |
| 39 | + channelStub = sinon.stub().returns(mockChannel) |
| 40 | + |
| 41 | + log = { |
| 42 | + debug: sinon.spy(), |
| 43 | + error: sinon.spy(), |
| 44 | + warn: sinon.spy() |
| 45 | + } |
| 46 | + |
| 47 | + FlaggingProvider = proxyquire('../../src/openfeature/flagging_provider', { |
| 48 | + 'dc-polyfill': { |
| 49 | + channel: channelStub |
| 50 | + }, |
| 51 | + '../log': log |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + // Restore real timers |
| 57 | + clock.restore() |
| 58 | + }) |
| 59 | + |
| 60 | + it('should timeout after 30 seconds when configuration is not set', async () => { |
| 61 | + const provider = new FlaggingProvider(mockTracer, mockConfig) |
| 62 | + |
| 63 | + // Start initialization (returns a promise) |
| 64 | + const initPromise = provider.initialize() |
| 65 | + |
| 66 | + // Attach catch handler BEFORE ticking clock to prevent unhandled rejection |
| 67 | + initPromise.catch(() => { |
| 68 | + // Expected to reject on timeout |
| 69 | + }) |
| 70 | + |
| 71 | + // Verify initialization is in progress |
| 72 | + expect(provider.initController).to.exist |
| 73 | + expect(provider.initController.isInitializing()).to.be.true |
| 74 | + |
| 75 | + // Advance time by 30 seconds (default timeout) and run pending promises |
| 76 | + await clock.tickAsync(30000) |
| 77 | + |
| 78 | + // Wait for promise to settle |
| 79 | + await initPromise.catch(() => {}) |
| 80 | + |
| 81 | + // Verify initialization is no longer in progress |
| 82 | + expect(provider.initController.isInitializing()).to.be.false |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not timeout if configuration is set before 30 seconds', async () => { |
| 86 | + const provider = new FlaggingProvider(mockTracer, mockConfig) |
| 87 | + |
| 88 | + // Start initialization |
| 89 | + const initPromise = provider.initialize() |
| 90 | + |
| 91 | + // Verify initialization is in progress |
| 92 | + expect(provider.initController.isInitializing()).to.be.true |
| 93 | + |
| 94 | + // Advance time by 20 seconds (before timeout) |
| 95 | + await clock.tickAsync(20000) |
| 96 | + |
| 97 | + // Set configuration before timeout |
| 98 | + const ufc = { |
| 99 | + flags: { |
| 100 | + 'test-flag': { |
| 101 | + key: 'test-flag', |
| 102 | + variations: [ |
| 103 | + { key: 'on', value: true }, |
| 104 | + { key: 'off', value: false } |
| 105 | + ] |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + provider._setConfiguration(ufc) |
| 110 | + |
| 111 | + // Wait for initialization to complete |
| 112 | + await initPromise |
| 113 | + |
| 114 | + // Verify initialization completed successfully |
| 115 | + expect(provider.initController.isInitializing()).to.be.false |
| 116 | + expect(provider.getConfiguration()).to.equal(ufc) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should call setError with timeout message after 30 seconds', async () => { |
| 120 | + const provider = new FlaggingProvider(mockTracer, mockConfig) |
| 121 | + |
| 122 | + // Spy on setError method |
| 123 | + const setErrorSpy = sinon.spy(provider, 'setError') |
| 124 | + |
| 125 | + const initPromise = provider.initialize() |
| 126 | + |
| 127 | + // Attach catch handler BEFORE ticking clock to prevent unhandled rejection |
| 128 | + initPromise.catch(() => { |
| 129 | + // Expected to reject |
| 130 | + }) |
| 131 | + |
| 132 | + // Advance time to trigger timeout |
| 133 | + await clock.tickAsync(30000) |
| 134 | + |
| 135 | + await initPromise.catch(() => {}) |
| 136 | + |
| 137 | + // Verify setError was called with timeout error |
| 138 | + expect(setErrorSpy).to.have.been.calledOnce |
| 139 | + const errorArg = setErrorSpy.firstCall.args[0] |
| 140 | + expect(errorArg).to.be.instanceOf(Error) |
| 141 | + expect(errorArg.message).to.include('Initialization timeout') |
| 142 | + expect(errorArg.message).to.include('30000ms') |
| 143 | + }) |
| 144 | + |
| 145 | + it('should allow recovery if configuration is set after timeout', async () => { |
| 146 | + const provider = new FlaggingProvider(mockTracer, mockConfig) |
| 147 | + |
| 148 | + // Spy on event emitter |
| 149 | + const readyEventSpy = sinon.spy() |
| 150 | + provider.events.addHandler(ProviderEvents.Ready, readyEventSpy) |
| 151 | + |
| 152 | + const initPromise = provider.initialize() |
| 153 | + |
| 154 | + // Attach catch handler BEFORE ticking clock to prevent unhandled rejection |
| 155 | + initPromise.catch(() => { |
| 156 | + // Expected to reject |
| 157 | + }) |
| 158 | + |
| 159 | + // Trigger timeout |
| 160 | + await clock.tickAsync(30000) |
| 161 | + |
| 162 | + // Wait for initialization to complete/fail |
| 163 | + await initPromise.catch(() => {}) |
| 164 | + |
| 165 | + // Configuration is still not set |
| 166 | + expect(provider.getConfiguration()).to.be.undefined |
| 167 | + |
| 168 | + // Now set configuration after timeout |
| 169 | + const ufc = { flags: { 'recovery-flag': {} } } |
| 170 | + provider._setConfiguration(ufc) |
| 171 | + |
| 172 | + // Should emit READY event to signal recovery |
| 173 | + expect(readyEventSpy).to.have.been.calledOnce |
| 174 | + expect(provider.getConfiguration()).to.equal(ufc) |
| 175 | + }) |
| 176 | +}) |
0 commit comments