Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Jul 9, 2024
1 parent c4a714d commit e40257c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
14 changes: 5 additions & 9 deletions instrumentation/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,25 @@ func TestRefreshGoCollector(t *testing.T) {
// before the first refresh, all counters are zero
assert.Zero(t, collector.get(goMemoryAllocations))
// after the first refresh, counters are non-zero
allocateMemory(buffer)
buffer = allocateMemory(buffer)
collector.refresh()
initialAllocations := collector.get(goMemoryAllocations)
assert.NotZero(t, initialAllocations)
// if less than the refresh time has elapsed, the value is not updated
// on refresh.
testClock.increment(9 * time.Second)
collector.refresh()
allocateMemory(buffer)
buffer = allocateMemory(buffer)
assert.Equal(t, initialAllocations, collector.get(goMemoryAllocations))
// if greater than the refresh time has elapsed, the value changes.
testClock.increment(2 * time.Second)
collector.refresh()
allocateMemory(buffer)
_ = allocateMemory(buffer)
assert.NotEqual(t, initialAllocations, collector.get(goMemoryAllocations))
}

func allocateMemory(buffer [][]byte) {
newBuffer := make([]byte, 1000000)
for i := range newBuffer {
newBuffer[i] = 0
}
buffer = append(buffer, newBuffer)
func allocateMemory(buffer [][]byte) [][]byte {
return append(buffer, make([]byte, 1000000))
}

func newClock() *clock {
Expand Down
10 changes: 3 additions & 7 deletions instrumentation/runtime/test/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func TestRuntimeWithLimit(t *testing.T) {
// buffer for allocating memory
var buffer [][]byte
allocateMemory(buffer)
_ = allocateMemory(buffer)
t.Setenv("OTEL_GO_X_DEPRECATED_RUNTIME_METRICS", "false")
debug.SetMemoryLimit(1234567890)
// reset to default
Expand Down Expand Up @@ -152,10 +152,6 @@ func assertNonZeroValues(t *testing.T, sm metricdata.ScopeMetrics) {
}
}

func allocateMemory(buffer [][]byte) {
newBuffer := make([]byte, 1000000)
for i := range newBuffer {
newBuffer[i] = 0
}
buffer = append(buffer, newBuffer)
func allocateMemory(buffer [][]byte) [][]byte {
return append(buffer, make([]byte, 1000000))
}

0 comments on commit e40257c

Please sign in to comment.