Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions internal/pkg/mock/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package mock

import (
"slices"
"sync"

"github.com/kubevirt/device-plugin-manager/pkg/dpm"
Expand All @@ -28,7 +29,10 @@ type MockLister struct {
Namespace string
counts map[string]int
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 {
Expand Down Expand Up @@ -80,31 +84,34 @@ 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)
l.publishMutex.Lock()
defer l.publishMutex.Unlock()

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.mutex.Lock()
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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
136 changes: 136 additions & 0 deletions internal/pkg/mock/lister_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
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"
"time"

"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 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 {
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:
}
}

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)
}