From 3394bf3f79f0ee32de625ea034b513aa7bcde2a6 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Fri, 26 Apr 2024 20:36:24 -0400 Subject: [PATCH] More robust testing --- cluster/bootstrap.go | 24 ++++++++++++++++-------- cluster/bootstrap_test.go | 3 +++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cluster/bootstrap.go b/cluster/bootstrap.go index 2a476e1da..c932d9434 100644 --- a/cluster/bootstrap.go +++ b/cluster/bootstrap.go @@ -84,9 +84,10 @@ func (s Suffrage) IsNonVoter() bool { } const ( - requestTimeout = 5 * time.Second - numJoinAttempts = 1 - bootInterval = 2 * time.Second + requestTimeout = 5 * time.Second + numJoinAttempts = 1 + bootInterval = 2 * time.Second + bootCheckInterval = 10 * time.Millisecond ) // String returns a string representation of the BootStatus. @@ -123,15 +124,21 @@ type Bootstrapper struct { bootStatusMu sync.RWMutex bootStatus BootStatus + + checkInterval time.Duration + + // White-box testing only + nBootCanceled int } // NewBootstrapper returns an instance of a Bootstrapper. func NewBootstrapper(p AddressProvider, client *Client) *Bootstrapper { bs := &Bootstrapper{ - provider: p, - client: client, - logger: log.New(os.Stderr, "[cluster-bootstrap] ", log.LstdFlags), - Interval: bootInterval, + provider: p, + client: client, + logger: log.New(os.Stderr, "[cluster-bootstrap] ", log.LstdFlags), + Interval: bootInterval, + checkInterval: bootCheckInterval, } return bs } @@ -161,7 +168,7 @@ func (b *Bootstrapper) SetCredentials(creds *proto.Credentials) { func (b *Bootstrapper) Boot(ctx context.Context, id, raftAddr string, suf Suffrage, done func() bool, timeout time.Duration) error { timeoutT := time.NewTimer(timeout) defer timeoutT.Stop() - tickerT := time.NewTimer(random.Jitter(time.Millisecond)) + tickerT := time.NewTimer(random.Jitter(b.checkInterval)) defer tickerT.Stop() joiner := NewJoiner(b.client, numJoinAttempts, requestTimeout) @@ -169,6 +176,7 @@ func (b *Bootstrapper) Boot(ctx context.Context, id, raftAddr string, suf Suffra for { select { case <-ctx.Done(): + b.nBootCanceled++ if done() { b.logger.Printf("boot operation marked done") b.setBootStatus(BootDone) diff --git a/cluster/bootstrap_test.go b/cluster/bootstrap_test.go index d478680e8..7d5d16a85 100644 --- a/cluster/bootstrap_test.go +++ b/cluster/bootstrap_test.go @@ -135,6 +135,9 @@ func Test_BootstrapperBootCanceledDone(t *testing.T) { if err != nil { t.Fatalf("error returned from canceled boot even though it's done: %s", err) } + if bs.nBootCanceled == 0 { + t.Fatalf("boot not actually canceled") + } } func Test_BootstrapperBootSingleJoin(t *testing.T) {