Skip to content

Commit

Permalink
Add tests for signal closing shutdown (#5910)
Browse files Browse the repository at this point in the history
Avoid using directly the signal channel outside of the test and struct itself.

Signed-off-by: Bogdan <[email protected]>

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 15, 2022
1 parent df91e0e commit e89786d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
10 changes: 4 additions & 6 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ func New(set CollectorSettings) (*Collector, error) {
}

return &Collector{
set: set,
state: atomic.NewInt32(int32(Starting)),
shutdownChan: make(chan struct{}),
signalsChannel: make(chan os.Signal, 1),
asyncErrorChannel: make(chan error),

set: set,
state: atomic.NewInt32(int32(Starting)),
shutdownChan: make(chan struct{}),
}, nil

}

// GetState returns current state of the collector server.
Expand All @@ -127,7 +126,6 @@ func (col *Collector) Shutdown() {
func (col *Collector) runAndWaitForShutdownEvent(ctx context.Context) error {
col.service.telemetrySettings.Logger.Info("Everything is ready. Begin running and processing data.")

col.signalsChannel = make(chan os.Signal, 1)
// Only notify with SIGTERM and SIGINT if graceful shutdown is enabled.
if !col.set.DisableGracefulShutdown {
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
Expand Down
29 changes: 28 additions & 1 deletion service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ func TestCollectorReportError(t *testing.T) {
assert.Equal(t, Closed, col.GetState())
}

func TestCollectorSendSignal(t *testing.T) {
factories, err := componenttest.NopFactories()
require.NoError(t, err)

cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := New(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: cfgProvider,
telemetry: newColTelemetry(featuregate.NewRegistry()),
})
require.NoError(t, err)

wg := startCollector(context.Background(), t, col)

assert.Eventually(t, func() bool {
return Running == col.GetState()
}, 2*time.Second, 200*time.Millisecond)

col.signalsChannel <- syscall.SIGTERM

wg.Wait()
assert.Equal(t, Closed, col.GetState())
}

func TestCollectorFailedShutdown(t *testing.T) {
t.Skip("This test was using telemetry shutdown failure, switch to use a component that errors on shutdown.")
factories, err := componenttest.NopFactories()
Expand Down Expand Up @@ -321,7 +348,7 @@ func testCollectorStartHelper(t *testing.T, telemetry *telemetryInitializer, tc
assertMetrics(t, metricsAddr, tc.expectedLabels)

assertZPages(t)
col.signalsChannel <- syscall.SIGTERM
col.Shutdown()

wg.Wait()
assert.Equal(t, Closed, col.GetState())
Expand Down
4 changes: 1 addition & 3 deletions service/collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"flag"
"fmt"
"os"
"syscall"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -127,8 +126,7 @@ func (s *windowsService) start(elog *eventlog.Log, colErrorChannel chan error) e
}

func (s *windowsService) stop(colErrorChannel chan error) error {
// simulate a SIGTERM signal to terminate the collector server
s.col.signalsChannel <- syscall.SIGTERM
s.col.Shutdown()
// return the response of col.Start
return <-colErrorChannel
}
Expand Down

0 comments on commit e89786d

Please sign in to comment.