From e9993e894ab1d5dbf0e07afe67c1ebf23f961b03 Mon Sep 17 00:00:00 2001 From: Ryo Nakao Date: Sun, 1 Nov 2020 21:48:02 +0900 Subject: [PATCH] Add package comment to tell what it does (#4) --- agent/agent.go | 16 +++++++++------- agent/agent_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 agent/agent_test.go diff --git a/agent/agent.go b/agent/agent.go index d2ef22e..b72d2dd 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// Package agent provides an ability to handle the gosivy agent, +// which serves the process statistics. package agent import ( @@ -25,7 +27,7 @@ const defaultAddr = "127.0.0.1:0" var ( mu sync.Mutex - portfile string + pidFile string listener net.Listener logWriter io.Writer ) @@ -55,7 +57,7 @@ func Listen(opts Options) error { logWriter = os.Stderr } - if portfile != "" { + if pidFile != "" { return fmt.Errorf("gosivy agent already listening at: %v", listener.Addr()) } @@ -79,8 +81,8 @@ func Listen(opts Options) error { } listener = ln port := listener.Addr().(*net.TCPAddr).Port - portfile = fmt.Sprintf("%s/%d", cfgDir, os.Getpid()) - err = ioutil.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm) + pidFile = fmt.Sprintf("%s/%d", cfgDir, os.Getpid()) + err = ioutil.WriteFile(pidFile, []byte(strconv.Itoa(port)), os.ModePerm) if err != nil { return err } @@ -95,9 +97,9 @@ func Close() { mu.Lock() defer mu.Unlock() - if portfile != "" { - os.Remove(portfile) - portfile = "" + if pidFile != "" { + os.Remove(pidFile) + pidFile = "" } if listener != nil { listener.Close() diff --git a/agent/agent_test.go b/agent/agent_test.go new file mode 100644 index 0000000..ca89304 --- /dev/null +++ b/agent/agent_test.go @@ -0,0 +1,18 @@ +package agent + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListenAndClose(t *testing.T) { + err := Listen(Options{}) + assert.Nil(t, err) + Close() + _, err = os.Stat(pidFile) + + assert.True(t, os.IsNotExist(err)) + assert.Empty(t, pidFile) +}