mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
multi: add interface for subscribe client so it can be mocked
The current implementation of subscribe is difficult to mock because the queue that we send updates on in unexported, so you cannot create a subscribe.Client object and then add your own updates. While it is possible to run a subscribe server in tests, subscribe servers will shutdown before dispatching their udpates to all clients, which can be flakey (and is difficult to workaround). In this commit, we add a subscription interface so that these testing struggles can be addressed with a mock.
This commit is contained in:
@@ -14,9 +14,9 @@ var ErrServerShuttingDown = errors.New("subscription server shutting down")
|
||||
|
||||
// Client is used to get notified about updates the caller has subscribed to,
|
||||
type Client struct {
|
||||
// Cancel should be called in case the client no longer wants to
|
||||
// cancel should be called in case the client no longer wants to
|
||||
// subscribe for updates from the server.
|
||||
Cancel func()
|
||||
cancel func()
|
||||
|
||||
updates *queue.ConcurrentQueue
|
||||
quit chan struct{}
|
||||
@@ -34,6 +34,12 @@ func (c *Client) Quit() <-chan struct{} {
|
||||
return c.quit
|
||||
}
|
||||
|
||||
// Cancel should be called in case the client no longer wants to
|
||||
// subscribe for updates from the server.
|
||||
func (c *Client) Cancel() {
|
||||
c.cancel()
|
||||
}
|
||||
|
||||
// Server is a struct that manages a set of subscriptions and their
|
||||
// corresponding clients. Any update will be delivered to all active clients.
|
||||
type Server struct {
|
||||
@@ -118,7 +124,7 @@ func (s *Server) Subscribe() (*Client, error) {
|
||||
client := &Client{
|
||||
updates: queue.NewConcurrentQueue(20),
|
||||
quit: make(chan struct{}),
|
||||
Cancel: func() {
|
||||
cancel: func() {
|
||||
select {
|
||||
case s.clientUpdates <- &clientUpdate{
|
||||
cancel: true,
|
||||
|
Reference in New Issue
Block a user