forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_test.go
53 lines (44 loc) · 1.52 KB
/
runtime_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRefreshGoCollector(t *testing.T) {
// buffer for allocating memory
var buffer [][]byte
collector := newCollector(10 * time.Second)
testClock := newClock()
collector.now = testClock.now
// before the first refresh, all counters are zero
assert.Zero(t, collector.get(goMemoryAllocations))
// after the first refresh, counters are non-zero
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()
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)
assert.NotEqual(t, initialAllocations, collector.get(goMemoryAllocations))
}
func allocateMemory(buffer [][]byte) [][]byte {
return append(buffer, make([]byte, 1000000))
}
func newClock() *clock {
return &clock{current: time.Now()}
}
type clock struct {
current time.Time
}
func (c *clock) now() time.Time { return c.current }
func (c *clock) increment(d time.Duration) { c.current = c.current.Add(d) }