fn test_shared_cache()

in cachelib/rust/src/lrucache.rs [1366:1431]


    fn test_shared_cache(fb: FacebookInit) -> Result<()> {
        // All in same test to avoid race conditions when creating shared cache
        let temp_dir = create_temp_dir("test_shared_cache");
        create_shared_cache(fb, temp_dir.path().into());

        // Test set
        let pool = get_or_create_pool("find_pool_by_name", 4 * 1024 * 1024)?;
        assert!(
            pool.set(b"rimmer", Bytes::from(b"I am a fish".as_ref()))
                .unwrap(),
            "Set failed"
        );

        // Fetch an item that doesn't exist
        assert_eq!(
            pool.get(b"doesnotexist").unwrap(),
            None,
            "Successfully fetched a bad value"
        );

        // Test get
        assert_eq!(
            pool.get(b"rimmer").unwrap(),
            Some(Bytes::from(b"I am a fish".as_ref())),
            "Fetch failed"
        );

        // Test that RemoteHandle length is correct
        pool.set(b"not-rimmer", Bytes::from(b"I am a fish".as_ref()))?;
        assert_eq!(
            pool.get_handle(b"not-rimmer")?
                .unwrap()
                .get_remote_handle()?
                .get_length(),
            b"I am a fish".len(),
            "RemoteHandle handle length is incorrect"
        );

        // Test that two data offsets are not the same
        assert_ne!(
            pool.get_handle(b"rimmer")?
                .unwrap()
                .get_remote_handle()?
                .get_offset(),
            pool.get_handle(b"not-rimmer")?
                .unwrap()
                .get_remote_handle()?
                .get_offset(),
            "Two handles have same offset"
        );

        // Test getting pool by name
        let pool = get_pool("find_pool_by_name").unwrap();
        assert_eq!(
            pool.get(b"rimmer").unwrap(),
            Some(Bytes::from(b"I am a fish".as_ref())),
            "Fetch failed"
        );

        // Test getting nonexistent pool
        assert!(
            get_pool("There is no pool").is_none(),
            "non-existent pool found"
        );
        Ok(())
    }