Skip to content

Commit

Permalink
chain: test sequential start and stop of client
Browse files Browse the repository at this point in the history
adds a unit test that verifies that the neutrino client can
start and stop sequentially without errors or races in a
finite amount of time.  adjusts the Start method of the client
to remove a data race on the quit channel.
  • Loading branch information
MStreet3 committed Oct 16, 2022
1 parent c9d2561 commit 05658d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
15 changes: 7 additions & 8 deletions chain/neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,25 @@ func (s *NeutrinoClient) BackEnd() string {

// Start replicates the RPC client's Start method.
func (s *NeutrinoClient) Start() error {
s.clientMtx.Lock()
defer s.clientMtx.Unlock()

if err := s.CS.Start(); err != nil {
return fmt.Errorf("error starting chain service: %v", err)
}

s.clientMtx.Lock()
defer s.clientMtx.Unlock()
if !s.started {
s.enqueueNotification = make(chan interface{})
s.dequeueNotification = make(chan interface{})
s.currentBlock = make(chan *waddrmgr.BlockStamp)
s.quit = make(chan struct{})
s.started = true
s.wg.Add(1)
go func() {
select {
case s.enqueueNotification <- ClientConnected{}:
case <-s.quit:
}
}()
go s.notificationHandler()
select {
case s.enqueueNotification <- ClientConnected{}:
case <-s.quit:
}
}
return nil
}
Expand Down
36 changes: 31 additions & 5 deletions chain/neutrino_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
package chain

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)

// TestNeutrinoClientMultipleRestart ensures that multiple goroutines
// can Start and Stop the client without errors or races.
func TestNeutrinoClientMultipleRestart(t *testing.T) {
// call notifyreceived and rescan in a loop
// TestNeutrinoClientStartStop ensures that the client
// can sequentially Start and Stop without errors or races.
func TestNeutrinoClientStartStop(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
t.Cleanup(cancel)
nc := newMockNeutrinoClient(t)
_ = nc
numRestarts := 5

startStop := func() <-chan struct{} {
done := make(chan struct{})
go func() {
defer close(done)
err := nc.Start()
require.NoError(t, err)
nc.Stop()
nc.WaitForShutdown()
}()
return done
}

for i := 0; i < numRestarts; i++ {
done := startStop()
select {
case <-ctx.Done():
t.Fatal("timed out")
case <-done:
}
}
}

0 comments on commit 05658d0

Please sign in to comment.