/
late_start.go
125 lines (108 loc) · 2.95 KB
/
late_start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package runtime
import (
"context"
"errors"
"fmt"
"time"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario"
"github.com/oasisprotocol/oasis-core/go/runtime/client/api"
)
// LateStart is the LateStart node basic scenario.
var LateStart scenario.Scenario = newLateStartImpl("late-start")
const lateStartInitialWait = 2 * time.Minute
type lateStartImpl struct {
runtimeImpl
}
func newLateStartImpl(name string) scenario.Scenario {
return &lateStartImpl{
runtimeImpl: *newRuntimeImpl(name, BasicKVTestClient),
}
}
func (sc *lateStartImpl) Clone() scenario.Scenario {
return &lateStartImpl{
runtimeImpl: *sc.runtimeImpl.Clone().(*runtimeImpl),
}
}
func (sc *lateStartImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.runtimeImpl.Fixture()
if err != nil {
return nil, err
}
// Start without a client.
f.Clients = []oasis.ClientFixture{}
return f, nil
}
func (sc *lateStartImpl) Run(childEnv *env.Env) error {
ctx := context.Background()
// Start the network.
var err error
if err = sc.Net.Start(); err != nil {
return err
}
sc.Logger.Info("Waiting before starting the client node",
"wait_for", lateStartInitialWait,
)
time.Sleep(lateStartInitialWait)
sc.Logger.Info("Starting the client node")
clientFixture := &oasis.ClientFixture{
Runtimes: []int{1},
}
client, err := clientFixture.Create(sc.Net)
if err != nil {
return err
}
if err = client.Start(); err != nil {
return err
}
ctrl, err := oasis.NewController(client.SocketPath())
if err != nil {
return fmt.Errorf("failed to create controller for client: %w", err)
}
err = ctrl.RuntimeClient.SubmitTxNoWait(ctx, &api.SubmitTxRequest{
RuntimeID: runtimeID,
Data: cbor.Marshal(&TxnCall{
Method: "insert",
Args: struct {
Key string `json:"key"`
Value string `json:"value"`
}{
Key: "hello",
Value: "test",
},
}),
})
if !errors.Is(err, api.ErrNotSynced) {
return fmt.Errorf("expected error: %v, got: %v", api.ErrNotSynced, err)
}
_, err = ctrl.RuntimeClient.SubmitTx(ctx, &api.SubmitTxRequest{
RuntimeID: runtimeID,
Data: cbor.Marshal(&TxnCall{
Method: "insert",
Args: struct {
Key string `json:"key"`
Value string `json:"value"`
}{
Key: "hello",
Value: "test",
},
}),
})
if !errors.Is(err, api.ErrNotSynced) {
return fmt.Errorf("expected error: %v, got: %v", api.ErrNotSynced, err)
}
// Set the ClientController to the late-started one, so that the test
// client works.
sc.Net.SetClientController(ctrl)
sc.Logger.Info("Starting the basic test client")
// Explicitly wait for the client to sync, before starting the client.
if err = sc.waitForClientSync(ctx); err != nil {
return err
}
if err = sc.startTestClientOnly(ctx, childEnv); err != nil {
return err
}
return sc.waitTestClient()
}