Skip to content

Commit

Permalink
add a test for num_threads using a c++ program which runs 42 threads
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Sep 26, 2023
1 parent a33e2d0 commit c9b166f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
55 changes: 55 additions & 0 deletions metric/system/process/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
package process

import (
"bytes"
"os"
"os/exec"
"os/user"
"path"
"strconv"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -133,3 +138,53 @@ func TestParseProcStat(t *testing.T) {

assert.Equal(t, want, got, "")
}

func runThreads(t *testing.T) *exec.Cmd {
t.Helper()

out, err := exec.Command("go", "env", "CXX").CombinedOutput()
if err != nil {
t.Fatalf("go env CXX failed: %v", err)
}
cxx := strings.TrimSpace(string(out))
if len(strings.Fields(cxx)) != 1 {
t.Fatalf("CXX=%q, does not look like a c++ compiler, "+
"it's got more than 1 string", cxx)
}
_, err = exec.LookPath(cxx)
if err != nil {
t.Fatalf("could not find %q: %x\v", cxx, err)
}

threads := path.Join(t.TempDir(), "threads")
wd, err := os.Getwd()
require.NoErrorf(t, err, "could not get current working directory")

theadscpp := path.Join(wd, "testdata", "threads.cpp")

out, err = exec.Command(cxx, "-o", threads, theadscpp).CombinedOutput()
if err != nil {
t.Fatalf("could not compile %q : %v. Output: %s",
threads, err, string(out))
}

var b bytes.Buffer
cmd := exec.Command(threads)
cmd.Stdout = &b
cmd.Stderr = &b

err = cmd.Start()
require.NoErrorf(t, err, "failed to start %q", threads)

var log string
require.Eventually(t,
func() bool {
log += b.String()
return strings.Contains(log, "running")
},
time.Second, 50*time.Millisecond,
"could not determine if %q is running. Output: %q",
threads, log)

return cmd
}
21 changes: 21 additions & 0 deletions metric/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ func TestGetProcess(t *testing.T) {
}
}

func TestGetInfoForPid_numThreads(t *testing.T) {
want := 42
cmd := runThreads(t)

got, err := GetInfoForPid(
resolve.NewTestResolver("/"), cmd.Process.Pid)
require.NoError(t, err, "failed to GetInfoForPid")

if !got.NumThreads.Exists() {
bs, err := json.Marshal(got)
if err != nil {
t.Logf("could not marshal ProcState: %v", err)
}
t.Fatalf("num_thread was not collected. Collected info: %s", bs)
}

numThreads := got.NumThreads.ValueOr(-1)
assert.Equalf(t, want, numThreads,
"want %d threads, got %d", want, numThreads)
}

func TestMatchProcs(t *testing.T) {
var procStats = Stats{}

Expand Down
19 changes: 19 additions & 0 deletions metric/system/process/testdata/threads.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <thread>
#include <vector>

void importantStuff() {
std::this_thread::sleep_for(std::chrono::minutes(42));
}

int main(int argc, char* argv[]) {
std::vector<std::thread> threads;
for (int i = 0; i < 41; ++i) {
threads.emplace_back(importantStuff);
}

std::cout << "running...\n" << std::flush;
importantStuff();

return 0;
}

0 comments on commit c9b166f

Please sign in to comment.