Skip to content

Commit

Permalink
misc cleanups
Browse files Browse the repository at this point in the history
- `min` is now builtin
- `RWMutex` captures intent better
  • Loading branch information
silvestre committed Apr 2, 2024
1 parent b5838e0 commit 9f7bf82
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/acceptance/assets/app/go_app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
14 changes: 3 additions & 11 deletions src/acceptance/assets/app/go_app/internal/app/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-logr/logr"
"golang.org/x/exp/constraints"
)

//counterfeiter:generate . CPUWaster
Expand All @@ -20,7 +19,7 @@ type CPUWaster interface {
}

type ConcurrentBusyLoopCPUWaster struct {
mu sync.Mutex
mu sync.RWMutex
isRunning bool
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
6 changes: 3 additions & 3 deletions src/acceptance/assets/app/go_app/internal/app/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9f7bf82

Please sign in to comment.