|
1 | 1 | package chainfee |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "math/big" |
| 6 | + "sync" |
5 | 7 | "testing" |
6 | 8 | "time" |
7 | 9 |
|
8 | 10 | "github.com/smartcontractkit/chainlink-ccip/internal" |
| 11 | + "github.com/smartcontractkit/chainlink-ccip/pkg/consts" |
9 | 12 |
|
10 | 13 | "github.com/stretchr/testify/mock" |
11 | 14 |
|
@@ -431,3 +434,46 @@ func TestProcessor_Outcome(t *testing.T) { |
431 | 434 | }) |
432 | 435 | } |
433 | 436 | } |
| 437 | + |
| 438 | +func TestProcessor_Outcome_cacheInvalidation(t *testing.T) { |
| 439 | + lggr := logger.Test(t) |
| 440 | + ctx := tests.Context(t) |
| 441 | + |
| 442 | + obs := &asyncObserver{ |
| 443 | + lggr: lggr, |
| 444 | + cancelFunc: nil, |
| 445 | + mu: &sync.RWMutex{}, |
| 446 | + triggerSyncChan: make(chan time.Time), |
| 447 | + } |
| 448 | + |
| 449 | + p := &processor{ |
| 450 | + lggr: lggr, |
| 451 | + obs: obs, |
| 452 | + } |
| 453 | + |
| 454 | + obs.mu.Lock() |
| 455 | + obs.chainFeePriceUpdates = map[cciptypes.ChainSelector]Update{1: {}} |
| 456 | + obs.mu.Unlock() |
| 457 | + |
| 458 | + // cache is not invalidated with a normal context |
| 459 | + _, _ = p.Outcome(ctx, Outcome{}, Query{}, nil) |
| 460 | + updates := obs.getChainFeePriceUpdates(ctx, lggr) |
| 461 | + require.Len(t, updates, 1) |
| 462 | + |
| 463 | + // cache is not invalidated with invalidation context set to false |
| 464 | + ctx = context.WithValue(ctx, consts.InvalidateCacheKey, false) |
| 465 | + _, _ = p.Outcome(ctx, Outcome{}, Query{}, nil) |
| 466 | + updates = obs.getChainFeePriceUpdates(ctx, lggr) |
| 467 | + require.Len(t, updates, 1) |
| 468 | + |
| 469 | + // cache is invalidated with invalidation context set to true and a sync op is triggered |
| 470 | + wg := sync.WaitGroup{} |
| 471 | + go func() { |
| 472 | + <-obs.triggerSyncChan |
| 473 | + }() |
| 474 | + ctx = context.WithValue(ctx, consts.InvalidateCacheKey, true) |
| 475 | + _, _ = p.Outcome(ctx, Outcome{}, Query{}, nil) |
| 476 | + updates = obs.getChainFeePriceUpdates(ctx, lggr) |
| 477 | + require.Len(t, updates, 0) |
| 478 | + wg.Wait() // wait until receiving the sync operation signal |
| 479 | +} |
0 commit comments