Skip to content

Commit acbaca3

Browse files
committed
test: add test missing from previous commit
1 parent ad77a80 commit acbaca3

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

go/discovery_kit_sdk/caching_discovery_target_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// SPDX-License-Identifier: MIT
2-
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
1+
// Copyright 2025 steadybit GmbH. All rights reserved.
32

43
package discovery_kit_sdk
54

65
import (
76
"context"
87
"errors"
8+
"fmt"
9+
"github.com/rs/zerolog/log"
910
"github.com/steadybit/discovery-kit/go/discovery_kit_api"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/mock"
@@ -259,3 +260,45 @@ func Test_target_string_interning(t *testing.T) {
259260
}
260261
}
261262
}
263+
264+
func Test_target_string_interning_concurrent_modification(t *testing.T) {
265+
ctx := context.Background()
266+
267+
discovery := newMockTargetDiscovery()
268+
cached := NewCachedTargetDiscovery(discovery)
269+
270+
targets := make([]discovery_kit_api.Target, 1000)
271+
for i := range targets {
272+
targets[i] = discovery_kit_api.Target{
273+
Id: fmt.Sprintf("target-%d", i),
274+
TargetType: "example",
275+
Label: "Example Target",
276+
Attributes: map[string][]string{
277+
"example": {"yes"},
278+
"id": {fmt.Sprintf("target-%d", i)},
279+
},
280+
}
281+
}
282+
283+
discovery.On("DiscoverTargets", mock.Anything).Unset()
284+
discovery.On("DiscoverTargets", ctx).Return(targets, nil)
285+
286+
go func() {
287+
defer func() {
288+
if err := recover(); err != nil {
289+
log.Error().Any("err", err).Msgf("recovered")
290+
}
291+
}()
292+
293+
for {
294+
for i := range targets {
295+
targets[i].Attributes["loop"] = []string{fmt.Sprintf("loop-%d", i)}
296+
}
297+
}
298+
}()
299+
300+
assert.NotPanics(t, func() {
301+
cached.Refresh(ctx)
302+
_, _ = cached.DiscoverTargets(ctx)
303+
})
304+
}

0 commit comments

Comments
 (0)