-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmeter_test.go
More file actions
132 lines (104 loc) · 2.46 KB
/
meter_test.go
File metadata and controls
132 lines (104 loc) · 2.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2016
package instana
import (
"sync"
"testing"
"time"
)
func TestMeterS_Stop(t *testing.T) {
// Create a new meter
m := newMeter(defaultLogger)
// Track if Run is still executing
var wg sync.WaitGroup
wg.Add(1)
// Start the meter in a goroutine
go func() {
defer wg.Done()
m.Run(100 * time.Millisecond)
}()
// Let it run for a bit
time.Sleep(300 * time.Millisecond)
// Stop the meter
m.Stop()
// Wait for Run to exit with a timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Success - Run exited after Stop was called
case <-time.After(2 * time.Second):
t.Fatal("meter.Run() did not exit after Stop() was called")
}
}
func TestMeterS_Run_StopImmediately(t *testing.T) {
// Create a new meter
m := newMeter(defaultLogger)
// Track if Run is still executing
var wg sync.WaitGroup
wg.Add(1)
// Start the meter in a goroutine
go func() {
defer wg.Done()
m.Run(100 * time.Millisecond)
}()
// Stop immediately without waiting
m.Stop()
// Wait for Run to exit with a timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Success - Run exited after Stop was called
case <-time.After(2 * time.Second):
t.Fatal("meter.Run() did not exit after immediate Stop() was called")
}
}
func TestMeterS_CollectMetrics(t *testing.T) {
// Create a new meter
m := newMeter(defaultLogger)
// Collect metrics
metrics := m.collectMetrics()
// Verify metrics are collected
if metrics.Goroutine <= 0 {
t.Errorf("Expected positive goroutine count, got %d", metrics.Goroutine)
}
if metrics.MemoryStats.Alloc == 0 {
t.Error("Expected non-zero memory allocation")
}
}
func TestMeterS_CollectMemoryMetrics(t *testing.T) {
// Create a new meter
m := newMeter(defaultLogger)
// Collect memory metrics
memStats := m.collectMemoryMetrics()
// Verify memory stats are collected
if memStats.Alloc == 0 {
t.Error("Expected non-zero Alloc")
}
if memStats.Sys == 0 {
t.Error("Expected non-zero Sys")
}
if memStats.HeapAlloc == 0 {
t.Error("Expected non-zero HeapAlloc")
}
}
func TestMeterS_NewMeter(t *testing.T) {
// Create a new meter
m := newMeter(defaultLogger)
if m == nil {
t.Fatal("Expected non-nil meter")
}
if m.done == nil {
t.Error("Expected done channel to be initialized")
}
if m.numGC != 0 {
t.Errorf("Expected initial numGC to be 0, got %d", m.numGC)
}
}