multi: terminate SubscribeSingleInvoice once completed

This commit is contained in:
carla 2021-04-14 09:19:23 +02:00
parent 0686329062
commit db1d671b1a
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
3 changed files with 19 additions and 0 deletions

@ -362,6 +362,11 @@ func (c ContractState) String() string {
return "Unknown" return "Unknown"
} }
// IsFinal returns a boolean indicating whether an invoice state is final
func (c ContractState) IsFinal() bool {
return c == ContractSettled || c == ContractCanceled
}
// ContractTerm is a companion struct to the Invoice struct. This struct houses // ContractTerm is a companion struct to the Invoice struct. This struct houses
// the necessary conditions required before the invoice can be considered fully // the necessary conditions required before the invoice can be considered fully
// settled by the payee. // settled by the payee.

@ -247,6 +247,12 @@ func (s *Server) SubscribeSingleInvoice(req *SubscribeSingleInvoiceRequest,
return err return err
} }
// If we have reached a terminal state, close the
// stream with no error.
if newInvoice.State.IsFinal() {
return nil
}
case <-s.quit: case <-s.quit:
return nil return nil
} }

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"io"
"sync" "sync"
"time" "time"
@ -403,4 +404,11 @@ func testHoldInvoicePersistence(net *lntest.NetworkHarness, t *harnessTest) {
} }
} }
} }
// Check that all of our invoice streams are terminated by the server
// since the invoices have completed.
for _, stream := range invoiceStreams {
_, err = stream.Recv()
require.Equal(t.t, io.EOF, err)
}
} }