github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/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/testify/assert"
    12  	"github.com/instana/testify/require"
    13  	"github.com/mier85/go-sensor/autoprofile/internal"
    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  }