Skip to content

Commit

Permalink
fix test with sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Jun 20, 2023
1 parent f99b9e9 commit bec0544
Showing 1 changed file with 27 additions and 46 deletions.
73 changes: 27 additions & 46 deletions rabbitmq/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ func TestFinalizedInitializedPayments(t *testing.T) {
Times(1).
Return(ch, nil)

firstHash := "69e5f0f0590be75e30f671d56afe1d55"
secondHash := "ffff0f0590be75e30f671d56afe1d55"
firstHash := "69e5f0f0590be75e30f671d56afe1d55"
secondHash := "ffff0f0590be75e30f671d56afe1d55"

invoices := []models.Invoice{
{
ID: 0,
RHash: firstHash,
ID: 0,
RHash: firstHash,
},
{
ID: 1,
RHash: secondHash,
},
{
ID: 1,
RHash: secondHash,
},
}

lndHubService.EXPECT().
Expand Down Expand Up @@ -75,48 +75,29 @@ func TestFinalizedInitializedPayments(t *testing.T) {
Return(models.TransactionEntry{InvoiceID: invoices[1].ID}, nil)

ctx := context.Background()
successPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: firstHash, Status: lnrpc.Payment_SUCCEEDED})
if err != nil {
t.Error(err)
}

failedPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: secondHash, Status: lnrpc.Payment_FAILED})
if err != nil {
t.Error(err)
}

ch <- amqp.Delivery{Body: successPayment}
ch <- amqp.Delivery{Body: failedPayment}

wg := sync.WaitGroup{}

wg.Add(1)
go func() {
err = client.FinalizeInitializedPayments(ctx, lndHubService)
successPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: firstHash, Status: lnrpc.Payment_SUCCEEDED})
if err != nil {
t.Error(err)
}

assert.NoError(t, err)
wg.Done()
}()
failedPayment, err := json.Marshal(&lnrpc.Payment{PaymentHash: secondHash, Status: lnrpc.Payment_FAILED})
if err != nil {
t.Error(err)
}

waitTimeout(&wg, time.Second * 3, t)
}
ch <- amqp.Delivery{Body: successPayment}
ch <- amqp.Delivery{Body: failedPayment}

// 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()
}()
wg := sync.WaitGroup{}

select {
case <-c:
return false // completed normally
wg.Add(1)
go func() {
err = client.FinalizeInitializedPayments(ctx, lndHubService)

case <-time.After(timeout):
t.Errorf("Waiting on waitgroup timed out during test")
assert.NoError(t, err)
wg.Done()
}()

return true // timed out
}
//wait a bit for payments to be processed
time.Sleep(time.Second)
}

0 comments on commit bec0544

Please sign in to comment.