Skip to content

Commit f135e3b

Browse files
committed
Add test for verifying proper shutdown
Signed-off-by: Paschalis Tsilias <paschalis.tsilias@grafana.com>
1 parent f1840f7 commit f135e3b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

internal/examples/supervisor/supervisor/supervisor_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"fmt"
55
"os"
66
"testing"
7-
7+
"time"
8+
89
"github.com/stretchr/testify/assert"
910

1011
"github.com/open-telemetry/opamp-go/internal"
@@ -62,3 +63,33 @@ agent:
6263

6364
supervisor.Shutdown()
6465
}
66+
67+
func TestShutdownRaceCondition(t *testing.T) {
68+
tmpDir := changeCurrentDir(t)
69+
os.WriteFile("supervisor.yaml", []byte(fmt.Sprintf(`
70+
server:
71+
endpoint: ws://127.0.0.1:4320/v1/opamp
72+
agent:
73+
executable: %s/dummy_agent.sh`, tmpDir)), 0644)
74+
75+
os.WriteFile("dummy_agent.sh", []byte("#!/bin/sh\nsleep 9999\n"), 0755)
76+
77+
startOpampServer(t)
78+
79+
// There's no great way to ensure Shutdown gets called before Start.
80+
// The DelayLogger ensures some delay before the goroutine gets started.
81+
var supervisor *Supervisor
82+
var err error
83+
supervisor, err = NewSupervisor(&internal.DelayLogger{})
84+
supervisor.Shutdown()
85+
supervisor.hasNewConfig <- struct{}{}
86+
87+
assert.NoError(t, err)
88+
89+
// The Shutdown method has been called before the runAgentProcess goroutine
90+
// gets started and has a chance to load a new process. Make sure no PID
91+
// has been launched.
92+
assert.Never(t, func() bool {
93+
return supervisor.commander.Pid() != 0
94+
}, 2*time.Second, 10*time.Millisecond)
95+
}

internal/noplogger.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/open-telemetry/opamp-go/client/types"
78
)
@@ -12,3 +13,12 @@ type NopLogger struct{}
1213

1314
func (l *NopLogger) Debugf(ctx context.Context, format string, v ...interface{}) {}
1415
func (l *NopLogger) Errorf(ctx context.Context, format string, v ...interface{}) {}
16+
17+
type DelayLogger struct{}
18+
19+
func (l *DelayLogger) Debugf(ctx context.Context, format string, v ...interface{}) {
20+
time.Sleep(10 * time.Millisecond)
21+
}
22+
func (l *DelayLogger) Errorf(ctx context.Context, format string, v ...interface{}) {
23+
time.Sleep(10 * time.Millisecond)
24+
}

0 commit comments

Comments
 (0)