Skip to content

Commit 47e3c91

Browse files
committed
change package name
1 parent ab5f217 commit 47e3c91

File tree

1 file changed

+18
-97
lines changed

1 file changed

+18
-97
lines changed

pkg/stanza/fileconsumer/internal/archive/archive_test.go

Lines changed: 18 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package archive // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive"
4+
package archive_test // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive_test"
55

66
import (
77
"context"
88
"fmt"
99
"math/rand/v2"
1010
"testing"
1111

12-
"github.com/google/uuid"
1312
"github.com/stretchr/testify/require"
1413
"go.uber.org/zap"
1514

16-
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/checkpoint"
15+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/archive"
1716
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fileset"
1817
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
1918
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader"
20-
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
2119
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
2220
)
2321

22+
func archiveKey(i int) string {
23+
return fmt.Sprintf("knownFiles%d", i)
24+
}
25+
2426
func TestArchiveCRUD(t *testing.T) {
2527
// pollsToArchiveMatrix contains different polls_to_archive settings to test
2628
pollsToArchiveMatrix := []int{10, 20, 50, 100, 200}
@@ -36,10 +38,7 @@ func TestArchiveCRUD(t *testing.T) {
3638

3739
func testArchive(t *testing.T, pollsToArchive int) {
3840
persister := testutil.NewUnscopedMockPersister()
39-
a := New(context.Background(), zap.L(), pollsToArchive, persister)
40-
archive, isArchive := a.(*archive)
41-
require.True(t, isArchive, "expected archive")
42-
require.Equal(t, 0, archive.archiveIndex, "expected archiveIndex to be 0 at the beginning")
41+
a := archive.New(context.Background(), zap.L(), pollsToArchive, persister)
4342

4443
m := make(map[int]*fingerprint.Fingerprint)
4544

@@ -60,36 +59,31 @@ func testArchive(t *testing.T, pollsToArchive int) {
6059
set := fileset.New[*reader.Metadata](0)
6160
set.Add(&reader.Metadata{Fingerprint: fp})
6261

63-
archive.WriteFiles(context.Background(), set)
62+
a.WriteFiles(context.Background(), set)
6463
}
6564

6665
// sub-test 1: rolled over fingerprints should not be a part of archive
6766
for _, fp := range rolledOverFps {
68-
matchedData := archive.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
67+
matchedData := a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
6968
require.Nil(t, matchedData[0])
7069
}
7170

7271
// sub-test 2: newer fingerprints should be part of archive
73-
for index, fp := range m {
74-
oldMetadata, err := checkpoint.LoadKey(context.Background(), persister, archiveKey(index))
75-
require.NoErrorf(t, err, "expected no error while loading metadata, got %v", err)
76-
require.Lenf(t, oldMetadata, 1, "index %d should have exactly one item", index)
77-
72+
for _, fp := range m {
7873
// FindFiles removes the data from persister.
79-
matchedData := archive.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
74+
matchedData := a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
8075
require.NotNil(t, matchedData[0])
8176
require.True(t, fp.Equal(matchedData[0].GetFingerprint()), "expected fingerprints to match")
8277

8378
// archive should no longer contain data (as FindFiles removed the data)
84-
newMetadata, err := checkpoint.LoadKey(context.Background(), persister, archiveKey(index))
85-
require.NoErrorf(t, err, "expected no error while loading metadata, got %v", err)
86-
require.Emptyf(t, newMetadata, "index %d should have no items, found %d items", index, len(newMetadata))
79+
matchedData = a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
80+
require.Nil(t, matchedData[0])
8781
}
8882
}
8983

9084
func testArchiveNop(t *testing.T) {
9185
persister := testutil.NewUnscopedMockPersister()
92-
a := New(context.Background(), zap.L(), -1, persister)
86+
a := archive.New(context.Background(), zap.L(), -1, persister)
9387

9488
m := make(map[int]*fingerprint.Fingerprint)
9589

@@ -105,89 +99,16 @@ func testArchiveNop(t *testing.T) {
10599
}
106100

107101
// fingerprints should not be part of archive, as it's nop
108-
for index, fp := range m {
109-
oldMetadata, err := checkpoint.LoadKey(context.Background(), persister, archiveKey(index))
110-
require.NoErrorf(t, err, "expected no error while loading metadata, got %v", err)
111-
require.Emptyf(t, oldMetadata, "index %d should have no items, found %d items", index, len(oldMetadata))
102+
for _, fp := range m {
103+
matchedData := a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
104+
require.Nil(t, matchedData[0])
112105

113106
// FindFiles removes the data from persister.
114-
matchedData := a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
107+
matchedData = a.FindFiles(context.Background(), []*fingerprint.Fingerprint{fp})
115108
require.Nil(t, matchedData[0], "fingerprints shouldn't match for nopArchive")
116109
}
117110
}
118111

119-
func TestFindFilesOrder(t *testing.T) {
120-
fps := make([]*fingerprint.Fingerprint, 0)
121-
for i := 0; i < 100; i++ {
122-
fps = append(fps, fingerprint.New([]byte(uuid.NewString())))
123-
}
124-
persister := testutil.NewUnscopedMockPersister()
125-
fpInStorage := populatedPersisterData(persister, fps)
126-
127-
archive := New(context.Background(), zap.L(), 100, persister)
128-
matchables := archive.FindFiles(context.Background(), fps)
129-
130-
require.Len(t, matchables, len(fps), "return slice should be of same length as input slice")
131-
132-
for i := 0; i < len(matchables); i++ {
133-
if fpInStorage[i] {
134-
// if current fingerprint is present in storage, the corresponding return type should not be nil
135-
require.NotNilf(t, matchables[i], "resulting index %d should be not be nil type", i)
136-
require.Truef(t, fps[i].Equal(matchables[i].GetFingerprint()), "fingerprint at index %d is not equal to corresponding return value", i)
137-
} else {
138-
// if current fingerprint is absent from storage, the corresponding index should be empty i.e. "nil"
139-
require.Nil(t, matchables[i], "resulting index %d should be of nil type", i)
140-
}
141-
}
142-
}
143-
144-
func TestIndexInBounds(t *testing.T) {
145-
persister := testutil.NewUnscopedMockPersister()
146-
pollsToArchive := 100
147-
testArchive := New(context.Background(), zap.L(), 100, persister).(*archive)
148-
149-
// no index exists. archiveIndex should be 0
150-
require.Equal(t, 0, testArchive.archiveIndex)
151-
152-
// run archiving. Each time, index should be in bound.
153-
for i := 0; i < 1098; i++ {
154-
require.Equalf(t, i%pollsToArchive, testArchive.archiveIndex, "Index should %d, but was %d", i%pollsToArchive, testArchive.archiveIndex)
155-
testArchive.WriteFiles(context.Background(), &fileset.Fileset[*reader.Metadata]{})
156-
require.Truef(t, testArchive.archiveIndex >= 0 && testArchive.archiveIndex < pollsToArchive, "Index should be between 0 and %d, but was %d", pollsToArchive, testArchive.archiveIndex)
157-
}
158-
oldIndex := testArchive.archiveIndex
159-
160-
// re-create archive
161-
testArchive = New(context.Background(), zap.L(), 100, persister).(*archive)
162-
163-
// index should exist and new archiveIndex should be equal to oldIndex
164-
require.Equalf(t, oldIndex, testArchive.archiveIndex, "New index should %d, but was %d", oldIndex, testArchive.archiveIndex)
165-
166-
// re-create archive, with reduced pollsToArchive
167-
pollsToArchive = 70
168-
169-
testArchive = New(context.Background(), zap.L(), pollsToArchive, persister).(*archive)
170-
// index should exist but it is out of bounds. So it should reset to 0
171-
require.Equalf(t, 0, testArchive.archiveIndex, "Index should be reset to 0 but was %d", testArchive.archiveIndex)
172-
}
173-
174-
func populatedPersisterData(persister operator.Persister, fps []*fingerprint.Fingerprint) []bool {
175-
md := make([]*reader.Metadata, 0)
176-
177-
fpInStorage := make([]bool, len(fps))
178-
for i, fp := range fps {
179-
// 50-50 chance that a fingerprint exists in the storage
180-
if rand.Float32() < 0.5 {
181-
md = append(md, &reader.Metadata{Fingerprint: fp})
182-
fpInStorage[i] = true // mark the fingerprint at index i in storage
183-
}
184-
}
185-
// save half keys in knownFiles0 and other half in knownFiles1
186-
_ = checkpoint.SaveKey(context.Background(), persister, md[:len(md)/2], "knownFiles0")
187-
_ = checkpoint.SaveKey(context.Background(), persister, md[len(md)/2:], "knownFiles1")
188-
return fpInStorage
189-
}
190-
191112
func createRandomString(length int) string {
192113
// some characters for the random generation
193114
const letterBytes = " ,.;:*-+/[]{}<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

0 commit comments

Comments
 (0)