github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-unicorn/lib/unicorn_worker_test.go (about)

     1  package mpunicorn
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type TestFetchUnicornWorkerPidsCommand struct{}
     9  
    10  func (r TestFetchUnicornWorkerPidsCommand) Output(command string, args ...string) ([]byte, error) {
    11  	out := ` PID TTY      STAT   TIME COMMAND
    12    584 ?        Sl     0:27 unicorn worker[7] -c config/unicorn.rb -E deployment
    13   1857 ?        Sl     0:24 unicorn worker[6] -c config/unicorn.rb -E deployment
    14   2258 ?        Sl     0:19 unicorn worker[4] -c config/unicorn.rb -E deployment
    15   2627 ?        Sl     0:21 unicorn worker[2] -c config/unicorn.rb -E deployment
    16   2872 ?        Sl     0:22 unicorn worker[5] -c config/unicorn.rb -E deployment
    17   3085 ?        Sl     0:21 unicorn worker[3] -c config/unicorn.rb -E deployment
    18   3546 ?        Sl     0:17 unicorn worker[10] -c config/unicorn.rb -E deployment
    19   4392 ?        Sl     0:16 unicorn worker[8] -c config/unicorn.rb -E deployment
    20   6049 ?        Sl     0:12 unicorn worker[14] -c config/unicorn.rb -E deployment
    21   8430 ?        Sl     0:13 unicorn worker[0] -c config/unicorn.rb -E deployment
    22   8744 ?        Sl     0:10 unicorn worker[13] -c config/unicorn.rb -E deployment
    23   9293 ?        Sl     0:11 unicorn worker[1] -c config/unicorn.rb -E deployment
    24  10425 ?        Sl     0:08 unicorn worker[11] -c config/unicorn.rb -E deployment
    25  11152 ?        Sl     0:05 unicorn worker[9] -c config/unicorn.rb -E deployment
    26  11576 ?        Sl     0:05 unicorn worker[15] -c config/unicorn.rb -E deployment
    27  11685 ?        Sl     0:04 unicorn worker[12] -c config/unicorn.rb -E deployment`
    28  	return []byte(out), nil
    29  }
    30  
    31  func TestFetchUnicornWorkerPids(t *testing.T) {
    32  	command = TestFetchUnicornWorkerPidsCommand{}
    33  	masterPid := "30661"
    34  	expectedPids := []string{"584", "1857", "2258", "2627", "2872", "3085", "3546",
    35  		"4392", "6049", "8430", "8744", "9293", "10425", "11152", "11576", "11685"}
    36  	pids, _ := fetchUnicornWorkerPids(masterPid)
    37  	if !reflect.DeepEqual(pids, expectedPids) {
    38  		t.Errorf("fetchUnicornWorkerPids: expected %s but got %s", expectedPids, pids)
    39  	}
    40  }
    41  
    42  type TestCPUTimePipedCommands struct{}
    43  
    44  func (r TestCPUTimePipedCommands) Output(commands ...[]string) ([]byte, error) {
    45  	return []byte("418\n"), nil
    46  }
    47  
    48  func TestIdleWorkerCount(t *testing.T) {
    49  	pipedCommands = TestCPUTimePipedCommands{}
    50  	pids := []string{"584", "1857", "2258", "2627", "2872", "3085", "3546"}
    51  	expectedCount := len(pids)
    52  	c, _ := idleWorkerCount(pids)
    53  	if c != 7 {
    54  		t.Errorf("idleWorkerCount: expected %d but got %d", expectedCount, c)
    55  	}
    56  }
    57  
    58  func TestCPUTime(t *testing.T) {
    59  	pipedCommands = TestCPUTimePipedCommands{}
    60  	pid := "3061"
    61  	expectedCPUTime := "418"
    62  	c, _ := cpuTime(pid)
    63  	if c != expectedCPUTime {
    64  		t.Errorf("cpuTime: expected %s but got %s", expectedCPUTime, c)
    65  	}
    66  }