|
1 | 1 | import type OnyxInstance from '../../lib/Onyx'; |
2 | 2 | import type OnyxCache from '../../lib/OnyxCache'; |
3 | 3 | import type {CacheTask} from '../../lib/OnyxCache'; |
4 | | -import type {Connection} from '../../lib/OnyxConnectionManager'; |
5 | 4 | import type MockedStorage from '../../lib/storage/__mocks__'; |
6 | 5 | import type {InitOptions} from '../../lib/types'; |
7 | | -import generateRange from '../utils/generateRange'; |
8 | 6 | import waitForPromisesToResolve from '../utils/waitForPromisesToResolve'; |
9 | 7 |
|
10 | 8 | const MOCK_TASK = 'mockTask' as CacheTask; |
@@ -457,211 +455,6 @@ describe('Onyx', () => { |
457 | 455 | cache = require('../../lib/OnyxCache').default; |
458 | 456 | }); |
459 | 457 |
|
460 | | - it('Should keep recently accessed items in cache', () => { |
461 | | - // Given Storage with 10 different keys |
462 | | - StorageMock.getItem.mockResolvedValue('"mockValue"'); |
463 | | - const range = generateRange(0, 10); |
464 | | - StorageMock.getAllKeys.mockResolvedValue(range.map((number) => `${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}${number}`)); |
465 | | - let connections: Array<{key: string; connection: Connection}> = []; |
466 | | - |
467 | | - // Given Onyx is configured with max 5 keys in cache |
468 | | - return initOnyx({maxCachedKeysCount: 5}) |
469 | | - .then(() => { |
470 | | - // Given 10 connections for different keys |
471 | | - connections = range.map((number) => { |
472 | | - const key = `${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}${number}`; |
473 | | - return { |
474 | | - key, |
475 | | - connection: Onyx.connect({key, callback: jest.fn()}), |
476 | | - }; |
477 | | - }); |
478 | | - }) |
479 | | - .then(waitForPromisesToResolve) |
480 | | - .then(() => { |
481 | | - // When a new connection for a safe eviction key happens |
482 | | - Onyx.connect({key: `${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}10`, callback: jest.fn()}); |
483 | | - }) |
484 | | - .then(waitForPromisesToResolve) |
485 | | - .then(() => { |
486 | | - // The newly connected key should remain in cache |
487 | | - expect(cache.hasCacheForKey(`${ONYX_KEYS.COLLECTION.MOCK_COLLECTION}10`)).toBe(true); |
488 | | - |
489 | | - // With the updated implementation, all evictable keys are removed except the most recently added one |
490 | | - // Each time we connect to a safe eviction key, we remove all other evictable keys |
491 | | - for (const {key} of connections) { |
492 | | - expect(cache.hasCacheForKey(key)).toBe(false); |
493 | | - } |
494 | | - }); |
495 | | - }); |
496 | | - |
497 | | - it('Should clean cache when connections to eviction keys happen', () => { |
498 | | - // Given storage with some data |
499 | | - StorageMock.getItem.mockResolvedValue('"mockValue"'); |
500 | | - const range = generateRange(0, 10); |
501 | | - const keyPrefix = ONYX_KEYS.COLLECTION.MOCK_COLLECTION; |
502 | | - StorageMock.getAllKeys.mockResolvedValue(range.map((number) => `${keyPrefix}${number}`)); |
503 | | - let connections: Array<{key: string; connection: Connection}> = []; |
504 | | - |
505 | | - return initOnyx({ |
506 | | - maxCachedKeysCount: 3, |
507 | | - }) |
508 | | - .then(() => { |
509 | | - connections = range.map((number) => { |
510 | | - const key = `${keyPrefix}${number}`; |
511 | | - return { |
512 | | - key, |
513 | | - connection: Onyx.connect({key, callback: jest.fn()}), |
514 | | - }; |
515 | | - }); |
516 | | - }) |
517 | | - .then(waitForPromisesToResolve) |
518 | | - .then(() => { |
519 | | - Onyx.connect({key: `${keyPrefix}10`, callback: jest.fn()}); |
520 | | - }) |
521 | | - .then(waitForPromisesToResolve) |
522 | | - .then(() => { |
523 | | - // All previously connected evictable keys are removed |
524 | | - for (const {key} of connections) { |
525 | | - expect(cache.hasCacheForKey(key)).toBe(false); |
526 | | - } |
527 | | - |
528 | | - // Only the newly connected key should remain in cache |
529 | | - expect(cache.hasCacheForKey(`${keyPrefix}10`)).toBe(true); |
530 | | - }); |
531 | | - }); |
532 | | - |
533 | | - it('Should prioritize eviction of evictableKeys over non-evictable keys when cache limit is reached', () => { |
534 | | - const testKeys = { |
535 | | - ...ONYX_KEYS, |
536 | | - SAFE_FOR_EVICTION: 'evictable_', |
537 | | - NOT_SAFE_FOR_EVICTION: 'critical_', |
538 | | - }; |
539 | | - |
540 | | - const criticalKey1 = `${testKeys.NOT_SAFE_FOR_EVICTION}1`; |
541 | | - const criticalKey2 = `${testKeys.NOT_SAFE_FOR_EVICTION}2`; |
542 | | - const criticalKey3 = `${testKeys.NOT_SAFE_FOR_EVICTION}3`; |
543 | | - const evictableKey1 = `${testKeys.SAFE_FOR_EVICTION}1`; |
544 | | - const evictableKey2 = `${testKeys.SAFE_FOR_EVICTION}2`; |
545 | | - const evictableKey3 = `${testKeys.SAFE_FOR_EVICTION}3`; |
546 | | - const triggerKey = `${testKeys.SAFE_FOR_EVICTION}trigger`; |
547 | | - |
548 | | - StorageMock.getItem.mockResolvedValue('"mockValue"'); |
549 | | - const allKeys = [ |
550 | | - // Keys that should be evictable (these match the SAFE_FOR_EVICTION pattern) |
551 | | - evictableKey1, |
552 | | - evictableKey2, |
553 | | - evictableKey3, |
554 | | - triggerKey, |
555 | | - // Keys that should NOT be evictable |
556 | | - criticalKey1, |
557 | | - criticalKey2, |
558 | | - criticalKey3, |
559 | | - ]; |
560 | | - StorageMock.getAllKeys.mockResolvedValue(allKeys); |
561 | | - |
562 | | - return initOnyx({ |
563 | | - keys: testKeys, |
564 | | - maxCachedKeysCount: 3, |
565 | | - evictableKeys: [testKeys.SAFE_FOR_EVICTION], |
566 | | - }) |
567 | | - .then(() => { |
568 | | - // Verify keys are correctly identified as evictable or not |
569 | | - expect(cache.isEvictableKey?.(evictableKey1)).toBe(true); |
570 | | - expect(cache.isEvictableKey?.(evictableKey2)).toBe(true); |
571 | | - expect(cache.isEvictableKey?.(evictableKey3)).toBe(true); |
572 | | - expect(cache.isEvictableKey?.(triggerKey)).toBe(true); |
573 | | - expect(cache.isEvictableKey?.(criticalKey1)).toBe(false); |
574 | | - |
575 | | - // Connect to non-evictable keys first |
576 | | - Onyx.connect({key: criticalKey1, callback: jest.fn()}); |
577 | | - Onyx.connect({key: criticalKey2, callback: jest.fn()}); |
578 | | - Onyx.connect({key: criticalKey3, callback: jest.fn()}); |
579 | | - }) |
580 | | - .then(waitForPromisesToResolve) |
581 | | - .then(() => { |
582 | | - // Then connect to evictable keys |
583 | | - Onyx.connect({key: evictableKey1, callback: jest.fn()}); |
584 | | - Onyx.connect({key: evictableKey2, callback: jest.fn()}); |
585 | | - Onyx.connect({key: evictableKey3, callback: jest.fn()}); |
586 | | - }) |
587 | | - .then(waitForPromisesToResolve) |
588 | | - .then(() => { |
589 | | - // Trigger an eviction by connecting to a safe eviction key |
590 | | - Onyx.connect({key: triggerKey, callback: jest.fn()}); |
591 | | - }) |
592 | | - .then(waitForPromisesToResolve) |
593 | | - .then(() => { |
594 | | - // Previously connected evictable keys should be removed |
595 | | - expect(cache.hasCacheForKey(evictableKey1)).toBe(false); |
596 | | - expect(cache.hasCacheForKey(evictableKey2)).toBe(false); |
597 | | - expect(cache.hasCacheForKey(evictableKey3)).toBe(false); |
598 | | - |
599 | | - // Non-evictable keys should remain in cache |
600 | | - expect(cache.hasCacheForKey(criticalKey1)).toBe(true); |
601 | | - expect(cache.hasCacheForKey(criticalKey2)).toBe(true); |
602 | | - expect(cache.hasCacheForKey(criticalKey3)).toBe(true); |
603 | | - |
604 | | - // The trigger key should be in cache as it was just connected |
605 | | - expect(cache.hasCacheForKey(triggerKey)).toBe(true); |
606 | | - }); |
607 | | - }); |
608 | | - |
609 | | - it('Should not evict non-evictable keys even when cache limit is exceeded', () => { |
610 | | - const testKeys = { |
611 | | - ...ONYX_KEYS, |
612 | | - SAFE_FOR_EVICTION: 'evictable_', |
613 | | - NOT_SAFE_FOR_EVICTION: 'critical_', |
614 | | - }; |
615 | | - |
616 | | - const criticalKey1 = `${testKeys.NOT_SAFE_FOR_EVICTION}1`; |
617 | | - const criticalKey2 = `${testKeys.NOT_SAFE_FOR_EVICTION}2`; |
618 | | - const criticalKey3 = `${testKeys.NOT_SAFE_FOR_EVICTION}3`; |
619 | | - const evictableKey1 = `${testKeys.SAFE_FOR_EVICTION}1`; |
620 | | - // Additional trigger key for natural eviction |
621 | | - const triggerKey = `${testKeys.SAFE_FOR_EVICTION}trigger`; |
622 | | - |
623 | | - StorageMock.getItem.mockResolvedValue('"mockValue"'); |
624 | | - const allKeys = [ |
625 | | - evictableKey1, |
626 | | - triggerKey, |
627 | | - // Keys that should not be evicted |
628 | | - criticalKey1, |
629 | | - criticalKey2, |
630 | | - criticalKey3, |
631 | | - ]; |
632 | | - StorageMock.getAllKeys.mockResolvedValue(allKeys); |
633 | | - |
634 | | - return initOnyx({ |
635 | | - keys: testKeys, |
636 | | - maxCachedKeysCount: 2, |
637 | | - evictableKeys: [testKeys.SAFE_FOR_EVICTION], |
638 | | - }) |
639 | | - .then(() => { |
640 | | - Onyx.connect({key: criticalKey1, callback: jest.fn()}); // Should never be evicted |
641 | | - Onyx.connect({key: criticalKey2, callback: jest.fn()}); // Should never be evicted |
642 | | - Onyx.connect({key: criticalKey3, callback: jest.fn()}); // Should never be evicted |
643 | | - Onyx.connect({key: evictableKey1, callback: jest.fn()}); // Should be evicted when we connect to triggerKey |
644 | | - }) |
645 | | - .then(waitForPromisesToResolve) |
646 | | - .then(() => { |
647 | | - // Trigger eviction by connecting to another safe eviction key |
648 | | - Onyx.connect({key: triggerKey, callback: jest.fn()}); |
649 | | - }) |
650 | | - .then(waitForPromisesToResolve) |
651 | | - .then(() => { |
652 | | - // evictableKey1 should be evicted since it's an evictable key |
653 | | - expect(cache.hasCacheForKey(evictableKey1)).toBe(false); |
654 | | - |
655 | | - // Non-evictable keys should remain in cache |
656 | | - expect(cache.hasCacheForKey(criticalKey1)).toBe(true); |
657 | | - expect(cache.hasCacheForKey(criticalKey2)).toBe(true); |
658 | | - expect(cache.hasCacheForKey(criticalKey3)).toBe(true); |
659 | | - |
660 | | - // The trigger key should be in cache as it was just connected |
661 | | - expect(cache.hasCacheForKey(triggerKey)).toBe(true); |
662 | | - }); |
663 | | - }); |
664 | | - |
665 | 458 | it('should save RAM-only keys', () => { |
666 | 459 | const testKeys = { |
667 | 460 | ...ONYX_KEYS, |
|
0 commit comments