github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/sysinfo/sysinfo_test.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/ActiveState/cli/internal/testhelpers/performance"
     8  	"github.com/patrickmn/go-cache"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // TestPerformance tests the speed at which critical sysinfo methods execute
    13  // These timing are critical because we query this information at each invocation of State Tool
    14  func TestPerformance(t *testing.T) {
    15  	oldCache := sysinfoCache
    16  	defer func() { sysinfoCache = oldCache }()
    17  	sysinfoCache = cache.New(0, 0)
    18  
    19  	t.Run("OS", func(t *testing.T) {
    20  		maxDuration := time.Microsecond
    21  		err := performance.TimeIt(t, func() { OS() }, 10, maxDuration)
    22  		assert.NoError(t, err)
    23  	})
    24  
    25  	t.Run("OSVersion", func(t *testing.T) {
    26  		maxDuration := 10 * time.Millisecond
    27  		err := performance.TimeIt(t, func() {
    28  			_, err := OSVersion()
    29  			assert.NoError(t, err)
    30  		}, 10, maxDuration)
    31  		assert.NoError(t, err)
    32  	})
    33  }
    34  
    35  func TestOSVersionInfoCached(t *testing.T) {
    36  	osVersionInfo1, _ := OSVersion()
    37  	osVersionInfo2, _ := OSVersion()
    38  	assert.True(t, osVersionInfo1 != nil, "OSVersion should not be nil")
    39  	assert.True(t, osVersionInfo1 == osVersionInfo2, "Pointers should be equal")
    40  }
    41  
    42  func TestLibcInfoCached(t *testing.T) {
    43  	libcInfo1, _ := Libc()
    44  	libcInfo2, _ := Libc()
    45  	assert.True(t, libcInfo1 != nil, "Libc should not be nil")
    46  	assert.True(t, libcInfo1 == libcInfo2, "Pointers should be equal")
    47  }
    48  
    49  func TestCompilersCached(t *testing.T) {
    50  	compilers1, _ := Compilers()
    51  	compilers2, _ := Compilers()
    52  	assert.True(t, compilers1 != nil, "Compilers should not be nil")
    53  	for i := range compilers1 {
    54  		assert.True(t, compilers1[i] == compilers2[i], "Pointers should be equal")
    55  	}
    56  }