@@ -20,6 +20,7 @@ import (
20
20
"github.com/ava-labs/avalanchego/ids"
21
21
"github.com/ava-labs/avalanchego/tests"
22
22
"github.com/ava-labs/avalanchego/tests/antithesis"
23
+ "github.com/ava-labs/avalanchego/tests/e2e/banff"
23
24
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
24
25
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
25
26
"github.com/ava-labs/avalanchego/utils/constants"
@@ -44,10 +45,13 @@ import (
44
45
45
46
const NumKeys = 5
46
47
48
+ // TODO(marun) Switch to using zap for logging
49
+ // TODO(marun) Extract the common elements of test execution for reuse across test setups
50
+
47
51
func main () {
48
52
// TODO(marun) Support choosing the log format
49
- tc := tests . NewTestContext (tests .NewDefaultLogger ("" ))
50
- defer tc .Cleanup ()
53
+ tc := antithesis . NewInstrumentedTestContext (tests .NewDefaultLogger ("" ))
54
+ defer tc .RecoverAndExit ()
51
55
require := require .New (tc )
52
56
53
57
c := antithesis .NewConfig (
@@ -57,17 +61,12 @@ func main() {
57
61
},
58
62
)
59
63
ctx := tests .DefaultNotifyContext (c .Duration , tc .DeferCleanup )
64
+ // Ensure contexts sourced from the test context use the notify context as their parent
65
+ tc .SetDefaultContextParent (ctx )
60
66
61
67
kc := secp256k1fx .NewKeychain (genesis .EWOQKey )
62
68
walletSyncStartTime := time .Now ()
63
- wallet , err := primary .MakeWallet (
64
- ctx ,
65
- c .URIs [0 ],
66
- kc ,
67
- kc ,
68
- primary.WalletConfig {},
69
- )
70
- require .NoError (err , "failed to initialize wallet" )
69
+ wallet := e2e .NewWallet (tc , kc , tmpnet.NodeURI {URI : c .URIs [0 ]})
71
70
tc .Log ().Info ("synced wallet" ,
72
71
zap .Duration ("duration" , time .Since (walletSyncStartTime )),
73
72
)
@@ -122,14 +121,7 @@ func main() {
122
121
uri := c .URIs [i % len (c .URIs )]
123
122
kc := secp256k1fx .NewKeychain (key )
124
123
walletSyncStartTime := time .Now ()
125
- wallet , err := primary .MakeWallet (
126
- ctx ,
127
- uri ,
128
- kc ,
129
- kc ,
130
- primary.WalletConfig {},
131
- )
132
- require .NoError (err , "failed to initialize wallet" )
124
+ wallet := e2e .NewWallet (tc , kc , tmpnet.NodeURI {URI : uri })
133
125
tc .Log ().Info ("synced wallet" ,
134
126
zap .Duration ("duration" , time .Since (walletSyncStartTime )),
135
127
)
@@ -162,12 +154,25 @@ type workload struct {
162
154
uris []string
163
155
}
164
156
157
+ // newTestContext returns a test context that ensures that log output and assertions are
158
+ // associated with this worker.
159
+ func (w * workload ) newTestContext (ctx context.Context ) * tests.SimpleTestContext {
160
+ return antithesis .NewInstrumentedTestContextWithArgs (
161
+ ctx ,
162
+ w .log ,
163
+ map [string ]any {
164
+ "worker" : w .id ,
165
+ },
166
+ )
167
+ }
168
+
165
169
func (w * workload ) run (ctx context.Context ) {
166
170
timer := timerpkg .StoppedTimer ()
167
171
168
- tc := tests .NewTestContext (w .log )
169
- defer tc .Cleanup ()
170
- require := require .New (tc )
172
+ tc := w .newTestContext (ctx )
173
+ // Any assertion failure from this test context will result in process exit due to the
174
+ // panic being rethrown. This ensures that failures in test setup are fatal.
175
+ defer tc .Recover (true /* rethrow */ )
171
176
172
177
xAVAX , pAVAX := e2e .GetWalletBalances (tc , w .wallet )
173
178
assert .Reachable ("wallet starting" , map [string ]any {
@@ -176,32 +181,30 @@ func (w *workload) run(ctx context.Context) {
176
181
"pBalance" : pAVAX ,
177
182
})
178
183
184
+ defaultExecutionDelay := big .NewInt (int64 (time .Second ))
179
185
for {
180
- val , err := rand .Int (rand .Reader , big .NewInt (5 ))
181
- require .NoError (err , "failed to read randomness" )
182
-
183
- flowID := val .Int64 ()
184
- w .log .Info ("executing test" ,
185
- zap .Int ("workerID" , w .id ),
186
- zap .Int64 ("flowID" , flowID ),
187
- )
188
- switch flowID {
189
- case 0 :
190
- w .issueXChainBaseTx (ctx )
191
- case 1 :
192
- w .issueXChainCreateAssetTx (ctx )
193
- case 2 :
194
- w .issueXChainOperationTx (ctx )
195
- case 3 :
196
- w .issueXToPTransfer (ctx )
197
- case 4 :
198
- w .issuePToXTransfer (ctx )
199
- }
186
+ w .executeTest (ctx )
200
187
201
- val , err = rand .Int (rand .Reader , big .NewInt (int64 (time .Second )))
202
- require .NoError (err , "failed to read randomness" )
188
+ // Delay execution of the next test by a random duration
189
+ rawExecutionDelay , err := rand .Int (rand .Reader , defaultExecutionDelay )
190
+ // Avoid using require.NoError since the execution delay is not critical and an
191
+ // assertion failure in this function is fatal.
192
+ if err != nil {
193
+ w .log .Error ("failed to read randomness" ,
194
+ zap .Error (err ),
195
+ )
196
+ assert .Unreachable ("failed to read randomness" , map [string ]any {
197
+ "worker" : w .id ,
198
+ "err" : err ,
199
+ })
200
+ rawExecutionDelay = defaultExecutionDelay
201
+ }
202
+ executionDelay := time .Duration (rawExecutionDelay .Int64 ())
203
+ w .log .Info ("waiting" ,
204
+ zap .Duration ("duration" , executionDelay ),
205
+ )
206
+ timer .Reset (executionDelay )
203
207
204
- timer .Reset (time .Duration (val .Int64 ()))
205
208
select {
206
209
case <- ctx .Done ():
207
210
return
@@ -210,6 +213,44 @@ func (w *workload) run(ctx context.Context) {
210
213
}
211
214
}
212
215
216
+ // executeTest executes a test at random.
217
+ func (w * workload ) executeTest (ctx context.Context ) {
218
+ tc := w .newTestContext (ctx )
219
+ // Panics will be recovered without being rethrown, ensuring that test failures are not fatal.
220
+ defer tc .Recover (false /* rethrow */ )
221
+ require := require .New (tc )
222
+
223
+ val , err := rand .Int (rand .Reader , big .NewInt (6 ))
224
+ require .NoError (err , "failed to read randomness" )
225
+
226
+ // TODO(marun)
227
+ flowID := val .Int64 ()
228
+ switch flowID {
229
+ case 0 :
230
+ // TODO(marun) Create abstraction for a test that supports a name e.g. `aTest{name: "foo", mytestfunc}`
231
+ w .log .Info ("executing issueXChainBaseTx" )
232
+ w .issueXChainBaseTx (ctx )
233
+ case 1 :
234
+ w .log .Info ("executing issueXChainCreateAssetTx" )
235
+ w .issueXChainCreateAssetTx (ctx )
236
+ case 2 :
237
+ w .log .Info ("executing issueXChainOperationTx" )
238
+ w .issueXChainOperationTx (ctx )
239
+ case 3 :
240
+ w .log .Info ("executing issueXToPTransfer" )
241
+ w .issueXToPTransfer (ctx )
242
+ case 4 :
243
+ w .log .Info ("executing issuePToXTransfer" )
244
+ w .issuePToXTransfer (ctx )
245
+ case 5 :
246
+ w .log .Info ("executing banff.TestCustomAssetTransfer" )
247
+ addr , _ := w .addrs .Peek ()
248
+ banff .TestCustomAssetTransfer (tc , w .wallet , addr )
249
+ case 6 :
250
+ w .log .Info ("sleeping" )
251
+ }
252
+ }
253
+
213
254
func (w * workload ) issueXChainBaseTx (ctx context.Context ) {
214
255
var (
215
256
xWallet = w .wallet .X ()
0 commit comments