diff --git a/src/client/eppo-client-with-bandits.spec.ts b/src/client/eppo-client-with-bandits.spec.ts index af6bc6a..0610a90 100644 --- a/src/client/eppo-client-with-bandits.spec.ts +++ b/src/client/eppo-client-with-bandits.spec.ts @@ -205,6 +205,8 @@ describe('EppoClient Bandits E2E test', () => { }); it('Flushed queued logging events when a logger is set', () => { + client.useLRUInMemoryAssignmentCache(5); + client.useLRUInMemoryBanditAssignmentCache(5); client.setAssignmentLogger(null as unknown as IAssignmentLogger); client.setBanditLogger(null as unknown as IBanditLogger); const banditAssignment = client.getBanditAction( @@ -221,6 +223,20 @@ describe('EppoClient Bandits E2E test', () => { expect(mockLogAssignment).not.toHaveBeenCalled(); expect(mockLogBanditAction).not.toHaveBeenCalled(); + const repeatAssignment = client.getBanditAction( + flagKey, + subjectKey, + subjectAttributes, + actions, + 'control', + ); + + expect(repeatAssignment.variation).toBe('banner_bandit'); + expect(repeatAssignment.action).toBe('adidas'); + + expect(mockLogAssignment).not.toHaveBeenCalled(); + expect(mockLogBanditAction).not.toHaveBeenCalled(); + client.setAssignmentLogger({ logAssignment: mockLogAssignment }); client.setBanditLogger({ logBanditAction: mockLogBanditAction }); diff --git a/src/client/eppo-client.ts b/src/client/eppo-client.ts index fb46a99..d791b20 100644 --- a/src/client/eppo-client.ts +++ b/src/client/eppo-client.ts @@ -670,18 +670,15 @@ export default class EppoClient { return; } - // If no logger defined, queue up the events (up to a max) to flush if a logger is later defined - if (!this.banditLogger) { - // No bandit logger set; enqueue the event in case a logger is later set - if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) { - this.queuedBanditEvents.push(banditEvent); - } - return; - } - // If here, we have a logger and a new assignment to be logged try { - this.banditLogger.logBanditAction(banditEvent); + if (this.banditLogger) { + this.banditLogger.logBanditAction(banditEvent); + } else if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) { + // If no logger defined, queue up the events (up to a max) to flush if a logger is later defined + this.queuedBanditEvents.push(banditEvent); + } + // Record in the assignment cache, if active, to deduplicate subsequent repeat assignments this.banditAssignmentCache?.set(banditAssignmentCacheProperties); } catch (err) { logger.warn('Error encountered logging bandit action', err); @@ -994,14 +991,14 @@ export default class EppoClient { } } - // assignment logger may be null while waiting for initialization - if (!this.assignmentLogger) { - this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE && - this.queuedAssignmentEvents.push(event); - return; - } try { - this.assignmentLogger.logAssignment(event); + if (this.assignmentLogger) { + this.assignmentLogger.logAssignment(event); + } else if (this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE) { + // assignment logger may be null while waiting for initialization, queue up events (up to a max) + // to be flushed when set + this.queuedAssignmentEvents.push(event); + } this.assignmentCache?.set({ flagKey, subjectKey,