@river-build/web3
Version:
Dapps for our Space and Registry contracts
130 lines • 4.89 kB
JavaScript
/**
* @group main
*/
import { EntitlementCache } from './EntitlementCache';
class Key {
key;
toKey() {
return this.key;
}
constructor(key) {
this.key = key;
}
}
class BooleanCacheResult {
value;
cacheHit;
isPositive;
constructor(value) {
this.value = value;
this.cacheHit = false;
this.isPositive = value;
}
}
class StringCacheResult {
value;
cacheHit;
isPositive;
constructor(value) {
this.value = value;
this.cacheHit = false;
this.isPositive = value !== '';
}
}
describe('EntitlementsCacheTests', () => {
it('caches repeat positive values', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 10,
negativeCacheTTLSeconds: 10,
});
const onMiss = (_) => {
return Promise.resolve(new BooleanCacheResult(true));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe(true);
expect(cacheHit).toBe(false);
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe(true);
expect(cacheHit2).toBe(true);
});
it('caches repeat negative values', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 10,
negativeCacheTTLSeconds: 10,
});
const onMiss = (_) => {
return Promise.resolve(new BooleanCacheResult(false));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe(false);
expect(cacheHit).toBe(false);
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe(false);
expect(cacheHit2).toBe(true);
});
it('caches non-boolean positive values', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 10,
negativeCacheTTLSeconds: 10,
});
const onMiss = (_) => {
return Promise.resolve(new StringCacheResult('value'));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe('value');
expect(cacheHit).toBe(false);
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe('value');
expect(cacheHit2).toBe(true);
});
it('caches non-boolean falsy keys', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 10,
negativeCacheTTLSeconds: 10,
});
const onMiss = (_) => {
return Promise.resolve(new StringCacheResult(''));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe('');
expect(cacheHit).toBe(false);
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe('');
expect(cacheHit2).toBe(true);
});
it('positive cache values expire after ttl', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 1,
negativeCacheTTLSeconds: 10,
});
const onMiss = (_) => {
return Promise.resolve(new BooleanCacheResult(true));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe(true);
expect(cacheHit).toBe(false);
// Wait 5 seconds for the positive auth cache to expire
await new Promise((f) => setTimeout(f, 5000));
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe(true);
expect(cacheHit2).toBe(false);
});
it('negative cache values expire after ttl', async () => {
const cache = new EntitlementCache({
positiveCacheTTLSeconds: 10,
negativeCacheTTLSeconds: 1,
});
const onMiss = (_) => {
return Promise.resolve(new BooleanCacheResult(false));
};
const { value, cacheHit } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value).toBe(false);
expect(cacheHit).toBe(false);
// Wait 5 seconds for the positive auth cache to expire
await new Promise((f) => setTimeout(f, 1000));
const { value: value2, cacheHit: cacheHit2 } = await cache.executeUsingCache(new Key('key'), onMiss);
expect(value2).toBe(false);
expect(cacheHit2).toBe(false);
});
});
//# sourceMappingURL=EntitlementCache.test.js.map