Skip to content

Commit

Permalink
Add package comment to tell what it does (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne authored Nov 1, 2020
1 parent abb23fe commit e9993e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 9 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
Expand Down Expand Up @@ -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())
}

Expand All @@ -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
}
Expand All @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e9993e8

Please sign in to comment.