fix test with sleep

This commit is contained in:
kiwiidb
2023-06-20 13:45:15 +02:00
parent f99b9e9824
commit bec0544a55

View File

@@ -35,18 +35,18 @@ func TestFinalizedInitializedPayments(t *testing.T) {
Times(1). Times(1).
Return(ch, nil) Return(ch, nil)
firstHash := "69e5f0f0590be75e30f671d56afe1d55" firstHash := "69e5f0f0590be75e30f671d56afe1d55"
secondHash := "ffff0f0590be75e30f671d56afe1d55" secondHash := "ffff0f0590be75e30f671d56afe1d55"
invoices := []models.Invoice{ invoices := []models.Invoice{
{ {
ID: 0, ID: 0,
RHash: firstHash, RHash: firstHash,
},
{
ID: 1,
RHash: secondHash,
}, },
{
ID: 1,
RHash: secondHash,
},
} }
lndHubService.EXPECT(). lndHubService.EXPECT().
@@ -75,48 +75,29 @@ func TestFinalizedInitializedPayments(t *testing.T) {
Return(models.TransactionEntry{InvoiceID: invoices[1].ID}, nil) Return(models.TransactionEntry{InvoiceID: invoices[1].ID}, nil)
ctx := context.Background() ctx := context.Background()
successPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: firstHash, Status: lnrpc.Payment_SUCCEEDED}) successPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: firstHash, Status: lnrpc.Payment_SUCCEEDED})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
failedPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: secondHash, Status: lnrpc.Payment_FAILED}) failedPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: secondHash, Status: lnrpc.Payment_FAILED})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
ch <- amqp.Delivery{Body: successPayment} ch <- amqp.Delivery{Body: successPayment}
ch <- amqp.Delivery{Body: failedPayment} ch <- amqp.Delivery{Body: failedPayment}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
go func() { go func() {
err = client.FinalizeInitializedPayments(ctx, lndHubService) err = client.FinalizeInitializedPayments(ctx, lndHubService)
assert.NoError(t, err) assert.NoError(t, err)
wg.Done() wg.Done()
}() }()
waitTimeout(&wg, time.Second * 3, t) //wait a bit for payments to be processed
} time.Sleep(time.Second)
// waitTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration, t *testing.T) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
t.Errorf("Waiting on waitgroup timed out during test")
return true // timed out
}
} }