From c4913e6f4a4e81ca22a914ae81dbeadc04f21b78 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Sun, 12 Sep 2021 01:58:05 +0800 Subject: [PATCH] itest: require server being started when creating node We now require the lnd to be fully started when creating a new node using newNode. --- lntest/node.go | 35 +++++++++++++++++++++++++++++++++++ lntest/timeouts.go | 4 ++++ lntest/timeouts_darwin.go | 4 ++++ lntest/timeouts_etcd.go | 4 ++++ 4 files changed, 47 insertions(+) diff --git a/lntest/node.go b/lntest/node.go index 1b33d55d1..da60349f1 100644 --- a/lntest/node.go +++ b/lntest/node.go @@ -397,6 +397,7 @@ type HarnessNode struct { WalletKitClient walletrpc.WalletKitClient Watchtower watchtowerrpc.WatchtowerClient WatchtowerClient wtclientrpc.WatchtowerClientClient + StateClient lnrpc.StateClient // backupDbDir is the path where a database backup is stored, if any. backupDbDir string @@ -940,6 +941,34 @@ func (hn *HarnessNode) Unlock(ctx context.Context, return hn.initClientWhenReady(DefaultTimeout) } +// waitTillServerStarted makes a subscription to the server's state change and +// blocks until the server is in state ServerActive. +func (hn *HarnessNode) waitTillServerStarted() error { + ctxb := context.Background() + ctxt, cancel := context.WithTimeout(ctxb, NodeStartTimeout) + defer cancel() + + client, err := hn.StateClient.SubscribeState( + ctxt, &lnrpc.SubscribeStateRequest{}, + ) + if err != nil { + return fmt.Errorf("failed to subscribe to state: %w", err) + } + + for { + resp, err := client.Recv() + if err != nil { + return fmt.Errorf("failed to receive state "+ + "client stream: %w", err) + } + + if resp.State == lnrpc.WalletState_SERVER_ACTIVE { + return nil + } + } + +} + // initLightningClient constructs the grpc LightningClient from the given client // connection and subscribes the harness node to graph topology updates. // This method also spawns a lightning network watcher for this node, @@ -955,6 +984,12 @@ func (hn *HarnessNode) initLightningClient(conn *grpc.ClientConn) error { hn.Watchtower = watchtowerrpc.NewWatchtowerClient(conn) hn.WatchtowerClient = wtclientrpc.NewWatchtowerClientClient(conn) hn.SignerClient = signrpc.NewSignerClient(conn) + hn.StateClient = lnrpc.NewStateClient(conn) + + // Wait until the server is fully started. + if err := hn.waitTillServerStarted(); err != nil { + return err + } // Set the harness node's pubkey to what the node claims in GetInfo. // Since the RPC might not be immediately active, we wrap the call in a diff --git a/lntest/timeouts.go b/lntest/timeouts.go index 88ccdcb22..0070c49e4 100644 --- a/lntest/timeouts.go +++ b/lntest/timeouts.go @@ -24,4 +24,8 @@ const ( // AsyncBenchmarkTimeout is the timeout used when running the async // payments benchmark. AsyncBenchmarkTimeout = 2 * time.Minute + + // NodeStartTimeout is the timeout value when waiting for a node to + // become fully started. + NodeStartTimeout = time.Second * 60 ) diff --git a/lntest/timeouts_darwin.go b/lntest/timeouts_darwin.go index 114f408cb..4c4bdfe45 100644 --- a/lntest/timeouts_darwin.go +++ b/lntest/timeouts_darwin.go @@ -25,4 +25,8 @@ const ( // payments benchmark. This timeout takes considerably longer on darwin // after go1.12 corrected its use of fsync. AsyncBenchmarkTimeout = time.Minute * 3 + + // NodeStartTimeout is the timeout value when waiting for a node to + // become fully started. + NodeStartTimeout = time.Second * 120 ) diff --git a/lntest/timeouts_etcd.go b/lntest/timeouts_etcd.go index b4e0825f4..28a248307 100644 --- a/lntest/timeouts_etcd.go +++ b/lntest/timeouts_etcd.go @@ -24,4 +24,8 @@ const ( // AsyncBenchmarkTimeout is the timeout used when running the async // payments benchmark. AsyncBenchmarkTimeout = 2 * time.Minute + + // NodeStartTimeout is the timeout value when waiting for a node to + // become fully started. + NodeStartTimeout = time.Second * 60 )