Skip to content

Commit d4371b9

Browse files
committed
feedback from self-review of PR
1 parent f263b50 commit d4371b9

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/client/eppo-client-with-bandits.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ describe('EppoClient Bandits E2E test', () => {
434434
describe('Assignment logging deduplication', () => {
435435
let mockEvaluateFlag: jest.SpyInstance;
436436
let mockEvaluateBandit: jest.SpyInstance;
437+
// The below two variables allow easily changing what the mock evaluation functions return throughout the test
437438
let variationToReturn: string;
438439
let actionToReturn: string | null;
439440

@@ -485,6 +486,7 @@ describe('EppoClient Bandits E2E test', () => {
485486

486487
afterEach(() => {
487488
client.disableAssignmentCache();
489+
client.disableBanditAssignmentCache();
488490
});
489491

490492
afterAll(() => {
@@ -500,8 +502,8 @@ describe('EppoClient Bandits E2E test', () => {
500502

501503
expect(firstNonBanditAssignment.variation).toBe('non-bandit');
502504
expect(firstNonBanditAssignment.action).toBeNull();
503-
expect(mockLogAssignment).toHaveBeenCalledTimes(1);
504-
expect(mockLogBanditAction).not.toHaveBeenCalled();
505+
expect(mockLogAssignment).toHaveBeenCalledTimes(1); // new variation assignment
506+
expect(mockLogBanditAction).not.toHaveBeenCalled(); // no bandit assignment
505507

506508
// Assign bandit action
507509
variationToReturn = 'banner_bandit';
@@ -510,8 +512,8 @@ describe('EppoClient Bandits E2E test', () => {
510512

511513
expect(firstBanditAssignment.variation).toBe('banner_bandit');
512514
expect(firstBanditAssignment.action).toBe('toyota');
513-
expect(mockLogAssignment).toHaveBeenCalledTimes(2);
514-
expect(mockLogBanditAction).toHaveBeenCalledTimes(1);
515+
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // new variation assignment
516+
expect(mockLogBanditAction).toHaveBeenCalledTimes(1); // new bandit assignment
515517

516518
// Repeat bandit action assignment
517519
variationToReturn = 'banner_bandit';
@@ -520,8 +522,8 @@ describe('EppoClient Bandits E2E test', () => {
520522

521523
expect(secondBanditAssignment.variation).toBe('banner_bandit');
522524
expect(secondBanditAssignment.action).toBe('toyota');
523-
expect(mockLogAssignment).toHaveBeenCalledTimes(2);
524-
expect(mockLogBanditAction).toHaveBeenCalledTimes(1);
525+
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
526+
expect(mockLogBanditAction).toHaveBeenCalledTimes(1); // repeat bandit assignment
525527

526528
// New bandit action assignment
527529
variationToReturn = 'banner_bandit';
@@ -530,8 +532,8 @@ describe('EppoClient Bandits E2E test', () => {
530532

531533
expect(thirdBanditAssignment.variation).toBe('banner_bandit');
532534
expect(thirdBanditAssignment.action).toBe('honda');
533-
expect(mockLogAssignment).toHaveBeenCalledTimes(2);
534-
expect(mockLogBanditAction).toHaveBeenCalledTimes(2);
535+
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
536+
expect(mockLogBanditAction).toHaveBeenCalledTimes(2); // new bandit assignment
535537

536538
// Flip-flop to an earlier action assignment
537539
variationToReturn = 'banner_bandit';
@@ -540,8 +542,8 @@ describe('EppoClient Bandits E2E test', () => {
540542

541543
expect(fourthBanditAssignment.variation).toBe('banner_bandit');
542544
expect(fourthBanditAssignment.action).toBe('toyota');
543-
expect(mockLogAssignment).toHaveBeenCalledTimes(2);
544-
expect(mockLogBanditAction).toHaveBeenCalledTimes(3);
545+
expect(mockLogAssignment).toHaveBeenCalledTimes(2); // repeat variation assignment
546+
expect(mockLogBanditAction).toHaveBeenCalledTimes(3); // "new" bandit assignment
545547

546548
// Flip-flop back to non-bandit assignment
547549
variationToReturn = 'non-bandit';
@@ -550,8 +552,8 @@ describe('EppoClient Bandits E2E test', () => {
550552

551553
expect(secondNonBanditAssignment.variation).toBe('non-bandit');
552554
expect(secondNonBanditAssignment.action).toBeNull();
553-
expect(mockLogAssignment).toHaveBeenCalledTimes(3);
554-
expect(mockLogBanditAction).toHaveBeenCalledTimes(3);
555+
expect(mockLogAssignment).toHaveBeenCalledTimes(3); // "new" variation assignment
556+
expect(mockLogBanditAction).toHaveBeenCalledTimes(3); // no bandit assignment
555557
});
556558
});
557559
});

src/client/eppo-client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,9 @@ export default class EppoClient {
659659

660660
// What our bandit assignment cache cares about for avoiding logging duplicate bandit assignments,
661661
// if one is active. Like the flag assignment cache, entries are only stored for a given flag
662-
// and subject.
662+
// and subject. However, Bandit and action keys are also used for determining assignment uniqueness.
663+
// This means that if a flag's bandit assigns one action, and then later a new action, the first
664+
// one will be evicted from the cache. If later assigned again, it will be treated as new.
663665
const banditAssignmentCacheProperties = {
664666
flagKey,
665667
subjectKey,
@@ -668,6 +670,7 @@ export default class EppoClient {
668670
};
669671

670672
if (this.banditAssignmentCache?.has(banditAssignmentCacheProperties)) {
673+
// Ignore repeat assignment
671674
return;
672675
}
673676

@@ -678,7 +681,7 @@ export default class EppoClient {
678681
}
679682
return;
680683
}
681-
// If here, we have a logger
684+
// If here, we have a logger and a new assignment to be logged
682685
try {
683686
this.banditLogger.logBanditAction(banditEvent);
684687
this.banditAssignmentCache?.set(banditAssignmentCacheProperties);

0 commit comments

Comments
 (0)