forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
539 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// +build !windows | ||
|
||
package process | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync/atomic" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// test that a restarting process resets pipes properly | ||
func TestRestartingRebindsPipes(t *testing.T) { | ||
exe, err := os.Executable() | ||
require.NoError(t, err) | ||
|
||
p, err := New([]string{exe, "-external"}) | ||
p.RestartDelay = 100 * time.Nanosecond | ||
p.Log = testutil.Logger{} | ||
require.NoError(t, err) | ||
|
||
linesRead := int64(0) | ||
p.ReadStdoutFn = func(r io.Reader) { | ||
scanner := bufio.NewScanner(r) | ||
|
||
for scanner.Scan() { | ||
atomic.AddInt64(&linesRead, 1) | ||
} | ||
} | ||
|
||
require.NoError(t, p.Start()) | ||
|
||
for atomic.LoadInt64(&linesRead) < 1 { | ||
time.Sleep(1 * time.Millisecond) | ||
} | ||
|
||
syscall.Kill(p.Pid(), syscall.SIGKILL) | ||
|
||
for atomic.LoadInt64(&linesRead) < 2 { | ||
time.Sleep(1 * time.Millisecond) | ||
} | ||
|
||
p.Stop() | ||
} | ||
|
||
var external = flag.Bool("external", false, | ||
"if true, run externalProcess instead of tests") | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
if *external { | ||
externalProcess() | ||
os.Exit(0) | ||
} | ||
code := m.Run() | ||
os.Exit(code) | ||
} | ||
|
||
// externalProcess is an external "misbehaving" process that won't exit | ||
// cleanly. | ||
func externalProcess() { | ||
wait := make(chan int, 0) | ||
fmt.Fprintln(os.Stdout, "started") | ||
<-wait | ||
os.Exit(2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.