...

Source file src/github.com/tklauser/numcpus/numcpus_linux_test.go

Documentation: github.com/tklauser/numcpus

     1  // Copyright 2018 Tobias Klauser
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package numcpus
    16  
    17  import "testing"
    18  
    19  func TestParseCPURange(t *testing.T) {
    20  	testCases := []struct {
    21  		cpus string
    22  		n    int
    23  	}{
    24  		{"", 0},
    25  		{"0", 1},
    26  		{"0-1", 2},
    27  		{"0-7", 8},
    28  		{"1-7", 7},
    29  		{"1-15", 15},
    30  		{"0-3,7", 5},
    31  		{"0,2-4", 4},
    32  		{"0,2-4,7", 5},
    33  		{"0,2-4,7-15", 13},
    34  	}
    35  
    36  	for _, tc := range testCases {
    37  		n, err := parseCPURange(tc.cpus)
    38  		if err != nil {
    39  			t.Errorf("failed to parse CPU range: %v", err)
    40  		}
    41  
    42  		if n != tc.n {
    43  			t.Errorf("parseCPURange(%q) = %d, expected %d", tc.cpus, n, tc.n)
    44  		}
    45  	}
    46  
    47  	str := "invalid"
    48  	_, err := parseCPURange(str)
    49  	if err == nil {
    50  		t.Errorf("parseCPURange(%q) unexpectedly succeeded", str)
    51  	}
    52  }
    53  
    54  func TestGetFromCPUAffinity(t *testing.T) {
    55  	nAffinity, err := getFromCPUAffinity()
    56  	if err != nil {
    57  		t.Fatalf("getFromCPUAffinity: %v", err)
    58  	}
    59  
    60  	cpus := "online"
    61  	nSysfs, err := readCPURange(cpus)
    62  	if err != nil {
    63  		t.Fatalf("readCPURange(%q): %v", cpus, err)
    64  	}
    65  
    66  	if nAffinity != nSysfs {
    67  		t.Errorf("getFromCPUAffinity() = %d, readCPURange(%q) = %d, want the same return value", nAffinity, cpus, nSysfs)
    68  	}
    69  }
    70  

View as plain text