volcano.sh/volcano@v1.9.0/pkg/scheduler/api/shared_device_pool_test.go (about)

     1  package api
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_ignoredDevicesList_Set_BasicUsage(t *testing.T) {
    11  	tests := []struct {
    12  		name                   string
    13  		deviceLists            [][]string
    14  		expectedIgnoredDevices []string
    15  	}{
    16  		{
    17  			name:                   "set several values to ignoredDevicesList",
    18  			deviceLists:            [][]string{{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"}},
    19  			expectedIgnoredDevices: []string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"},
    20  		},
    21  		{
    22  			name:                   "set several lists of values to ignoredDevicesList atomically",
    23  			deviceLists:            [][]string{{"volcano.sh/vgpu-memory"}, {"volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"}},
    24  			expectedIgnoredDevices: []string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"},
    25  		},
    26  		{
    27  			name:                   "possible way to clear ignoredDevicesList",
    28  			deviceLists:            nil,
    29  			expectedIgnoredDevices: nil,
    30  		},
    31  	}
    32  	for _, tt := range tests {
    33  		t.Run(tt.name, func(t *testing.T) {
    34  			lst := ignoredDevicesList{}
    35  			lst.Set(tt.deviceLists...)
    36  			assert.Equal(t, tt.expectedIgnoredDevices, lst.ignoredDevices)
    37  		})
    38  	}
    39  }
    40  
    41  func Test_ignoredDevicesList_Range_BasicUsage(t *testing.T) {
    42  	lst := ignoredDevicesList{}
    43  	lst.Set([]string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"})
    44  
    45  	t.Run("read and copy values from the ignoredDevicesList", func(t *testing.T) {
    46  		ignoredDevices := make([]string, 0, len(lst.ignoredDevices))
    47  		lst.Range(func(_ int, device string) bool {
    48  			ignoredDevices = append(ignoredDevices, device)
    49  			return true
    50  		})
    51  		assert.Equal(t, lst.ignoredDevices, ignoredDevices)
    52  	})
    53  
    54  	t.Run("break iteration through the ignoredDevicesList", func(t *testing.T) {
    55  		i := 0
    56  		flag := false
    57  		lst.Range(func(_ int, device string) bool {
    58  			i++
    59  			if lst.ignoredDevices[1] == device {
    60  				flag = true
    61  				return false
    62  			}
    63  			return true
    64  		})
    65  
    66  		assert.Equal(t, true, flag)
    67  		assert.Equal(t, 2, i)
    68  	})
    69  }
    70  
    71  func Test_ignoredDevicesList_Set_Concurrent(t *testing.T) {
    72  	lst := ignoredDevicesList{}
    73  	expected := []string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"}
    74  
    75  	var wg sync.WaitGroup
    76  	wg.Add(8)
    77  	for i := 0; i < 8; i++ {
    78  		go func() {
    79  			defer wg.Done()
    80  			lst.Set(expected)
    81  		}()
    82  	}
    83  	wg.Wait()
    84  
    85  	assert.Equal(t, expected, lst.ignoredDevices)
    86  }
    87  
    88  func Test_ignoredDevicesList_Range_Concurrent(t *testing.T) {
    89  	lst := ignoredDevicesList{}
    90  	lst.Set([]string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"})
    91  
    92  	var wg sync.WaitGroup
    93  	wg.Add(8)
    94  	for i := 0; i < 8; i++ {
    95  		go func() {
    96  			defer wg.Done()
    97  			ignoredDevices := make([]string, 0, len(lst.ignoredDevices))
    98  			lst.Range(func(_ int, device string) bool {
    99  				ignoredDevices = append(ignoredDevices, device)
   100  				return true
   101  			})
   102  			assert.Equal(t, ignoredDevices, lst.ignoredDevices)
   103  		}()
   104  	}
   105  	wg.Wait()
   106  }
   107  
   108  func Test_ignoredDevicesList_NoRace(t *testing.T) {
   109  	lst := ignoredDevicesList{}
   110  
   111  	var wg sync.WaitGroup
   112  	wg.Add(16)
   113  	for i := 0; i < 8; i++ {
   114  		go func() {
   115  			defer wg.Done()
   116  			lst.Set([]string{"volcano.sh/vgpu-memory", "volcano.sh/vgpu-memory-percentage", "volcano.sh/vgpu-cores"})
   117  		}()
   118  		go func() {
   119  			defer wg.Done()
   120  			lst.Range(func(_ int, _ string) bool {
   121  				return true
   122  			})
   123  		}()
   124  	}
   125  	wg.Wait()
   126  }