-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
mock_storage.go
85 lines (70 loc) · 1.85 KB
/
mock_storage.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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internal // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal"
import (
"context"
"errors"
"sync"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension/experimental/storage"
)
type mockStorageExtension struct {
component.StartFunc
component.ShutdownFunc
st sync.Map
getClientError error
}
func (m *mockStorageExtension) GetClient(_ context.Context, _ component.Kind, _ component.ID, _ string) (storage.Client, error) {
if m.getClientError != nil {
return nil, m.getClientError
}
return &mockStorageClient{st: &m.st}, nil
}
func NewMockStorageExtension(getClientError error) storage.Extension {
return &mockStorageExtension{getClientError: getClientError}
}
type mockStorageClient struct {
st *sync.Map
closeCounter uint64
}
func (m *mockStorageClient) Get(_ context.Context, s string) ([]byte, error) {
val, found := m.st.Load(s)
if !found {
return nil, nil
}
return val.([]byte), nil
}
func (m *mockStorageClient) Set(_ context.Context, s string, bytes []byte) error {
m.st.Store(s, bytes)
return nil
}
func (m *mockStorageClient) Delete(_ context.Context, s string) error {
m.st.Delete(s)
return nil
}
func (m *mockStorageClient) Close(_ context.Context) error {
m.closeCounter++
return nil
}
func (m *mockStorageClient) Batch(_ context.Context, ops ...storage.Operation) error {
for _, op := range ops {
switch op.Type {
case storage.Get:
val, found := m.st.Load(op.Key)
if !found {
break
}
op.Value = val.([]byte)
case storage.Set:
m.st.Store(op.Key, op.Value)
case storage.Delete:
m.st.Delete(op.Key)
default:
return errors.New("wrong operation type")
}
}
return nil
}
func (m *mockStorageClient) getCloseCount() uint64 {
return m.closeCounter
}