Skip to content

Commit

Permalink
close handing
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed May 23, 2024
1 parent 72683a3 commit 2a30e65
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/services/lnd/liquidityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class LiquidityProvider {
latestMaxWithdrawable: number | null = null
invoicePaidCb: InvoicePaidCb
connecting = false
readyInterval: NodeJS.Timeout
// make the sub process accept client
constructor(pubDestination: string, invoicePaidCb: InvoicePaidCb) {
if (!pubDestination) {
Expand All @@ -34,14 +35,18 @@ export class LiquidityProvider {
retrieveNostrUserAuth: async () => this.myPub,
}, this.clientSend, this.clientSub)

const interval = setInterval(() => {
this.readyInterval = setInterval(() => {
if (this.ready) {
clearInterval(interval)
clearInterval(this.readyInterval)
this.Connect()
}
}, 1000)
}

Stop = () => {
clearInterval(this.readyInterval)
}

Connect = async () => {
await new Promise(res => setTimeout(res, 2000))
this.log("ready")
Expand Down
1 change: 1 addition & 0 deletions src/services/lnd/lnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default class {
}
Stop() {
this.abortController.abort()
this.liquidProvider.Stop()
}

async ShouldUseLiquidityProvider(req: LiquidityRequest): Promise<boolean> {
Expand Down
3 changes: 2 additions & 1 deletion src/tests/liquidityProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export default async (T: TestBase) => {
disableLoggers([], ["EventsLogManager", "watchdog", "htlcTracker", "debugHtlcs", "debugLndBalancev3", "metrics", "mainForTest", "main"])
await safelySetUserBalance(T, T.user1, 2000)
T.d("starting liquidityProvider tests...")
const { bootstrapped, bootstrappedUser } = await initBootstrappedInstance(T)
const { bootstrapped, bootstrappedUser, stop } = await initBootstrappedInstance(T)
await testInboundPaymentFromProvider(T, bootstrapped, bootstrappedUser)
await testOutboundPaymentFromProvider(T, bootstrapped, bootstrappedUser)
stop()
await runSanityCheck(T)
}

Expand Down
3 changes: 3 additions & 0 deletions src/tests/networkSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const setupNetwork = async () => {
throw new Error("bob not synced to graph")
}
}, 15, 2000)

alice.Stop()
bob.Stop()
}

const tryUntil = async <T>(fn: (attempt: number) => Promise<T>, maxTries: number, interval: number) => {
Expand Down
13 changes: 8 additions & 5 deletions src/tests/setupBootstrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { TestBase, TestUserData } from './testBase.js'
import * as Types from '../../proto/autogenerated/ts/types.js'

export const initBootstrappedInstance = async (T: TestBase) => {
const requests = {}
const settings = LoadTestSettingsFromEnv()
settings.lndSettings.useOnlyLiquidityProvider = true
settings.lndSettings.liquidityProviderPub = T.app.publicKey
Expand All @@ -15,8 +14,8 @@ export const initBootstrappedInstance = async (T: TestBase) => {
if (!initialized) {
throw new Error("failed to initialize bootstrapped main handler")
}
const { mainHandler: bootstrapped, liquidityProviderInfo, liquidityProviderApp, apps } = initialized
T.main.attachNostrSend(async (initiator, data, r) => {
const { mainHandler: bootstrapped, liquidityProviderInfo, liquidityProviderApp } = initialized
T.main.attachNostrSend(async (_, data, r) => {
if (data.type === 'event') {
throw new Error("unsupported event type")
}
Expand All @@ -27,7 +26,7 @@ export const initBootstrappedInstance = async (T: TestBase) => {
console.log("sending new operation to provider")
bootstrapped.liquidProvider.onEvent(j, T.app.publicKey)
})
bootstrapped.liquidProvider.attachNostrSend(async (initiator, data, r) => {
bootstrapped.liquidProvider.attachNostrSend(async (_, data, r) => {
const res = await handleSend(T, data)
if (data.type === 'event') {
throw new Error("unsupported event type")
Expand All @@ -50,7 +49,11 @@ export const initBootstrappedInstance = async (T: TestBase) => {
})
const bUser = await bootstrapped.applicationManager.AddAppUser(liquidityProviderApp.appId, { identifier: "user1_bootstrapped", balance: 0, fail_if_exists: true })
const bootstrappedUser: TestUserData = { userId: bUser.info.userId, appUserIdentifier: bUser.identifier, appId: liquidityProviderApp.appId }
return { bootstrapped, liquidityProviderInfo, liquidityProviderApp, bootstrappedUser }
return {
bootstrapped, liquidityProviderInfo, liquidityProviderApp, bootstrappedUser, stop: () => {
bootstrapped.Stop()
}
}
}
type TransportRequest = { requestId: string, authIdentifier: string } & (
{ rpcName: 'GetUserInfo' } |
Expand Down
2 changes: 1 addition & 1 deletion src/tests/testRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { globby } from 'globby'
import { setupNetwork } from './networkSetup.js'
import { Describe, SetupTest, teardown, TestBase } from './testBase.js'

type TestModule = {
ignore?: boolean
dev?: boolean
Expand Down

0 comments on commit 2a30e65

Please sign in to comment.