Skip to content

Commit

Permalink
Allow checkup logger interrupt to be called multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Oct 11, 2023
1 parent 1429279 commit af5086f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/debug/checkups/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type (
}

checkPointer struct {
logger logger
knapsack types.Knapsack
interrupt chan struct{}
logger logger
knapsack types.Knapsack
interrupt chan struct{}
interrupted bool
}
)

Expand Down Expand Up @@ -53,6 +54,13 @@ func (c *checkPointer) Run() error {
}

func (c *checkPointer) Interrupt(_ error) {
// Only perform shutdown tasks on first call to interrupt -- no need to repeat on potential extra calls.
if c.interrupted {
return
}

c.interrupted = true

c.interrupt <- struct{}{}
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/debug/checkups/checkpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package checkups

import (
"errors"
"testing"
"time"

"github.com/go-kit/kit/log"
storageci "github.com/kolide/launcher/pkg/agent/storage/ci"
typesmocks "github.com/kolide/launcher/pkg/agent/types/mocks"
"github.com/stretchr/testify/require"
)

func TestInterrupt_Multiple(t *testing.T) {
t.Parallel()

mockKnapsack := typesmocks.NewKnapsack(t)
mockKnapsack.On("UpdateChannel").Return("nightly").Maybe()
mockKnapsack.On("TufServerURL").Return("localhost").Maybe()
mockKnapsack.On("BboltDB").Return(storageci.SetupDB(t)).Maybe()
mockKnapsack.On("KolideHosted").Return(false).Maybe()
mockKnapsack.On("KolideServerURL").Return("localhost").Maybe()
mockKnapsack.On("ControlServerURL").Return("localhost").Maybe()
mockKnapsack.On("InsecureTransportTLS").Return(true).Maybe()
checkupLogger := NewCheckupLogger(log.NewNopLogger(), mockKnapsack)
mockKnapsack.AssertExpectations(t)

// Start and then interrupt
go checkupLogger.Run()
checkupLogger.Interrupt(errors.New("test error"))

// Confirm we can call Interrupt multiple times without blocking
interruptComplete := make(chan struct{})
expectedInterrupts := 3
for i := 0; i < expectedInterrupts; i += 1 {
go func() {
checkupLogger.Interrupt(nil)
interruptComplete <- struct{}{}
}()
}

receivedInterrupts := 0
for {
if receivedInterrupts >= expectedInterrupts {
break
}

select {
case <-interruptComplete:
receivedInterrupts += 1
continue
case <-time.After(5 * time.Second):
t.Errorf("could not call interrupt multiple times and return within 5 seconds -- received %d interrupts before timeout", receivedInterrupts)
t.FailNow()
}
}

require.Equal(t, expectedInterrupts, receivedInterrupts)
}

0 comments on commit af5086f

Please sign in to comment.