Skip to content

Commit

Permalink
fix: Test and bugfixes (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Dec 13, 2024
1 parent c9cb0a2 commit 6ae2d36
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
12 changes: 7 additions & 5 deletions src/Centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,13 @@ export class Centrifuge {
const sharedSubject = new Subject<Observable<T>>()
function createShared(): Observable<T> {
const $shared = observableCallback().pipe(
shareReplayWithDelayedReset({
bufferSize: (options?.cache ?? true) ? 1 : 0,
resetDelay: (options?.cache === false ? 0 : (options?.observableCacheTime ?? 60)) * 1000,
windowTime: (options?.valueCacheTime ?? Infinity) * 1000,
})
keys
? shareReplayWithDelayedReset({
bufferSize: (options?.cache ?? true) ? 1 : 0,
resetDelay: (options?.cache === false ? 0 : (options?.observableCacheTime ?? 60)) * 1000,
windowTime: (options?.valueCacheTime ?? Infinity) * 1000,
})
: map((val) => val)
)
sharedSubject.next($shared)
return $shared
Expand Down
6 changes: 4 additions & 2 deletions src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { catchError, combineLatest, map, of, switchMap, timeout } from 'rxjs'
import type { Centrifuge } from './Centrifuge.js'
import { Entity } from './Entity.js'
import { PoolNetwork } from './PoolNetwork.js'
import { PoolMetadata } from './types/poolMetadata.js'
import { Reports } from './Reports/index.js'
import { PoolMetadata } from './types/poolMetadata.js'

export class Pool extends Entity {
constructor(
Expand Down Expand Up @@ -80,7 +80,9 @@ export class Pool extends Entity {
return combineLatest(
networks.map((network) =>
network.isActive().pipe(
timeout(8000),
// Because this is fetching from multiple networks and we're waiting on all of them before returning a value,
// we want a timeout in case one of the endpoints is too slow
timeout({ first: 5000 }),
catchError(() => {
return of(false)
})
Expand Down
24 changes: 24 additions & 0 deletions src/PoolNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ describe('PoolNetwork', () => {
// Calls should get batched
expect(fetchSpy.getCalls().length).to.equal(1)
})

it('should deploy a tranche', async () => {
const poolId = '1287682503'
const trancheId = '0x02bbf52e452ddb47103913051212382c'
const pool = new Pool(context.centrifuge, poolId)
const poolNetwork = new PoolNetwork(context.centrifuge, pool, 11155111)

const canTrancheBeDeployed = await poolNetwork.canTrancheBeDeployed(trancheId)
expect(canTrancheBeDeployed).to.equal(true)

const result = await poolNetwork.deployTranche(trancheId)
expect(result.type).to.equal('TransactionConfirmed')
})

it('should deploy a vault', async () => {
const poolId = '1287682503'
const trancheId = '0x02bbf52e452ddb47103913051212382c'
const pool = new Pool(context.centrifuge, poolId)
const poolNetwork = new PoolNetwork(context.centrifuge, pool, 11155111)
const tUSD = '0x8503b4452Bf6238cC76CdbEE223b46d7196b1c93'

const result = await poolNetwork.deployVault(trancheId, tUSD)
expect(result.type).to.equal('TransactionConfirmed')
})
})
31 changes: 20 additions & 11 deletions src/tests/Centrifuge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Centrifuge', () => {

it('should cache the latest value by default', async () => {
let subscribedTimes = 0
const query1 = context.centrifuge._query(null, () =>
const query1 = context.centrifuge._query([Math.random()], () =>
defer(() => {
subscribedTimes++
return lazy(1)
Expand All @@ -93,9 +93,18 @@ describe('Centrifuge', () => {
expect(value3).to.equal(1)
})

it("should't cache the value when no keys are passed", async () => {
let value = 0
const query = context.centrifuge._query(null, () => defer(() => lazy(++value)))
const value1 = await query
const value2 = await query
expect(value1).to.equal(1)
expect(value2).to.equal(2)
})

it("should invalidate the cache when there's no subscribers for a while on an infinite observable", async () => {
const subject = new Subject()
const query1 = context.centrifuge._query(null, () => subject)
const query1 = context.centrifuge._query([Math.random()], () => subject)
setTimeout(() => subject.next(1), 10)
const value1 = await query1
setTimeout(() => subject.next(2), 10)
Expand All @@ -113,7 +122,7 @@ describe('Centrifuge', () => {

it('should invalidate the cache when a finite observable completes, when given a `valueCacheTime`', async () => {
let value = 0
const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), { valueCacheTime: 1 })
const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), { valueCacheTime: 1 })
const value1 = await query1
const value2 = await query1
clock.tick(1_000)
Expand All @@ -126,7 +135,7 @@ describe('Centrifuge', () => {
it("shouldn't cache the latest value when `cache` is `false`", async () => {
let value = 0
const query1 = context.centrifuge._query(
null,
[Math.random()],
() =>
defer(() => {
value++
Expand All @@ -144,7 +153,7 @@ describe('Centrifuge', () => {

it("shouldn't reset the cache with a longer `observableCacheTime`", async () => {
let value = 0
const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), {
const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), {
observableCacheTime: Infinity,
})
const value1 = await query1
Expand All @@ -156,7 +165,7 @@ describe('Centrifuge', () => {

it('should push new data for new subscribers to old subscribers', async () => {
let value = 0
const query1 = context.centrifuge._query(null, () => defer(() => lazy(++value)), { valueCacheTime: 1 })
const query1 = context.centrifuge._query([Math.random()], () => defer(() => lazy(++value)), { valueCacheTime: 1 })
let lastValue: number | null = null
const subscription = query1.subscribe((next) => (lastValue = next))
await query1
Expand All @@ -168,8 +177,8 @@ describe('Centrifuge', () => {
})

it('should cache nested queries', async () => {
const query1 = context.centrifuge._query(['key1'], () => interval(50).pipe(map((i) => i + 1)))
const query2 = context.centrifuge._query(['key2'], () => interval(50).pipe(map((i) => (i + 1) * 2)))
const query1 = context.centrifuge._query([Math.random()], () => interval(50).pipe(map((i) => i + 1)))
const query2 = context.centrifuge._query([Math.random()], () => interval(50).pipe(map((i) => (i + 1) * 2)))
const query3 = context.centrifuge._query(null, () =>
combineLatest([query1, query2]).pipe(map(([v1, v2]) => v1 + v2))
)
Expand All @@ -184,8 +193,8 @@ describe('Centrifuge', () => {

it('should update dependant queries with values from dependencies', async () => {
let i = 0
const query1 = context.centrifuge._query(['key3'], () => of(++i), { valueCacheTime: 120 })
const query2 = context.centrifuge._query(['key4'], () => query1.pipe(map((v1) => v1 * 10)))
const query1 = context.centrifuge._query([Math.random()], () => of(++i), { valueCacheTime: 120 })
const query2 = context.centrifuge._query([Math.random()], () => query1.pipe(map((v1) => v1 * 10)))
const value1 = await query2
clock.tick(150_000)
const value3 = await query2
Expand All @@ -196,7 +205,7 @@ describe('Centrifuge', () => {
it('should recreate the shared observable when the cached value is expired', async () => {
let i = 0
const query1 = context.centrifuge._query(
null,
[Math.random()],
() =>
defer(async function* () {
yield await lazy(`${++i}-A`)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/tenderly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class TenderlyFork {
display_name: `Centrifuge Sepolia Fork ${timestamp}`,
fork_config: {
network_id: this.chain.id,
block_number: '7116950',
block_number: '7210089',
},
virtual_network_config: {
chain_config: {
Expand Down

0 comments on commit 6ae2d36

Please sign in to comment.