@@ -5,20 +5,64 @@ package itest
55import (
66 "context"
77 "flag"
8+ "fmt"
89 "testing"
910
1011 "github.com/lightninglabs/taproot-assets/taprpc"
1112 "github.com/lightningnetwork/lnd/lntest"
1213 "github.com/stretchr/testify/require"
14+ "golang.org/x/exp/rand"
1315)
1416
15- var optionalTests = flag .Bool ("optional" , false , "if true, the optional test" +
16- "list will be used" )
17+ const (
18+ // defaultSplitTranches is the default number of tranches we split the
19+ // test cases into.
20+ defaultSplitTranches uint = 1
21+
22+ // defaultRunTranche is the default index of the test cases tranche that
23+ // we run.
24+ defaultRunTranche uint = 0
25+ )
26+
27+ var (
28+ optionalTests = flag .Bool (
29+ "optional" , false , "if true, the optional test list will be " +
30+ "used" ,
31+ )
32+
33+ // testCasesSplitParts is the number of tranches the test cases should
34+ // be split into. By default this is set to 1, so no splitting happens.
35+ // If this value is increased, then the -runtranche flag must be
36+ // specified as well to indicate which part should be run in the current
37+ // invocation.
38+ testCasesSplitTranches = flag .Uint (
39+ "splittranches" , defaultSplitTranches , "split the test cases " +
40+ "in this many tranches and run the tranche at " +
41+ "0-based index specified by the -runtranche flag" ,
42+ )
43+
44+ // shuffleSeedFlag is the source of randomness used to shuffle the test
45+ // cases. If not specified, the test cases won't be shuffled.
46+ shuffleSeedFlag = flag .Uint64 (
47+ "shuffleseed" , 0 , "if set, shuffles the test cases using this " +
48+ "as the source of randomness" ,
49+ )
50+
51+ // testCasesRunTranche is the 0-based index of the split test cases
52+ // tranche to run in the current invocation.
53+ testCasesRunTranche = flag .Uint (
54+ "runtranche" , defaultRunTranche , "run the tranche of the " +
55+ "split test cases with the given (0-based) index" ,
56+ )
57+ )
1758
1859// TestTaprootAssetsDaemon performs a series of integration tests amongst a
1960// programmatically driven set of participants, namely a Taproot Assets daemon
2061// and a universe server.
2162func TestTaprootAssetsDaemon (t * testing.T ) {
63+ // Get the test cases to be run in this tranche.
64+ testCases , trancheIndex , trancheOffset := getTestCaseSplitTranche ()
65+
2266 // Switch to the list of optional test cases with the '-optional' flag.
2367 testList := testCases
2468 if * optionalTests {
@@ -43,9 +87,16 @@ func TestTaprootAssetsDaemon(t *testing.T) {
4387 lndHarness .CleanShutDown ()
4488 })
4589
90+ // Get the current block height.
91+ height := lndHarness .CurrentHeight ()
92+
4693 t .Logf ("Running %v integration tests" , len (testList ))
47- for _ , testCase := range testList {
48- success := t .Run (testCase .name , func (t1 * testing.T ) {
94+ for idx , testCase := range testList {
95+ name := fmt .Sprintf ("tranche%02d/%02d-of-%d/%s" ,
96+ trancheIndex , trancheOffset + uint (idx )+ 1 ,
97+ len (allTestCases ), testCase .name )
98+
99+ success := t .Run (name , func (t1 * testing.T ) {
49100 // Create a new LND node for use with the universe
50101 // server.
51102 t .Log ("Starting universe server LND node" )
@@ -90,6 +141,11 @@ func TestTaprootAssetsDaemon(t *testing.T) {
90141 return
91142 }
92143 }
144+
145+ //nolint:forbidigo
146+ fmt .Printf ("=========> tranche %v finished, tested %d cases, mined " +
147+ "blocks: %d\n " , trancheIndex , len (testCases ),
148+ lndHarness .CurrentHeight ()- height )
93149}
94150
95151// testGetInfo tests the GetInfo RPC call.
@@ -115,3 +171,87 @@ func testGetInfo(t *harnessTest) {
115171 // Ensure the response matches the expected response.
116172 require .Equal (t .t , resp , respCli )
117173}
174+
175+ // maybeShuffleTestCases shuffles the test cases if the flag `shuffleseed` is
176+ // set and not 0. In parallel tests we want to shuffle the test cases so they
177+ // are executed in a random order. This is done to even out the blocks mined in
178+ // each test tranche so they can run faster.
179+ //
180+ // NOTE: Because the parallel tests are initialized with the same seed (job
181+ // ID), they will always have the same order.
182+ func maybeShuffleTestCases () {
183+ // Exit if not set.
184+ if shuffleSeedFlag == nil {
185+ return
186+ }
187+
188+ // Exit if set to 0.
189+ if * shuffleSeedFlag == 0 {
190+ return
191+ }
192+
193+ // Init the seed and shuffle the test cases.
194+ rand .Seed (* shuffleSeedFlag )
195+ rand .Shuffle (len (allTestCases ), func (i , j int ) {
196+ allTestCases [i ], allTestCases [j ] =
197+ allTestCases [j ], allTestCases [i ]
198+ })
199+ }
200+
201+ // createIndices divides the number of test cases into pairs of indices that
202+ // specify the start and end of a tranche.
203+ func createIndices (numCases , numTranches uint ) [][2 ]uint {
204+ // Calculate base value and remainder.
205+ base := numCases / numTranches
206+ remainder := numCases % numTranches
207+
208+ // Generate indices.
209+ indices := make ([][2 ]uint , numTranches )
210+ start := uint (0 )
211+
212+ for i := uint (0 ); i < numTranches ; i ++ {
213+ end := start + base
214+ if i < remainder {
215+ // Add one for the remainder.
216+ end ++
217+ }
218+ indices [i ] = [2 ]uint {start , end }
219+ start = end
220+ }
221+
222+ return indices
223+ }
224+
225+ // getTestCaseSplitTranche returns the sub slice of the test cases that should
226+ // be run as the current split tranche as well as the index and slice offset of
227+ // the tranche.
228+ func getTestCaseSplitTranche () ([]* testCase , uint , uint ) {
229+ numTranches := defaultSplitTranches
230+ if testCasesSplitTranches != nil {
231+ numTranches = * testCasesSplitTranches
232+ }
233+ runTranche := defaultRunTranche
234+ if testCasesRunTranche != nil {
235+ runTranche = * testCasesRunTranche
236+ }
237+
238+ // There's a special flake-hunt mode where we run the same test multiple
239+ // times in parallel. In that case the tranche index is equal to the
240+ // thread ID, but we need to actually run all tests for the regex
241+ // selection to work.
242+ threadID := runTranche
243+ if numTranches == 1 {
244+ runTranche = 0
245+ }
246+
247+ // Shuffle the test cases if the `shuffleseed` flag is set.
248+ maybeShuffleTestCases ()
249+
250+ numCases := uint (len (allTestCases ))
251+ indices := createIndices (numCases , numTranches )
252+ index := indices [runTranche ]
253+ trancheOffset , trancheEnd := index [0 ], index [1 ]
254+
255+ return allTestCases [trancheOffset :trancheEnd ], threadID ,
256+ trancheOffset
257+ }
0 commit comments