Skip to content

Commit

Permalink
Add concurrency testing (#1249)
Browse files Browse the repository at this point in the history
* Add test-race make target

* Ensure manager is shutdown in test

* Ensure concurrent safe access to noopProbe

* Add race-test GitHub action workflow

* Update .github/workflows/checks.yml

Co-authored-by: Ron Federman <[email protected]>

---------

Co-authored-by: Ron Federman <[email protected]>
  • Loading branch information
MrAlias and RonFed authored Nov 7, 2024
1 parent 05c1095 commit 1838d09
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ jobs:
run: make license-header-check dependabot-check go-mod-tidy golangci-lint
- name: Check clean repository
run: make check-clean-work-tree
race-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
check-latest: true
cache-dependency-path: "**/go.sum"
- name: Install build dependencies
run: sudo apt-get update && sudo apt-get install -y clang llvm
- name: Run tests
run: make test-race
compatibility-test:
strategy:
matrix:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ $(TOOLS)/offsetgen: PACKAGE=go.opentelemetry.io/auto/$(TOOLS_MOD_DIR)/inspect/cm
.PHONY: tools
tools: $(GOLICENSES) $(MULTIMOD) $(GOLANGCI_LINT) $(DBOTCONF) $(OFFSETGEN)

TEST_TARGETS := test-verbose test-ebpf
TEST_TARGETS := test-verbose test-ebpf test-race
.PHONY: $(TEST_TARGETS) test
test-ebpf: ARGS = -tags=ebpf_test -run ^TestEBPF # These need to be run with sudo.
test-verbose: ARGS = -v
test-race: ARGS = -race
$(TEST_TARGETS): test
test: go-mod-tidy generate $(ALL_GO_MODS:%=test/%)
test/%/go.mod:
Expand Down
33 changes: 23 additions & 10 deletions internal/pkg/instrumentation/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package instrumentation
import (
"context"
"log/slog"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -298,24 +299,24 @@ func (p slowProbe) Close() error {
}

type noopProbe struct {
loaded, running, closed bool
loaded, running, closed atomic.Bool
}

var _ probe.Probe = (*noopProbe)(nil)

func (p *noopProbe) Load(*link.Executable, *process.TargetDetails, *sampling.Config) error {
p.loaded = true
p.loaded.Store(true)
return nil
}

func (p *noopProbe) Run(c chan<- ptrace.ScopeSpans) {
p.running = true
p.running.Store(true)
}

func (p *noopProbe) Close() error {
p.closed = true
p.loaded = false
p.running = false
p.closed.Store(true)
p.loaded.Store(false)
p.running.Store(false)
return nil
}

Expand Down Expand Up @@ -381,7 +382,19 @@ func TestConfigProvider(t *testing.T) {

mockExeAndBpffs(t)
runCtx, cancel := context.WithCancel(context.Background())
go func() { _ = m.Run(runCtx, &process.TargetDetails{PID: 1000}) }()
errCh := make(chan error, 1)
go func() { errCh <- m.Run(runCtx, &process.TargetDetails{PID: 1000}) }()
t.Cleanup(func() {
assert.Eventually(t, func() bool {
select {
case <-errCh:
return true
default:
return false
}
}, time.Second, 10*time.Millisecond)
})

assert.Eventually(t, func() bool {
select {
case <-loadedIndicator:
Expand All @@ -393,17 +406,17 @@ func TestConfigProvider(t *testing.T) {

probeRunning := func(id probe.ID) bool {
p := m.probes[id].(*noopProbe)
return p.loaded && p.running
return p.loaded.Load() && p.running.Load()
}

probePending := func(id probe.ID) bool {
p := m.probes[id].(*noopProbe)
return !p.loaded && !p.running
return !p.loaded.Load() && !p.running.Load()
}

probeClosed := func(id probe.ID) bool {
p := m.probes[id].(*noopProbe)
return p.closed
return p.closed.Load()
}

assert.True(t, probePending(netHTTPClientProbeID))
Expand Down

0 comments on commit 1838d09

Please sign in to comment.