...

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

Documentation: github.com/tklauser/numcpus

     1  // Copyright 2019-2020 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_test
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"os/exec"
    21  	"runtime"
    22  	"strconv"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/tklauser/numcpus"
    27  )
    28  
    29  func testGetconf(t *testing.T, got int, name, getconfWhich string) {
    30  	t.Helper()
    31  
    32  	getconf, err := exec.LookPath("getconf")
    33  	if err != nil {
    34  		t.Skipf("getconf not found in PATH: %v", err)
    35  	}
    36  
    37  	cmd := exec.Command(getconf, getconfWhich)
    38  	var outb, errb bytes.Buffer
    39  	cmd.Stdout = &outb
    40  	cmd.Stderr = &errb
    41  	if err := cmd.Run(); err != nil {
    42  		t.Skipf("failed to run %v: %v (%v)", cmd, err, strings.TrimSpace(errb.String()))
    43  	}
    44  
    45  	want, err := strconv.ParseInt(strings.TrimSpace(outb.String()), 10, 64)
    46  	if err != nil {
    47  		t.Skipf("failed to parse getconf output: %v", err)
    48  	}
    49  
    50  	if got != int(want) {
    51  		t.Errorf("%s returned %v, want %v (getconf %s)", name, got, want, getconfWhich)
    52  	}
    53  }
    54  
    55  func confName(name string) string {
    56  	switch runtime.GOOS {
    57  	case "illumos", "netbsd", "openbsd", "solaris":
    58  		return strings.TrimPrefix(name, "_")
    59  	}
    60  	return name
    61  }
    62  
    63  func TestGetConfigured(t *testing.T) {
    64  	n, err := numcpus.GetConfigured()
    65  	if errors.Is(err, numcpus.ErrNotSupported) {
    66  		t.Skipf("GetConfigured not supported on %s", runtime.GOOS)
    67  	} else if err != nil {
    68  		t.Fatalf("GetConfigured: %v", err)
    69  	}
    70  	t.Logf("Configured = %v", n)
    71  
    72  	testGetconf(t, n, "GetConfigured", confName("_NPROCESSORS_CONF"))
    73  }
    74  
    75  func TestGetKernelMax(t *testing.T) {
    76  	n, err := numcpus.GetKernelMax()
    77  	if errors.Is(err, numcpus.ErrNotSupported) {
    78  		t.Skipf("GetKernelMax not supported on %s", runtime.GOOS)
    79  	} else if err != nil {
    80  		t.Fatalf("GetKernelMax: %v", err)
    81  	}
    82  	t.Logf("KernelMax = %v", n)
    83  }
    84  
    85  func TestGetOffline(t *testing.T) {
    86  	n, err := numcpus.GetOffline()
    87  	if errors.Is(err, numcpus.ErrNotSupported) {
    88  		t.Skipf("GetOffline not supported on %s", runtime.GOOS)
    89  	} else if err != nil {
    90  		t.Fatalf("GetOffline: %v", err)
    91  	}
    92  	t.Logf("Offline = %v", n)
    93  }
    94  
    95  func TestGetOnline(t *testing.T) {
    96  	n, err := numcpus.GetOnline()
    97  	if errors.Is(err, numcpus.ErrNotSupported) {
    98  		t.Skipf("GetOnline not supported on %s", runtime.GOOS)
    99  	} else if err != nil {
   100  		t.Fatalf("GetOnline: %v", err)
   101  	}
   102  	t.Logf("Online = %v", n)
   103  
   104  	testGetconf(t, n, "GetOnline", confName("_NPROCESSORS_ONLN"))
   105  }
   106  
   107  func TestGetPossible(t *testing.T) {
   108  	n, err := numcpus.GetPossible()
   109  	if errors.Is(err, numcpus.ErrNotSupported) {
   110  		t.Skipf("GetPossible not supported on %s", runtime.GOOS)
   111  	} else if err != nil {
   112  		t.Fatalf("GetPossible: %v", err)
   113  	}
   114  	t.Logf("Possible = %v", n)
   115  }
   116  
   117  func TestGetPresent(t *testing.T) {
   118  	n, err := numcpus.GetPresent()
   119  	if errors.Is(err, numcpus.ErrNotSupported) {
   120  		t.Skipf("GetPresent not supported on %s", runtime.GOOS)
   121  	} else if err != nil {
   122  		t.Fatalf("GetPresent: %v", err)
   123  	}
   124  	t.Logf("Present = %v", n)
   125  }
   126  

View as plain text