Skip to content

Commit

Permalink
Fix flake in streamer utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Jan 30, 2019
1 parent b3541ac commit a23dc9a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/stream/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package stream

import (
"fmt"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestGetProcessEnvironment(t *testing.T) {
"BAR=asd",
})
defer tc.Stop()
waitForChildExec(t, tc.Pid())
env, err := getProcessEnvironment(int32(tc.Pid()))
if err != nil {
t.Error(err)
Expand All @@ -80,3 +82,25 @@ func TestGetProcessEnvironment(t *testing.T) {
}
}
}

// waitForChildExec waits for a child process to perform exec()
// by means of waiting when its `/proc/PID/cmdline` becomes different from the
// cmdline of the current process
func waitForChildExec(t *testing.T, pid int) {
ownPid := os.Getpid()
ownCmdline, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", ownPid))
if err != nil {
t.Fatalf("cannot read own cmdline: %v", err)
}
processCmdlineLocation := fmt.Sprintf("/proc/%d/cmdline", pid)
for {
processCmdline, err := ioutil.ReadFile(processCmdlineLocation)
if err != nil {
t.Fatalf("cannot read cmdline for pid %q: %v", pid, err)
}
if string(processCmdline) != string(ownCmdline) {
break
}
time.Sleep(100 * time.Millisecond)
}
}

0 comments on commit a23dc9a

Please sign in to comment.