Skip to content

Commit

Permalink
[chore] [exporterhelper] Cleanup mock storage test helpers (#8349)
Browse files Browse the repository at this point in the history
Remove redundant test helpers
  • Loading branch information
dmitryax authored Sep 7, 2023
1 parent bcb00a7 commit 744eb93
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 62 deletions.
4 changes: 2 additions & 2 deletions exporter/exporterhelper/internal/mock_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (m mockStorageExtension) GetClient(_ context.Context, _ component.Kind, _ c
return &mockStorageClient{st: map[string][]byte{}}, nil
}

func NewMockStorageExtension() storage.Extension {
return &mockStorageExtension{}
func NewMockStorageExtension(getClientError error) storage.Extension {
return &mockStorageExtension{getClientError: getClientError}
}

type mockStorageClient struct {
Expand Down
9 changes: 3 additions & 6 deletions exporter/exporterhelper/internal/persistent_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createTestQueue(t *testing.T, capacity, numConsumers int, callback func(ite
pq := NewPersistentQueue(capacity, numConsumers, component.ID{}, newFakeTracesRequestMarshalerFunc(),
newFakeTracesRequestUnmarshalerFunc())
host := &mockHost{ext: map[component.ID]component.Component{
{}: createStorageExtension(t.TempDir()),
{}: NewMockStorageExtension(nil),
}}
err := pq.Start(context.Background(), host, newNopQueueSettings(callback))
require.NoError(t, err)
Expand All @@ -45,13 +45,11 @@ func createTestQueue(t *testing.T, capacity, numConsumers int, callback func(ite
}

func TestPersistentQueue_Capacity(t *testing.T) {
path := t.TempDir()

for i := 0; i < 100; i++ {
pq := NewPersistentQueue(5, 1, component.ID{}, newFakeTracesRequestMarshalerFunc(),
newFakeTracesRequestUnmarshalerFunc())
host := &mockHost{ext: map[component.ID]component.Component{
{}: createStorageExtension(path),
{}: NewMockStorageExtension(nil),
}}
err := pq.Start(context.Background(), host, newNopQueueSettings(func(req Request) {}))
require.NoError(t, err)
Expand Down Expand Up @@ -252,8 +250,7 @@ func TestToStorageClient(t *testing.T) {

var extensions = map[component.ID]component.Component{}
for i := 0; i < tC.numStorages; i++ {
extensions[component.NewIDWithName("file_storage",
strconv.Itoa(i))] = &mockStorageExtension{getClientError: tC.getClientError}
extensions[component.NewIDWithName("file_storage", strconv.Itoa(i))] = NewMockStorageExtension(tC.getClientError)
}
host := &mockHost{ext: extensions}
ownerID := component.NewID("foo_exporter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
)

func TestPersistentStorageBatch_Operations(t *testing.T) {
path := t.TempDir()

ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

Expand Down
35 changes: 9 additions & 26 deletions exporter/exporterhelper/internal/persistent_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)

func createStorageExtension(_ string) storage.Extension {
// After having storage moved to core, we could leverage storagetest.NewTestExtension(nil, path)
return NewMockStorageExtension()
}

func createTestClient(extension storage.Extension) storage.Client {
client, err := extension.GetClient(context.Background(), component.KindReceiver, component.ID{}, "")
if err != nil {
Expand Down Expand Up @@ -86,8 +81,6 @@ func newFakeTracesRequestMarshalerFunc() RequestMarshaler {
}

func TestPersistentStorage_CorruptedData(t *testing.T) {
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)

Expand Down Expand Up @@ -152,7 +145,7 @@ func TestPersistentStorage_CorruptedData(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

Expand Down Expand Up @@ -202,12 +195,10 @@ func TestPersistentStorage_CorruptedData(t *testing.T) {
}

func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)

ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

Expand Down Expand Up @@ -269,13 +260,12 @@ func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
// close to full and with some items dispatched
func TestPersistentStorage_StartWithNonDispatched(t *testing.T) {
var capacity uint64 = 5 // arbitrary small number
path := t.TempDir()
logger := zap.NewNop()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)

ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorageWithLoggingAndCapacity(client, logger, capacity)

Expand Down Expand Up @@ -307,13 +297,11 @@ func TestPersistentStorage_StartWithNonDispatched(t *testing.T) {
}

func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
path := t.TempDir()

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)

for i := 0; i < 10; i++ {
ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)
require.Equal(t, uint64(0), ps.size())
Expand All @@ -328,7 +316,7 @@ func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
require.NoError(t, err)

// TODO: when replacing mock with real storage, this could actually be uncommented
// ext = createStorageExtension(path)
// ext = NewMockStorageExtension(nil)
// ps = createTestPersistentStorage(ext)

// The first element should be already picked by loop
Expand All @@ -349,16 +337,14 @@ func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
}

// No more items
ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
wq := createTestQueue(t, 1000, 1, func(Request) {})
require.Equal(t, 0, wq.Size())
require.NoError(t, ext.Shutdown(context.Background()))
}

func TestPersistentStorage_EmptyRequest(t *testing.T) {
path := t.TempDir()

ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

Expand Down Expand Up @@ -394,8 +380,7 @@ func BenchmarkPersistentStorage_TraceSpans(b *testing.B) {

for _, c := range cases {
b.Run(fmt.Sprintf("#traces: %d #spansPerTrace: %d", c.numTraces, c.numSpansPerTrace), func(bb *testing.B) {
path := bb.TempDir()
ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorageWithLoggingAndCapacity(client, zap.NewNop(), 10000000)

Expand Down Expand Up @@ -453,9 +438,7 @@ func TestPersistentStorage_ItemIndexMarshaling(t *testing.T) {
}

func TestPersistentStorage_StopShouldCloseClient(t *testing.T) {
path := t.TempDir()

ext := createStorageExtension(path)
ext := NewMockStorageExtension(nil)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

Expand Down
2 changes: 1 addition & 1 deletion exporter/exporterhelper/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestLogsExporter_WithPersistentQueue(t *testing.T) {
require.NoError(t, err)

host := &mockHost{ext: map[component.ID]component.Component{
storageID: internal.NewMockStorageExtension(),
storageID: internal.NewMockStorageExtension(nil),
}}
require.NoError(t, te.Start(context.Background(), host))
t.Cleanup(func() { require.NoError(t, te.Shutdown(context.Background())) })
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporterhelper/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestMetricsExporter_WithPersistentQueue(t *testing.T) {
require.NoError(t, err)

host := &mockHost{ext: map[component.ID]component.Component{
storageID: internal.NewMockStorageExtension(),
storageID: internal.NewMockStorageExtension(nil),
}}
require.NoError(t, te.Start(context.Background(), host))
t.Cleanup(func() { require.NoError(t, te.Shutdown(context.Background())) })
Expand Down
24 changes: 2 additions & 22 deletions exporter/exporterhelper/queued_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/extension/experimental/storage"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport/obsreporttest"
)
Expand Down Expand Up @@ -470,7 +469,7 @@ func TestQueuedRetryPersistenceEnabled(t *testing.T) {
require.NoError(t, err)

var extensions = map[component.ID]component.Component{
storageID: &mockStorageExtension{},
storageID: internal.NewMockStorageExtension(nil),
}
host := &mockHost{ext: extensions}

Expand All @@ -497,7 +496,7 @@ func TestQueuedRetryPersistenceEnabledStorageError(t *testing.T) {
require.NoError(t, err)

var extensions = map[component.ID]component.Component{
storageID: &mockStorageExtension{GetClientError: storageError},
storageID: internal.NewMockStorageExtension(storageError),
}
host := &mockHost{ext: extensions}

Expand Down Expand Up @@ -727,25 +726,6 @@ func (nh *mockHost) GetExtensions() map[component.ID]component.Component {
return nh.ext
}

type mockStorageExtension struct {
GetClientError error
}

func (mse *mockStorageExtension) Start(_ context.Context, _ component.Host) error {
return nil
}

func (mse *mockStorageExtension) Shutdown(_ context.Context) error {
return nil
}

func (mse *mockStorageExtension) GetClient(_ context.Context, _ component.Kind, _ component.ID, _ string) (storage.Client, error) {
if mse.GetClientError != nil {
return nil, mse.GetClientError
}
return storage.NewNopClient(), nil
}

type producerConsumerQueueWithCounter struct {
internal.ProducerConsumerQueue
produceCounter *atomic.Uint32
Expand Down
2 changes: 1 addition & 1 deletion exporter/exporterhelper/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestTracesExporter_WithPersistentQueue(t *testing.T) {
require.NoError(t, err)

host := &mockHost{ext: map[component.ID]component.Component{
storageID: internal.NewMockStorageExtension(),
storageID: internal.NewMockStorageExtension(nil),
}}
require.NoError(t, te.Start(context.Background(), host))
t.Cleanup(func() { require.NoError(t, te.Shutdown(context.Background())) })
Expand Down

0 comments on commit 744eb93

Please sign in to comment.