Skip to content

Commit

Permalink
More robust testing
Browse files Browse the repository at this point in the history
  • Loading branch information
otoolep committed Apr 27, 2024
1 parent 7012a1c commit 3394bf3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cluster/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -161,14 +168,15 @@ 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)
joiner.SetCredentials(b.creds)
for {
select {
case <-ctx.Done():
b.nBootCanceled++
if done() {
b.logger.Printf("boot operation marked done")
b.setBootStatus(BootDone)
Expand Down
3 changes: 3 additions & 0 deletions cluster/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3394bf3

Please sign in to comment.