invoices: elminitate sleep from expiry watcher tests
This commit removes sleeps from invoice expiry watcher tests in favor of a timeout guard.
This commit is contained in:
parent
3ad7ab223e
commit
70fc9c1901
@ -1,6 +1,7 @@
|
|||||||
package invoices
|
package invoices
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
// for InvoiceExpiryWatcher tests.
|
// for InvoiceExpiryWatcher tests.
|
||||||
type invoiceExpiryWatcherTest struct {
|
type invoiceExpiryWatcherTest struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
|
wg sync.WaitGroup
|
||||||
watcher *InvoiceExpiryWatcher
|
watcher *InvoiceExpiryWatcher
|
||||||
testData invoiceExpiryTestData
|
testData invoiceExpiryTestData
|
||||||
canceledInvoices []lntypes.Hash
|
canceledInvoices []lntypes.Hash
|
||||||
@ -30,8 +32,11 @@ func newInvoiceExpiryWatcherTest(t *testing.T, now time.Time,
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test.wg.Add(numExpiredInvoices)
|
||||||
|
|
||||||
err := test.watcher.Start(func(paymentHash lntypes.Hash) error {
|
err := test.watcher.Start(func(paymentHash lntypes.Hash) error {
|
||||||
test.canceledInvoices = append(test.canceledInvoices, paymentHash)
|
test.canceledInvoices = append(test.canceledInvoices, paymentHash)
|
||||||
|
test.wg.Done()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -42,6 +47,22 @@ func newInvoiceExpiryWatcherTest(t *testing.T, now time.Time,
|
|||||||
return test
|
return test
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *invoiceExpiryWatcherTest) waitForFinish(timeout time.Duration) {
|
||||||
|
done := make(chan struct{})
|
||||||
|
|
||||||
|
// Wait for all cancels.
|
||||||
|
go func() {
|
||||||
|
t.wg.Wait()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(timeout):
|
||||||
|
t.t.Fatalf("test timeout")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *invoiceExpiryWatcherTest) checkExpectations() {
|
func (t *invoiceExpiryWatcherTest) checkExpectations() {
|
||||||
// Check that invoices that got canceled during the test are the ones
|
// Check that invoices that got canceled during the test are the ones
|
||||||
// that expired.
|
// that expired.
|
||||||
@ -83,9 +104,10 @@ func TestInvoiceExpiryWatcherStartStop(t *testing.T) {
|
|||||||
// Tests that no invoices will expire from an empty InvoiceExpiryWatcher.
|
// Tests that no invoices will expire from an empty InvoiceExpiryWatcher.
|
||||||
func TestInvoiceExpiryWithNoInvoices(t *testing.T) {
|
func TestInvoiceExpiryWithNoInvoices(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
test := newInvoiceExpiryWatcherTest(t, testTime, 0, 0)
|
test := newInvoiceExpiryWatcherTest(t, testTime, 0, 0)
|
||||||
|
|
||||||
time.Sleep(testTimeout)
|
test.waitForFinish(testTimeout)
|
||||||
test.watcher.Stop()
|
test.watcher.Stop()
|
||||||
test.checkExpectations()
|
test.checkExpectations()
|
||||||
}
|
}
|
||||||
@ -101,7 +123,7 @@ func TestInvoiceExpiryWithOnlyExpiredInvoices(t *testing.T) {
|
|||||||
test.watcher.AddInvoice(paymentHash, invoice)
|
test.watcher.AddInvoice(paymentHash, invoice)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(testTimeout)
|
test.waitForFinish(testTimeout)
|
||||||
test.watcher.Stop()
|
test.watcher.Stop()
|
||||||
test.checkExpectations()
|
test.checkExpectations()
|
||||||
}
|
}
|
||||||
@ -110,6 +132,7 @@ func TestInvoiceExpiryWithOnlyExpiredInvoices(t *testing.T) {
|
|||||||
// will be canceled.
|
// will be canceled.
|
||||||
func TestInvoiceExpiryWithPendingAndExpiredInvoices(t *testing.T) {
|
func TestInvoiceExpiryWithPendingAndExpiredInvoices(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
||||||
|
|
||||||
for paymentHash, invoice := range test.testData.expiredInvoices {
|
for paymentHash, invoice := range test.testData.expiredInvoices {
|
||||||
@ -120,7 +143,7 @@ func TestInvoiceExpiryWithPendingAndExpiredInvoices(t *testing.T) {
|
|||||||
test.watcher.AddInvoice(paymentHash, invoice)
|
test.watcher.AddInvoice(paymentHash, invoice)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(testTimeout)
|
test.waitForFinish(testTimeout)
|
||||||
test.watcher.Stop()
|
test.watcher.Stop()
|
||||||
test.checkExpectations()
|
test.checkExpectations()
|
||||||
}
|
}
|
||||||
@ -128,8 +151,10 @@ func TestInvoiceExpiryWithPendingAndExpiredInvoices(t *testing.T) {
|
|||||||
// Tests adding multiple invoices at once.
|
// Tests adding multiple invoices at once.
|
||||||
func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
|
func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
||||||
var invoices []channeldb.InvoiceWithPaymentHash
|
var invoices []channeldb.InvoiceWithPaymentHash
|
||||||
|
|
||||||
for hash, invoice := range test.testData.expiredInvoices {
|
for hash, invoice := range test.testData.expiredInvoices {
|
||||||
invoices = append(invoices,
|
invoices = append(invoices,
|
||||||
channeldb.InvoiceWithPaymentHash{
|
channeldb.InvoiceWithPaymentHash{
|
||||||
@ -138,6 +163,7 @@ func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for hash, invoice := range test.testData.pendingInvoices {
|
for hash, invoice := range test.testData.pendingInvoices {
|
||||||
invoices = append(invoices,
|
invoices = append(invoices,
|
||||||
channeldb.InvoiceWithPaymentHash{
|
channeldb.InvoiceWithPaymentHash{
|
||||||
@ -148,7 +174,7 @@ func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test.watcher.AddInvoices(invoices)
|
test.watcher.AddInvoices(invoices)
|
||||||
time.Sleep(testTimeout)
|
test.waitForFinish(testTimeout)
|
||||||
test.watcher.Stop()
|
test.watcher.Stop()
|
||||||
test.checkExpectations()
|
test.checkExpectations()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user