From 9f7bf824d73045b258e9ed8a515e3fd6d56f1488 Mon Sep 17 00:00:00 2001 From: Silvestre Zabala Date: Tue, 2 Apr 2024 15:22:40 +0200 Subject: [PATCH] misc cleanups - `min` is now builtin - `RWMutex` captures intent better --- src/acceptance/assets/app/go_app/go.mod | 2 +- .../assets/app/go_app/internal/app/cpu.go | 14 +++----------- .../assets/app/go_app/internal/app/cpu_test.go | 6 +++--- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/acceptance/assets/app/go_app/go.mod b/src/acceptance/assets/app/go_app/go.mod index 443f0200f3..443bd29177 100644 --- a/src/acceptance/assets/app/go_app/go.mod +++ b/src/acceptance/assets/app/go_app/go.mod @@ -25,7 +25,6 @@ require ( go.opentelemetry.io/otel/sdk v1.24.0 go.opentelemetry.io/otel/trace v1.24.0 go.uber.org/zap v1.27.0 - golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 ) require ( @@ -62,6 +61,7 @@ require ( go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.7.0 // indirect golang.org/x/crypto v0.21.0 // indirect + golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect golang.org/x/mod v0.16.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sync v0.6.0 // indirect diff --git a/src/acceptance/assets/app/go_app/internal/app/cpu.go b/src/acceptance/assets/app/go_app/internal/app/cpu.go index ef33342f83..35733cb307 100644 --- a/src/acceptance/assets/app/go_app/internal/app/cpu.go +++ b/src/acceptance/assets/app/go_app/internal/app/cpu.go @@ -9,7 +9,6 @@ import ( "github.com/gin-gonic/gin" "github.com/go-logr/logr" - "golang.org/x/exp/constraints" ) //counterfeiter:generate . CPUWaster @@ -20,7 +19,7 @@ type CPUWaster interface { } type ConcurrentBusyLoopCPUWaster struct { - mu sync.Mutex + mu sync.RWMutex isRunning bool } @@ -98,8 +97,8 @@ func (m *ConcurrentBusyLoopCPUWaster) UseCPU(utilisation uint64, duration time.D } func (m *ConcurrentBusyLoopCPUWaster) IsRunning() bool { - m.mu.Lock() - defer m.mu.Unlock() + m.mu.RLock() + defer m.mu.RUnlock() return m.isRunning } @@ -114,10 +113,3 @@ func (m *ConcurrentBusyLoopCPUWaster) startTest() { defer m.mu.Unlock() m.isRunning = true } - -func min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - return b -} diff --git a/src/acceptance/assets/app/go_app/internal/app/cpu_test.go b/src/acceptance/assets/app/go_app/internal/app/cpu_test.go index 73af7ca7af..0d8d3c7fdd 100644 --- a/src/acceptance/assets/app/go_app/internal/app/cpu_test.go +++ b/src/acceptance/assets/app/go_app/internal/app/cpu_test.go @@ -59,14 +59,14 @@ var _ = Describe("CPU tests", func() { Context("UseCPU", FlakeAttempts(3), func() { DescribeTable("should use cpu", func(utilisation uint64, duration time.Duration) { - oldCpu := getTotalCPUUsage("before cpuTest info test") + oldCpu := getTotalCPUUsage("before test") - By("allocating cpu") + By("wasting cpu time") cpuWaster := &app.ConcurrentBusyLoopCPUWaster{} cpuWaster.UseCPU(utilisation, duration) Expect(cpuWaster.IsRunning()).To(Equal(true)) Eventually(cpuWaster.IsRunning).WithTimeout(duration + time.Second).WithPolling(time.Second).Should(Equal(false)) - newCpu := getTotalCPUUsage("after cpuTest info test") + newCpu := getTotalCPUUsage("after test") expectedCPUUsage := multiplyDurationByPercentage(duration, utilisation) // Give 10% tolerance - but at least 1 second, as this is the internal resolution of the CPU waster tolerance := max(multiplyDurationByPercentage(expectedCPUUsage, 10), time.Second)