From 36db634eceae8243be227e0728eb84c278151694 Mon Sep 17 00:00:00 2001 From: blackdragoon26 Date: Sat, 8 Aug 2026 17:40:28 +0530 Subject: [PATCH 1/2] fix(mock): rediscover resource name changes Signed-off-by: blackdragoon26 --- internal/pkg/mock/lister.go | 46 ++++++++-------- internal/pkg/mock/lister_test.go | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 internal/pkg/mock/lister_test.go diff --git a/internal/pkg/mock/lister.go b/internal/pkg/mock/lister.go index 3238624..1120cd4 100644 --- a/internal/pkg/mock/lister.go +++ b/internal/pkg/mock/lister.go @@ -14,6 +14,7 @@ limitations under the License. package mock import ( + "slices" "sync" "github.com/kubevirt/device-plugin-manager/pkg/dpm" @@ -28,6 +29,7 @@ type MockLister struct { Namespace string counts map[string]int pluginsMap map[string]*MockPlugin + resources dpm.PluginNameList mutex sync.Mutex } @@ -80,31 +82,31 @@ func (l *MockLister) NewPlugin(resourceLastName string) dpm.PluginInterface { } func (l *MockLister) SetResource(resourceMap map[string]int) { - if len(resourceMap) == 0 { - return - } l.mutex.Lock() - defer l.mutex.Unlock() - l.counts = resourceMap - pluginNums := len(l.pluginsMap) - - if pluginNums == 0 { - resourceNames := make([]string, 0, len(resourceMap)) - hasNoZeroValue := false - for name, val := range resourceMap { - resourceNames = append(resourceNames, name) - if val > 0 { - hasNoZeroValue = true - } + l.counts = make(map[string]int, len(resourceMap)) + resourceNames := make(dpm.PluginNameList, 0, len(resourceMap)) + hasPositiveValue := false + for resourceName, val := range resourceMap { + l.counts[resourceName] = val + resourceNames = append(resourceNames, resourceName) + if val > 0 { + hasPositiveValue = true } - if hasNoZeroValue { - l.ResUpdateChan <- resourceNames + if plugin, exists := l.pluginsMap[resourceName]; exists { + plugin.SetCount(val) } + } + slices.Sort(resourceNames) + + resourcesChanged := !slices.Equal(l.resources, resourceNames) + if resourcesChanged && (hasPositiveValue || len(l.resources) > 0) { + l.resources = slices.Clone(resourceNames) } else { - for resourceName, val := range resourceMap { - if plugin, exists := l.pluginsMap[resourceName]; exists { - plugin.SetCount(val) - } - } + resourcesChanged = false + } + l.mutex.Unlock() + + if resourcesChanged { + l.ResUpdateChan <- resourceNames } } diff --git a/internal/pkg/mock/lister_test.go b/internal/pkg/mock/lister_test.go new file mode 100644 index 0000000..b7082d2 --- /dev/null +++ b/internal/pkg/mock/lister_test.go @@ -0,0 +1,91 @@ +/* +Copyright 2026 The HAMi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mock + +import ( + "slices" + "testing" + + "github.com/kubevirt/device-plugin-manager/pkg/dpm" +) + +func TestSetResourceDiscoversResourceNameChanges(t *testing.T) { + lister := NewMockLister("example.com") + lister.ResUpdateChan = make(chan dpm.PluginNameList, 1) + + lister.SetResource(map[string]int{"memory": 4}) + assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{"memory"}) + + memoryPlugin := lister.NewPlugin("memory").(*MockPlugin) + lister.SetResource(map[string]int{"memory": 8, "core": 100}) + assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{"core", "memory"}) + if got := memoryPlugin.GetCount(); got != 8 { + t.Fatalf("memory plugin count = %d, want 8", got) + } + + corePlugin := lister.NewPlugin("core").(*MockPlugin) + if got := corePlugin.GetCount(); got != 100 { + t.Fatalf("core plugin count = %d, want 100", got) + } + + lister.SetResource(map[string]int{"memory": 16, "core": 200}) + assertNoResourceUpdate(t, lister.ResUpdateChan) + if got := memoryPlugin.GetCount(); got != 16 { + t.Fatalf("memory plugin count = %d, want 16", got) + } + if got := corePlugin.GetCount(); got != 200 { + t.Fatalf("core plugin count = %d, want 200", got) + } + + lister.SetResource(map[string]int{"memory": 16}) + assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{"memory"}) + + lister.SetResource(map[string]int{}) + assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{}) +} + +func TestSetResourceWaitsForPositiveInitialCount(t *testing.T) { + lister := NewMockLister("example.com") + lister.ResUpdateChan = make(chan dpm.PluginNameList, 1) + + lister.SetResource(map[string]int{"memory": 0, "core": 0}) + assertNoResourceUpdate(t, lister.ResUpdateChan) + + lister.SetResource(map[string]int{"memory": 4, "core": 0}) + assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{"core", "memory"}) +} + +func assertResourceUpdate(t *testing.T, updates <-chan dpm.PluginNameList, want dpm.PluginNameList) { + t.Helper() + select { + case got := <-updates: + if !slices.Equal(got, want) { + t.Fatalf("resource update = %v, want %v", got, want) + } + default: + t.Fatalf("resource update = none, want %v", want) + } +} + +func assertNoResourceUpdate(t *testing.T, updates <-chan dpm.PluginNameList) { + t.Helper() + select { + case got := <-updates: + t.Fatalf("unexpected resource update: %v", got) + default: + } +} From c5e7bb80ebfcb9ecf2b2830e123381f87e7d1c46 Mon Sep 17 00:00:00 2001 From: blackdragoon26 Date: Sat, 8 Aug 2026 17:56:42 +0530 Subject: [PATCH 2/2] fix(mock): serialize resource list publication Signed-off-by: blackdragoon26 --- internal/pkg/mock/lister.go | 5 ++++ internal/pkg/mock/lister_test.go | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/internal/pkg/mock/lister.go b/internal/pkg/mock/lister.go index 1120cd4..fd83c21 100644 --- a/internal/pkg/mock/lister.go +++ b/internal/pkg/mock/lister.go @@ -31,6 +31,8 @@ type MockLister struct { pluginsMap map[string]*MockPlugin resources dpm.PluginNameList mutex sync.Mutex + // publishMutex keeps resource state changes and discovery notifications ordered. + publishMutex sync.Mutex } func NewMockLister(namespace string) *MockLister { @@ -82,6 +84,9 @@ func (l *MockLister) NewPlugin(resourceLastName string) dpm.PluginInterface { } func (l *MockLister) SetResource(resourceMap map[string]int) { + l.publishMutex.Lock() + defer l.publishMutex.Unlock() + l.mutex.Lock() l.counts = make(map[string]int, len(resourceMap)) resourceNames := make(dpm.PluginNameList, 0, len(resourceMap)) diff --git a/internal/pkg/mock/lister_test.go b/internal/pkg/mock/lister_test.go index b7082d2..891d1f0 100644 --- a/internal/pkg/mock/lister_test.go +++ b/internal/pkg/mock/lister_test.go @@ -19,6 +19,7 @@ package mock import ( "slices" "testing" + "time" "github.com/kubevirt/device-plugin-manager/pkg/dpm" ) @@ -69,6 +70,23 @@ func TestSetResourceWaitsForPositiveInitialCount(t *testing.T) { assertResourceUpdate(t, lister.ResUpdateChan, dpm.PluginNameList{"core", "memory"}) } +func TestSetResourceSerializesConcurrentUpdates(t *testing.T) { + lister := NewMockLister("example.com") + + go func() { + lister.SetResource(map[string]int{"memory": 4}) + }() + waitForResources(t, lister, dpm.PluginNameList{"memory"}) + + go func() { + lister.SetResource(map[string]int{"core": 100}) + }() + + assertResourceUpdateEventually(t, lister.ResUpdateChan, dpm.PluginNameList{"memory"}) + assertResourceUpdateEventually(t, lister.ResUpdateChan, dpm.PluginNameList{"core"}) + waitForResources(t, lister, dpm.PluginNameList{"core"}) +} + func assertResourceUpdate(t *testing.T, updates <-chan dpm.PluginNameList, want dpm.PluginNameList) { t.Helper() select { @@ -89,3 +107,30 @@ func assertNoResourceUpdate(t *testing.T, updates <-chan dpm.PluginNameList) { default: } } + +func assertResourceUpdateEventually(t *testing.T, updates <-chan dpm.PluginNameList, want dpm.PluginNameList) { + t.Helper() + select { + case got := <-updates: + if !slices.Equal(got, want) { + t.Fatalf("resource update = %v, want %v", got, want) + } + case <-time.After(time.Second): + t.Fatalf("timed out waiting for resource update %v", want) + } +} + +func waitForResources(t *testing.T, lister *MockLister, want dpm.PluginNameList) { + t.Helper() + deadline := time.Now().Add(time.Second) + for time.Now().Before(deadline) { + lister.mutex.Lock() + matched := slices.Equal(lister.resources, want) + lister.mutex.Unlock() + if matched { + return + } + time.Sleep(time.Millisecond) + } + t.Fatalf("timed out waiting for committed resources %v", want) +}