Skip to content

Commit 54bb6cd

Browse files
committed
linear: fix test cases
Signed-off-by: Fu-Sheng <[email protected]>
1 parent 342cb6f commit 54bb6cd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pkg/cache/v3/linear_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ func TestLinearMixedWatches(t *testing.T) {
742742
assert.Equal(t, 2, c.NumResources())
743743

744744
sotwState := stream.NewStreamState(false, nil)
745+
sotwState.SetKnownResourceNamesAsList(testType, []string{"a", "b"})
745746
w := make(chan Response, 1)
746747
c.CreateWatch(&Request{ResourceNames: []string{"a", "b"}, TypeUrl: testType, VersionInfo: c.getVersion()}, sotwState, w)
747748
mustBlock(t, w)
@@ -754,7 +755,7 @@ func TestLinearMixedWatches(t *testing.T) {
754755
err = c.UpdateResources(map[string]types.Resource{"a": a}, nil)
755756
assert.NoError(t, err)
756757
// This behavior is currently invalid for cds and lds, but due to a current limitation of linear cache sotw implementation
757-
verifyResponse(t, w, c.getVersion(), 1)
758+
verifyResponse(t, w, c.getVersion(), 2)
758759
checkVersionMapNotSet(t, c)
759760

760761
c.CreateWatch(&Request{ResourceNames: []string{"a", "b"}, TypeUrl: testType, VersionInfo: c.getVersion()}, sotwState, w)
@@ -775,6 +776,6 @@ func TestLinearMixedWatches(t *testing.T) {
775776
assert.NoError(t, err)
776777
checkVersionMapSet(t, c)
777778

778-
verifyResponse(t, w, c.getVersion(), 0)
779+
verifyResponse(t, w, c.getVersion(), 1)
779780
verifyDeltaResponse(t, wd, nil, []string{"b"})
780781
}

pkg/server/stream/v3/stream.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type StreamState struct { // nolint:golint,revive
4141

4242
// indicates whether the object has been modified since its creation
4343
first bool
44+
45+
mu *sync.RWMutex
4446
}
4547

4648
// GetSubscribedResourceNames returns the list of resources currently explicitly subscribed to
@@ -95,10 +97,16 @@ func (s *StreamState) IsWildcard() bool {
9597
}
9698

9799
func (s *StreamState) SetKnownResourceNames(url string, names map[string]struct{}) {
100+
s.mu.Lock()
101+
defer s.mu.Unlock()
102+
98103
s.knownResourceNames[url] = names
99104
}
100105

101106
func (s *StreamState) SetKnownResourceNamesAsList(url string, names []string) {
107+
s.mu.Lock()
108+
defer s.mu.Unlock()
109+
102110
m := map[string]struct{}{}
103111
for _, name := range names {
104112
m[name] = struct{}{}
@@ -107,6 +115,9 @@ func (s *StreamState) SetKnownResourceNamesAsList(url string, names []string) {
107115
}
108116

109117
func (s *StreamState) GetKnownResourceNames(url string) map[string]struct{} {
118+
s.mu.Lock()
119+
defer s.mu.Unlock()
120+
110121
return s.knownResourceNames[url]
111122
}
112123

@@ -118,6 +129,7 @@ func NewStreamState(wildcard bool, initialResourceVersions map[string]string) St
118129
resourceVersions: initialResourceVersions,
119130
first: true,
120131
knownResourceNames: map[string]map[string]struct{}{},
132+
mu: &sync.RWMutex{},
121133
}
122134

123135
if initialResourceVersions == nil {

0 commit comments

Comments
 (0)