background routine: add context cancel support

This commit is contained in:
kiwiidb
2022-04-22 14:32:48 +02:00
parent 8dd8c14cd7
commit 624909484e
2 changed files with 31 additions and 24 deletions

View File

@@ -271,6 +271,7 @@ func (suite *WebSocketTestSuite) TestWebSocketMissingInvoice() {
func (suite *WebSocketTestSuite) TearDownSuite() {
suite.invoiceUpdateSubCancelFn()
suite.websocketServer.Close()
clearTable(suite.service, "invoices")
}
func TestWebSocketSuite(t *testing.T) {

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"strings"
"time"
@@ -122,6 +123,10 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
return err
}
for {
select {
case <-ctx.Done():
return fmt.Errorf("Context was canceled")
default:
// receive the next invoice update
rawInvoice, err := invoiceSubscriptionStream.Recv()
if err != nil {
@@ -150,4 +155,5 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
sentry.CaptureException(processingError)
}
}
}
}