Skip to content

Commit b3b8b87

Browse files
committed
added tests to improve coverage
Signed-off-by: Andreas Gkizas <[email protected]>
1 parent 25ea91a commit b3b8b87

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

exporter/exporterhelper/internal/queue/persistent_queue_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ func (int64Encoding) Unmarshal(bytes []byte) (context.Context, int64, error) {
5858
return context.Background(), val, nil
5959
}
6060

61+
func TestPersistentQueue_CorruptedItemOnRead(t *testing.T) {
62+
ext := storagetest.NewMockStorageExtension(nil)
63+
pq := createTestPersistentQueueWithRequestsSizer(t, ext, 100)
64+
65+
// Add one item
66+
require.NoError(t, pq.Offer(context.Background(), int64(123)))
67+
assert.EqualValues(t, 1, pq.Size())
68+
assert.EqualValues(t, 123, pq.metadata.ItemsSize)
69+
assert.EqualValues(t, 1230, pq.metadata.BytesSize)
70+
71+
// Corrupt the item in storage
72+
require.NoError(t, pq.client.Set(context.Background(), getItemKey(0), []byte("invalid data")))
73+
74+
// Try to read the item. It should fail to unmarshal.
75+
// The loop in Read continues, and since there are no more items, Read will block.
76+
readFinished := make(chan struct{})
77+
go func() {
78+
_, _, _, ok := pq.Read(context.Background())
79+
// Read returns false because the queue is stopped.
80+
assert.False(t, ok)
81+
close(readFinished)
82+
}()
83+
84+
// Wait a bit to ensure Read has processed the corrupted item and is now waiting.
85+
assert.Eventually(t, func() bool {
86+
pq.mu.Lock()
87+
defer pq.mu.Unlock()
88+
// After the failed read, the queue should be empty and sizes should be reset.
89+
return pq.requestSize() == 0 && pq.metadata.BytesSize == 0 && pq.metadata.ItemsSize == 0
90+
}, time.Second, 10*time.Millisecond, "queue size and byte/item sizes should be reset to 0")
91+
92+
// Shutdown the queue to unblock Read.
93+
require.NoError(t, pq.Shutdown(context.Background()))
94+
<-readFinished
95+
}
96+
6197
func newFakeBoundedStorageClient(maxSizeInBytes int) *fakeBoundedStorageClient {
6298
return &fakeBoundedStorageClient{
6399
st: map[string][]byte{},

exporter/exporterhelper/logs_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func TestLogsEncoding_UnmarshalError(t *testing.T) {
6868

6969
enc := logsEncoding{}
7070
ctx, req, err := enc.Unmarshal([]byte("!invalid-proto"))
71-
assert.Error(t, err)
72-
assert.Nil(t, req)
73-
assert.NotNil(t, ctx)
71+
require.Error(t, err)
72+
require.Nil(t, req)
73+
require.NotNil(t, ctx)
7474
}
7575

7676
func TestLogs_InvalidName(t *testing.T) {

exporter/exporterhelper/metrics_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func TestMetricsEncoding_UnmarshalError(t *testing.T) {
6868

6969
enc := metricsEncoding{}
7070
ctx, req, err := enc.Unmarshal([]byte("!invalid-proto"))
71-
assert.Error(t, err)
72-
assert.Nil(t, req)
73-
assert.NotNil(t, ctx)
71+
require.Error(t, err)
72+
require.Nil(t, req)
73+
require.NotNil(t, ctx)
7474
}
7575

7676
func TestMetrics_NilConfig(t *testing.T) {

exporter/exporterhelper/traces_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func TestTracesEncoding_UnmarshalError(t *testing.T) {
6464

6565
enc := tracesEncoding{}
6666
ctx, req, err := enc.Unmarshal([]byte("!invalid-proto"))
67-
assert.Error(t, err)
68-
assert.Nil(t, req)
69-
assert.NotNil(t, ctx)
67+
require.Error(t, err)
68+
require.Nil(t, req)
69+
require.NotNil(t, ctx)
7070
}
7171

7272
func TestTraces_InvalidName(t *testing.T) {

exporter/exporterhelper/xexporterhelper/profiles_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ func TestProfilesRequest(t *testing.T) {
5757
)
5858
}
5959

60+
func TestProfilesEncoding_UnmarshalError(t *testing.T) {
61+
fgOrigReadState := queue.PersistRequestContextOnRead
62+
queue.PersistRequestContextOnRead = func() bool { return false }
63+
t.Cleanup(func() {
64+
queue.PersistRequestContextOnRead = fgOrigReadState
65+
})
66+
67+
enc := profilesEncoding{}
68+
ctx, req, err := enc.Unmarshal([]byte("!invalid-proto"))
69+
require.Error(t, err)
70+
require.Nil(t, req)
71+
require.NotNil(t, ctx)
72+
}
73+
6074
func TestProfilesExporter_InvalidName(t *testing.T) {
6175
le, err := NewProfiles(context.Background(), exportertest.NewNopSettings(exportertest.NopType), nil, newPushProfilesData(nil))
6276
require.Nil(t, le)

0 commit comments

Comments
 (0)