@@ -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,15 +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 (ctx , & primary.WalletConfig {
64
- URI : c .URIs [0 ],
65
- AVAXKeychain : kc ,
66
- EthKeychain : kc ,
67
- })
68
- require .NoError (err , "failed to initialize wallet" )
69
+ wallet := e2e .NewWallet (tc , kc , tmpnet.NodeURI {URI : c .URIs [0 ]})
69
70
tc .Log ().Info ("synced wallet" ,
70
71
zap .Duration ("duration" , time .Since (walletSyncStartTime )),
71
72
)
@@ -120,12 +121,7 @@ func main() {
120
121
uri := c .URIs [i % len (c .URIs )]
121
122
kc := secp256k1fx .NewKeychain (key )
122
123
walletSyncStartTime := time .Now ()
123
- wallet , err := primary .MakeWallet (ctx , & primary.WalletConfig {
124
- URI : uri ,
125
- AVAXKeychain : kc ,
126
- EthKeychain : kc ,
127
- })
128
- require .NoError (err , "failed to initialize wallet" )
124
+ wallet := e2e .NewWallet (tc , kc , tmpnet.NodeURI {URI : uri })
129
125
tc .Log ().Info ("synced wallet" ,
130
126
zap .Duration ("duration" , time .Since (walletSyncStartTime )),
131
127
)
@@ -158,12 +154,25 @@ type workload struct {
158
154
uris []string
159
155
}
160
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
+
161
169
func (w * workload ) run (ctx context.Context ) {
162
170
timer := timerpkg .StoppedTimer ()
163
171
164
- tc := tests .NewTestContext (w .log )
165
- defer tc .Cleanup ()
166
- 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 */ )
167
176
168
177
xAVAX , pAVAX := e2e .GetWalletBalances (tc , w .wallet )
169
178
assert .Reachable ("wallet starting" , map [string ]any {
@@ -172,32 +181,30 @@ func (w *workload) run(ctx context.Context) {
172
181
"pBalance" : pAVAX ,
173
182
})
174
183
184
+ defaultExecutionDelay := big .NewInt (int64 (time .Second ))
175
185
for {
176
- val , err := rand .Int (rand .Reader , big .NewInt (5 ))
177
- require .NoError (err , "failed to read randomness" )
186
+ w .executeTest (ctx )
178
187
179
- flowID := val .Int64 ()
180
- w .log .Info ("executing test" ,
181
- zap .Int ("workerID" , w .id ),
182
- zap .Int64 ("flowID" , flowID ),
183
- )
184
- switch flowID {
185
- case 0 :
186
- w .issueXChainBaseTx (ctx )
187
- case 1 :
188
- w .issueXChainCreateAssetTx (ctx )
189
- case 2 :
190
- w .issueXChainOperationTx (ctx )
191
- case 3 :
192
- w .issueXToPTransfer (ctx )
193
- case 4 :
194
- w .issuePToXTransfer (ctx )
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
195
201
}
202
+ executionDelay := time .Duration (rawExecutionDelay .Int64 ())
203
+ w .log .Info ("waiting" ,
204
+ zap .Duration ("duration" , executionDelay ),
205
+ )
206
+ timer .Reset (executionDelay )
196
207
197
- val , err = rand .Int (rand .Reader , big .NewInt (int64 (time .Second )))
198
- require .NoError (err , "failed to read randomness" )
199
-
200
- timer .Reset (time .Duration (val .Int64 ()))
201
208
select {
202
209
case <- ctx .Done ():
203
210
return
@@ -206,6 +213,44 @@ func (w *workload) run(ctx context.Context) {
206
213
}
207
214
}
208
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
+
209
254
func (w * workload ) issueXChainBaseTx (ctx context.Context ) {
210
255
var (
211
256
xWallet = w .wallet .X ()
0 commit comments