Skip to content

Commit 7a3bee8

Browse files
committed
go/oasis-test-runner: Add wait methods to controller
1 parent 44005e2 commit 7a3bee8

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

go/oasis-test-runner/oasis/controller.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package oasis
22

33
import (
4+
"context"
5+
"fmt"
6+
47
"google.golang.org/grpc"
58
"google.golang.org/grpc/credentials/insecure"
69

710
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
11+
"github.com/oasisprotocol/oasis-core/go/common"
812
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
913
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
1014
control "github.com/oasisprotocol/oasis-core/go/control/api"
@@ -46,6 +50,52 @@ func (c *Controller) Close() {
4650
c.conn.Close()
4751
}
4852

53+
// WaitConsensusHeight waits until the controller observes at least specified consensus height.
54+
func (c *Controller) WaitConsensusHeight(ctx context.Context, height int64) error {
55+
blkCh, sub, err := c.Consensus.WatchBlocks(ctx)
56+
if err != nil {
57+
return fmt.Errorf("failed to watch consensus blocks: %w", err)
58+
}
59+
defer sub.Close()
60+
61+
for {
62+
select {
63+
case blk, ok := <-blkCh:
64+
if !ok {
65+
return fmt.Errorf("consensus block channel closed")
66+
}
67+
if blk.Height >= height {
68+
return nil
69+
}
70+
case <-ctx.Done():
71+
return ctx.Err()
72+
}
73+
}
74+
}
75+
76+
// WaitRuntimeRound waits until the controller observes at least specified runtime round.
77+
func (c *Controller) WaitRuntimeRound(ctx context.Context, runtimeID common.Namespace, round uint64) error {
78+
blkCh, sub, err := c.RuntimeClient.WatchBlocks(ctx, runtimeID)
79+
if err != nil {
80+
return fmt.Errorf("failed to watch runtime blocks: %w", err)
81+
}
82+
defer sub.Close()
83+
84+
for {
85+
select {
86+
case annBlk, ok := <-blkCh:
87+
if !ok {
88+
return fmt.Errorf("runtime block channel closed")
89+
}
90+
if annBlk.Block.Header.Round >= round {
91+
return nil
92+
}
93+
case <-ctx.Done():
94+
return ctx.Err()
95+
}
96+
}
97+
}
98+
4999
// NewController creates a new node controller given the path to
50100
// a node's internal socket.
51101
func NewController(socketPath string) (*Controller, error) {

0 commit comments

Comments
 (0)