Flaky shim test (#7656)
This commit is contained in:
parent
e00424d7b2
commit
dda46ea32b
|
|
@ -106,7 +106,7 @@ func (s *Shim) Run(pollInterval time.Duration) error {
|
|||
}
|
||||
gatherPromptCh := make(chan empty, 1)
|
||||
s.gatherPromptChans = append(s.gatherPromptChans, gatherPromptCh)
|
||||
wg.Add(1)
|
||||
wg.Add(1) // one per input
|
||||
go func(input telegraf.Input) {
|
||||
startGathering(ctx, input, acc, gatherPromptCh, pollInterval)
|
||||
if serviceInput, ok := input.(telegraf.ServiceInput); ok {
|
||||
|
|
@ -216,11 +216,7 @@ func startGathering(ctx context.Context, input telegraf.Input, acc telegraf.Accu
|
|||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case _, open := <-gatherPromptCh:
|
||||
if !open {
|
||||
// stdin has closed.
|
||||
return
|
||||
}
|
||||
case <-gatherPromptCh:
|
||||
if err := input.Gather(acc); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to gather metrics: %s", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,13 +51,7 @@ func TestShimUSR1SignalingWorks(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
|
||||
select {
|
||||
case <-metricProcessed:
|
||||
case <-timeout.C:
|
||||
require.Fail(t, "Timeout waiting for metric to arrive")
|
||||
}
|
||||
<-metricProcessed
|
||||
cancel()
|
||||
|
||||
r := bufio.NewReader(stdoutReader)
|
||||
|
|
@ -66,5 +60,7 @@ func TestShimUSR1SignalingWorks(t *testing.T) {
|
|||
require.Equal(t, "measurement,tag=tag field=1i 1234000005678\n", out)
|
||||
|
||||
stdinWriter.Close()
|
||||
readUntilEmpty(r)
|
||||
|
||||
<-exited
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,23 +21,13 @@ func TestShimWorks(t *testing.T) {
|
|||
|
||||
stdin, _ = io.Pipe() // hold the stdin pipe open
|
||||
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
metricProcessed, _ := runInputPlugin(t, 10*time.Millisecond)
|
||||
|
||||
select {
|
||||
case <-metricProcessed:
|
||||
case <-timeout.C:
|
||||
require.Fail(t, "Timeout waiting for metric to arrive")
|
||||
}
|
||||
<-metricProcessed
|
||||
for stdoutBytes.Len() == 0 {
|
||||
select {
|
||||
case <-timeout.C:
|
||||
require.Fail(t, "Timeout waiting to read metric from stdout")
|
||||
return
|
||||
default:
|
||||
t.Log("Waiting for bytes available in stdout")
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
out := string(stdoutBytes.Bytes())
|
||||
require.Contains(t, out, "\n")
|
||||
|
|
@ -52,16 +42,11 @@ func TestShimStdinSignalingWorks(t *testing.T) {
|
|||
stdin = stdinReader
|
||||
stdout = stdoutWriter
|
||||
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
metricProcessed, exited := runInputPlugin(t, 40*time.Second)
|
||||
|
||||
stdinWriter.Write([]byte("\n"))
|
||||
|
||||
select {
|
||||
case <-metricProcessed:
|
||||
case <-timeout.C:
|
||||
require.Fail(t, "Timeout waiting for metric to arrive")
|
||||
}
|
||||
<-metricProcessed
|
||||
|
||||
r := bufio.NewReader(stdoutReader)
|
||||
out, err := r.ReadString('\n')
|
||||
|
|
@ -69,12 +54,15 @@ func TestShimStdinSignalingWorks(t *testing.T) {
|
|||
require.Equal(t, "measurement,tag=tag field=1i 1234000005678\n", out)
|
||||
|
||||
stdinWriter.Close()
|
||||
|
||||
readUntilEmpty(r)
|
||||
|
||||
// check that it exits cleanly
|
||||
<-exited
|
||||
}
|
||||
|
||||
func runInputPlugin(t *testing.T, interval time.Duration) (metricProcessed chan bool, exited chan bool) {
|
||||
metricProcessed = make(chan bool)
|
||||
metricProcessed = make(chan bool, 10)
|
||||
exited = make(chan bool)
|
||||
inp := &testInput{
|
||||
metricProcessed: metricProcessed,
|
||||
|
|
@ -172,3 +160,15 @@ func (i *serviceInput) Start(acc telegraf.Accumulator) error {
|
|||
|
||||
func (i *serviceInput) Stop() {
|
||||
}
|
||||
|
||||
// we can get stuck if stdout gets clogged up and nobody's reading from it.
|
||||
// make sure we keep it going
|
||||
func readUntilEmpty(r *bufio.Reader) {
|
||||
go func() {
|
||||
var err error
|
||||
for err != io.EOF {
|
||||
_, err = r.ReadString('\n')
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue