github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/autoprofile/internal/block_sampler_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package internal_test
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/instana/go-sensor/autoprofile/internal"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestCreateBlockProfile(t *testing.T) {
    17  	blockSampler := internal.NewBlockSampler()
    18  	internal.IncludeProfilerFrames = true
    19  
    20  	blockSampler.Reset()
    21  	blockSampler.Start()
    22  
    23  	simulateBlocking(150 * time.Millisecond)
    24  
    25  	blockSampler.Stop()
    26  
    27  	profile, err := blockSampler.Profile(500*1e6, 120)
    28  	require.NoError(t, err)
    29  
    30  	assert.Contains(t, fmt.Sprintf("%v", internal.NewAgentProfile(profile)), "simulateBlocking")
    31  }
    32  
    33  func simulateBlocking(d time.Duration) {
    34  	wait := make(chan struct{})
    35  
    36  	time.AfterFunc(d, func() { wait <- struct{}{} })
    37  	<-wait
    38  }