forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persister_test.go
102 lines (88 loc) · 2.53 KB
/
persister_test.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver"
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/Azure/azure-event-hubs-go/v3/persist"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/extension/experimental/storage"
)
func TestStorageOffsetPersisterUnknownCheckpoint(t *testing.T) {
client := newMockClient()
s := storageCheckpointPersister{storageClient: client}
// check we have no match
checkpoint, err := s.Read("foo", "bar", "foobar", "foobarfoo")
assert.NoError(t, err)
assert.NotNil(t, checkpoint)
assert.Equal(t, "-1", checkpoint.Offset)
}
func TestStorageOffsetPersisterWithKnownCheckpoint(t *testing.T) {
client := newMockClient()
s := storageCheckpointPersister{storageClient: client}
checkpoint := persist.Checkpoint{
Offset: "foo",
SequenceNumber: 2,
EnqueueTime: time.Now(),
}
err := s.Write("foo", "bar", "foobar", "foobarfoo", checkpoint)
assert.NoError(t, err)
read, err := s.Read("foo", "bar", "foobar", "foobarfoo")
assert.NoError(t, err)
assert.Equal(t, checkpoint.Offset, read.Offset)
assert.Equal(t, checkpoint.SequenceNumber, read.SequenceNumber)
assert.True(t, checkpoint.EnqueueTime.Equal(read.EnqueueTime))
}
// copied from pkg/stanza/adapter/mocks_test.go
type mockClient struct {
cache map[string][]byte
cacheMux sync.Mutex
}
func newMockClient() *mockClient {
return &mockClient{
cache: make(map[string][]byte),
}
}
func (p *mockClient) Get(_ context.Context, key string) ([]byte, error) {
p.cacheMux.Lock()
defer p.cacheMux.Unlock()
return p.cache[key], nil
}
func (p *mockClient) Set(_ context.Context, key string, value []byte) error {
p.cacheMux.Lock()
defer p.cacheMux.Unlock()
p.cache[key] = value
return nil
}
func (p *mockClient) Delete(_ context.Context, key string) error {
p.cacheMux.Lock()
defer p.cacheMux.Unlock()
delete(p.cache, key)
return nil
}
func (p *mockClient) Batch(_ context.Context, ops ...storage.Operation) error {
p.cacheMux.Lock()
defer p.cacheMux.Unlock()
for _, op := range ops {
switch op.Type {
case storage.Get:
op.Value = p.cache[op.Key]
case storage.Set:
p.cache[op.Key] = op.Value
case storage.Delete:
delete(p.cache, op.Key)
default:
return errors.New("wrong operation type")
}
}
return nil
}
func (p *mockClient) Close(_ context.Context) error {
p.cacheMux.Lock()
defer p.cacheMux.Unlock()
p.cache = nil
return nil
}