github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/acceptor/process_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package acceptor_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/instana/go-sensor/acceptor"
    10  	"github.com/instana/go-sensor/process"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestNewProcessPluginPayload(t *testing.T) {
    15  	data := acceptor.ProcessData{
    16  		PID: 42,
    17  	}
    18  
    19  	assert.Equal(t, acceptor.PluginPayload{
    20  		Name:     "com.instana.plugin.process",
    21  		EntityID: "id1",
    22  		Data:     data,
    23  	}, acceptor.NewProcessPluginPayload("id1", data))
    24  }
    25  
    26  func TestNewProcessCPUStatsDelta(t *testing.T) {
    27  	stats := process.CPUStats{
    28  		User:   1,
    29  		System: 10,
    30  	}
    31  
    32  	t.Run("equal stats", func(t *testing.T) {
    33  		assert.Nil(t, acceptor.NewProcessCPUStatsDelta(stats, stats, 2))
    34  	})
    35  
    36  	t.Run("same time", func(t *testing.T) {
    37  		assert.Nil(t, acceptor.NewProcessCPUStatsDelta(process.CPUStats{}, stats, 0))
    38  	})
    39  
    40  	t.Run("increased insignificantly", func(t *testing.T) {
    41  		assert.Nil(t, acceptor.NewProcessCPUStatsDelta(process.CPUStats{}, stats, 10000))
    42  	})
    43  
    44  	t.Run("decreased both", func(t *testing.T) {
    45  		assert.Nil(t, acceptor.NewProcessCPUStatsDelta(stats, process.CPUStats{}, 2))
    46  	})
    47  
    48  	t.Run("increased", func(t *testing.T) {
    49  		assert.Equal(t,
    50  			&acceptor.ProcessCPUStatsDelta{
    51  				User:   0.5,
    52  				System: 5,
    53  			},
    54  			acceptor.NewProcessCPUStatsDelta(process.CPUStats{}, stats, 2),
    55  		)
    56  	})
    57  
    58  	t.Run("one has increased insignificantly", func(t *testing.T) {
    59  		assert.Equal(t,
    60  			&acceptor.ProcessCPUStatsDelta{
    61  				System: 0.01,
    62  			},
    63  			acceptor.NewProcessCPUStatsDelta(process.CPUStats{}, stats, 1000),
    64  		)
    65  	})
    66  
    67  	t.Run("one has decreased", func(t *testing.T) {
    68  		assert.Equal(t,
    69  			&acceptor.ProcessCPUStatsDelta{
    70  				User: 0.5,
    71  			},
    72  			acceptor.NewProcessCPUStatsDelta(stats, process.CPUStats{
    73  				User:   stats.User * 2,
    74  				System: stats.System / 2,
    75  			}, 2),
    76  		)
    77  	})
    78  }
    79  
    80  func TestNewProcessMemoryStatsUpdate(t *testing.T) {
    81  	stats := process.MemStats{
    82  		Total:  1,
    83  		Rss:    10,
    84  		Shared: 100,
    85  	}
    86  
    87  	t.Run("equal", func(t *testing.T) {
    88  		assert.Nil(t, acceptor.NewProcessMemoryStatsUpdate(stats, stats))
    89  	})
    90  
    91  	t.Run("changed", func(t *testing.T) {
    92  		assert.Equal(t,
    93  			&acceptor.ProcessMemoryStatsUpdate{
    94  				Total:  &stats.Total,
    95  				Rss:    &stats.Rss,
    96  				Shared: &stats.Shared,
    97  			},
    98  			acceptor.NewProcessMemoryStatsUpdate(process.MemStats{}, stats),
    99  		)
   100  	})
   101  
   102  	t.Run("changed some", func(t *testing.T) {
   103  		assert.Equal(t,
   104  			&acceptor.ProcessMemoryStatsUpdate{
   105  				Total:  &stats.Total,
   106  				Shared: &stats.Shared,
   107  			},
   108  			acceptor.NewProcessMemoryStatsUpdate(process.MemStats{
   109  				Total:  stats.Total * 2,
   110  				Rss:    stats.Rss,
   111  				Shared: stats.Shared * 2,
   112  			}, stats),
   113  		)
   114  	})
   115  }
   116  
   117  func TestNewProcessOpenFilesStatsUpdate(t *testing.T) {
   118  	stats := process.ResourceLimits{
   119  		OpenFiles: process.LimitedResource{
   120  			Current: 1,
   121  			Max:     10,
   122  		},
   123  	}
   124  
   125  	t.Run("equal", func(t *testing.T) {
   126  		assert.Nil(t, acceptor.NewProcessOpenFilesStatsUpdate(stats, stats))
   127  	})
   128  
   129  	t.Run("changed", func(t *testing.T) {
   130  		assert.Equal(t,
   131  			&acceptor.ProcessOpenFilesStatsUpdate{
   132  				Current: &stats.OpenFiles.Current,
   133  				Max:     &stats.OpenFiles.Max,
   134  			},
   135  			acceptor.NewProcessOpenFilesStatsUpdate(process.ResourceLimits{}, stats),
   136  		)
   137  	})
   138  
   139  	t.Run("changed some", func(t *testing.T) {
   140  		assert.Equal(t,
   141  			&acceptor.ProcessOpenFilesStatsUpdate{
   142  				Current: &stats.OpenFiles.Current,
   143  			},
   144  			acceptor.NewProcessOpenFilesStatsUpdate(process.ResourceLimits{
   145  				OpenFiles: process.LimitedResource{
   146  					Current: stats.OpenFiles.Current * 2,
   147  					Max:     stats.OpenFiles.Max,
   148  				},
   149  			}, stats),
   150  		)
   151  	})
   152  }