github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/snapshot_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana_test
     5  
     6  import (
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/instana/testify/assert"
    12  	instana "github.com/mier85/go-sensor"
    13  	"github.com/mier85/go-sensor/acceptor"
    14  )
    15  
    16  func TestSnapshotCollector_Collect(t *testing.T) {
    17  	sc := instana.SnapshotCollector{
    18  		ServiceName:        "test",
    19  		CollectionInterval: 500 * time.Millisecond,
    20  	}
    21  
    22  	assert.Equal(t, &acceptor.RuntimeInfo{
    23  		Name:          sc.ServiceName,
    24  		Version:       runtime.Version(),
    25  		Root:          runtime.GOROOT(),
    26  		MaxProcs:      runtime.GOMAXPROCS(0),
    27  		Compiler:      runtime.Compiler,
    28  		NumCPU:        runtime.NumCPU(),
    29  		SensorVersion: instana.Version,
    30  	}, sc.Collect())
    31  
    32  	t.Run("second call before collection interval", func(t *testing.T) {
    33  		assert.Nil(t, sc.Collect())
    34  	})
    35  
    36  	t.Run("second call after collection interval", func(t *testing.T) {
    37  		oldNumProcs := runtime.GOMAXPROCS(0)
    38  		defer runtime.GOMAXPROCS(oldNumProcs)
    39  
    40  		time.Sleep(sc.CollectionInterval)
    41  		runtime.GOMAXPROCS(oldNumProcs + 1)
    42  
    43  		assert.Equal(t, &acceptor.RuntimeInfo{
    44  			Name:          sc.ServiceName,
    45  			Version:       runtime.Version(),
    46  			Root:          runtime.GOROOT(),
    47  			MaxProcs:      oldNumProcs + 1,
    48  			Compiler:      runtime.Compiler,
    49  			NumCPU:        runtime.NumCPU(),
    50  			SensorVersion: instana.Version,
    51  		}, sc.Collect())
    52  	})
    53  }