Skip to content

Commit 1644a59

Browse files
Add more test coverage to statistics
1 parent ad85b54 commit 1644a59

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pkg/statistics/statistics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ func (stats *Statistics) EventsBroken() uint64 {
199199
return stats.eventsBroken.Load()
200200
}
201201

202+
func (stats *Statistics) BytesAPISent() uint64 {
203+
return stats.bytesAPISent.Load()
204+
}
205+
206+
func (stats *Statistics) BytesAPIAccepted() uint64 {
207+
return stats.bytesAPIAccepted.Load()
208+
}
209+
202210
func (stats *Statistics) BuffersEnqueuedAdd(i uint64) {
203211
stats.buffersEnqueued.Add(i)
204212
stats.add(stats.cBuffersEnqueued, i)

pkg/statistics/statistics_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package statistics
1919
import (
2020
"math/rand"
2121
"testing"
22+
"time"
2223

2324
"github.com/stretchr/testify/require"
2425
"go.opentelemetry.io/otel"
@@ -143,3 +144,69 @@ func TestEventsBroken(t *testing.T) {
143144
})
144145
}
145146
}
147+
148+
func TestBytesAPISent(t *testing.T) {
149+
for _, tt := range tests {
150+
t.Run(tt.name, func(*testing.T) {
151+
stats, err := NewStatistics(tt.meter)
152+
require.Nil(t, err)
153+
v := uint64(rand.Int())
154+
stats.BytesAPISentAdd(v)
155+
assert.Equal(t, v, stats.BytesAPISent())
156+
})
157+
}
158+
}
159+
160+
func TestBytesAPIAccepted(t *testing.T) {
161+
for _, tt := range tests {
162+
t.Run(tt.name, func(*testing.T) {
163+
stats, err := NewStatistics(tt.meter)
164+
require.Nil(t, err)
165+
v := uint64(rand.Int())
166+
stats.BytesAPIAcceptedAdd(v)
167+
assert.Equal(t, v, stats.BytesAPIAccepted())
168+
})
169+
}
170+
}
171+
172+
func TestExport(t *testing.T) {
173+
stats, err := NewStatistics(nil)
174+
require.Nil(t, err)
175+
176+
stats.BuffersEnqueuedAdd(1000)
177+
stats.BuffersProcessedAdd(100)
178+
stats.BuffersDroppedAdd(10)
179+
stats.BuffersBrokenAdd(1)
180+
181+
stats.EventsEnqueuedAdd(2000)
182+
stats.EventsProcessedAdd(200)
183+
stats.EventsDroppedAdd(20)
184+
stats.EventsBrokenAdd(2)
185+
186+
stats.BytesAPISentAdd(3000)
187+
stats.BytesAPIAcceptedAdd(300)
188+
189+
exp := stats.Export(time.Second)
190+
assert.Equal(t, &ExportedStatistics{
191+
Buffers: QueueStats{
192+
enqueued: 1000,
193+
processed: 100,
194+
dropped: 10,
195+
broken: 1,
196+
processingTime: time.Second,
197+
},
198+
Events: QueueStats{
199+
enqueued: 2000,
200+
processed: 200,
201+
dropped: 20,
202+
broken: 2,
203+
processingTime: time.Second,
204+
},
205+
Transfer: TransferStats{
206+
bytesSent: 3000,
207+
bytesAccepted: 300,
208+
buffersProcessed: 100,
209+
processingTime: time.Second,
210+
},
211+
}, exp)
212+
}

0 commit comments

Comments
 (0)