From 3d61d157c6ecac26e991def6b282c60e5e943bb5 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 10 Feb 2023 16:32:09 +0800 Subject: [PATCH 1/2] channeldb: add unique output index to unit test --- channeldb/channel_test.go | 13 ++++++++++++- channeldb/db_test.go | 6 ++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index 9840ba00b..ccb6b10a8 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -6,6 +6,7 @@ import ( "net" "reflect" "runtime" + "sync/atomic" "testing" "github.com/btcsuite/btcd/btcec/v2" @@ -64,6 +65,11 @@ var ( // dummyRemoteOutIndex specifics a default value for their output index // in this test. dummyRemoteOutIndex = uint32(1) + + // uniqueOutputIndex is used to create a unique funding outpoint. + // + // NOTE: must be incremented when used. + uniqueOutputIndex = atomic.Uint32{} ) // testChannelParams is a struct which details the specifics of how a channel @@ -299,10 +305,15 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel { chanID := lnwire.NewShortChanIDFromInt(uint64(rand.Int63())) + // Increment the uniqueOutputIndex so we always get a unique value for + // the funding outpoint. + uniqueOutputIndex.Add(1) + op := wire.OutPoint{Hash: key, Index: uniqueOutputIndex.Load()} + return &OpenChannel{ ChanType: SingleFunderBit | FrozenBit, ChainHash: key, - FundingOutpoint: wire.OutPoint{Hash: key, Index: rand.Uint32()}, + FundingOutpoint: op, ShortChannelID: chanID, IsInitiator: true, IsPending: true, diff --git a/channeldb/db_test.go b/channeldb/db_test.go index e2cad1fd1..bed9c6e32 100644 --- a/channeldb/db_test.go +++ b/channeldb/db_test.go @@ -243,11 +243,13 @@ func TestFetchChannel(t *testing.T) { spew.Sdump(channelState), spew.Sdump(dbChannel)) } - // If we attempt to query for a non-exist ante channel, then we should + // If we attempt to query for a non-existent channel, then we should // get an error. channelState2 := createTestChannelState(t, cdb) require.NoError(t, err, "unable to create channel state") - channelState2.FundingOutpoint.Index ^= 1 + + uniqueOutputIndex.Add(1) + channelState2.FundingOutpoint.Index = uniqueOutputIndex.Load() _, err = cdb.FetchChannel(nil, channelState2.FundingOutpoint) if err == nil { From 4a7d3bc9c20f4683997856df96040d2ea62ccc2d Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 10 Feb 2023 17:35:57 +0800 Subject: [PATCH 2/2] invoices: fix flake in `TestInvoiceExpiryWithRegistry` --- invoices/invoiceregistry_test.go | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index 45bb5712c..866ad540f 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -1006,27 +1006,34 @@ func TestInvoiceExpiryWithRegistry(t *testing.T) { // Fwd time 1 day. testClock.SetTime(testTime.Add(24 * time.Hour)) - // Give some time to the watcher to cancel everything. - time.Sleep(500 * time.Millisecond) - if err := registry.Stop(); err != nil { - t.Fatalf("failed to stop invoice registry: %v", err) - } - // Create the expected cancellation set before the final check. expectedCancellations = append( expectedCancellations, invoicesThatWillCancel..., ) - // Retrospectively check that all invoices that were expected to be - // canceled are indeed canceled. - for i := range expectedCancellations { - invoice, err := registry.LookupInvoice(expectedCancellations[i]) - if err != nil { - t.Fatalf("cannot find invoice: %v", err) + // canceled returns a bool to indicate whether all the invoices are + // canceled. + canceled := func() bool { + for i := range expectedCancellations { + invoice, err := registry.LookupInvoice( + expectedCancellations[i], + ) + require.NoError(t, err) + + if invoice.State != invpkg.ContractCanceled { + return false + } } - require.Equal(t, invpkg.ContractCanceled, invoice.State) + return true } + + // Retrospectively check that all invoices that were expected to be + // canceled are indeed canceled. + require.Eventually(t, canceled, testTimeout, 10*time.Millisecond) + + // Finally stop the registry. + require.NoError(t, registry.Stop(), "failed to stop invoice registry") } // TestOldInvoiceRemovalOnStart tests that we'll attempt to remove old canceled