Skip to content

Commit

Permalink
getlantern/lantern#2117 Switched to utility for counting file descrip…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
oxtoacart committed Jan 6, 2015
1 parent cb04363 commit 1de390f
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions waddell_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package waddell

import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"os/exec"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/getlantern/fdcount"
"github.com/getlantern/testify/assert"
)

Expand Down Expand Up @@ -104,17 +102,21 @@ func TestPeersTLS(t *testing.T) {
}

func doTestPeers(t *testing.T, useTLS bool) {
socketsAtStart := countTCPFiles()
_, fdc, err := fdcount.Matching("TCP")
if err != nil {
t.Fatal(err)
}

closeActions := make([]func(), 0)
peers := make([]*Client, 0, NumPeers)
defer func() {
for _, action := range closeActions {
action()
}

// Wait a short time to let sockets finish closing
time.Sleep(250 * time.Millisecond)
socketsAtEnd := countTCPFiles()
assert.Equal(t, socketsAtStart, socketsAtEnd, "All file descriptors should have been closed")
assert.NoError(t, fdc.AssertDelta(0), "All file descriptors should have been closed")

// Make sure we can't do stuff with closed client
client := peers[0]
Expand Down Expand Up @@ -320,13 +322,3 @@ func largeData() []byte {
}
return b
}

// see https://groups.google.com/forum/#!topic/golang-nuts/c0AnWXjzNIA
func countTCPFiles() int {
out, err := exec.Command("lsof", "-p", fmt.Sprintf("%v", os.Getpid())).Output()
if err != nil {
log.Fatal(err)
}
log.Tracef("lsof result: %s", string(out))
return bytes.Count(out, []byte("TCP"))
}

0 comments on commit 1de390f

Please sign in to comment.