Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions collector/tcpstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package collector

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -116,12 +116,12 @@ func getTCPStats(statsFile string) (map[tcpConnectionState]float64, error) {

func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
tcpStats := map[tcpConnectionState]float64{}
contents, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

for _, line := range strings.Split(string(contents), "\n")[1:] {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(strings.TrimSpace(line), "sl") {
continue
}
parts := strings.Fields(line)
if len(parts) == 0 {
continue
Expand Down
7 changes: 0 additions & 7 deletions collector/tcpstat_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ func Test_parseTCPStatsError(t *testing.T) {

func TestTCPStat(t *testing.T) {

noFile, _ := os.Open("follow the white rabbit")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should probably not be removed. We want to make sure we catch the error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because these lines are replaced

		return nil, err
	}

So that test is no longer valid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is not obsolete, you need to check the scanner for errors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i forgot to check scanner.Err().

defer noFile.Close()

if _, err := parseTCPStats(noFile); err == nil {
t.Fatal("expected an error, but none occurred")
}

file, err := os.Open("fixtures/proc/net/tcpstat")
if err != nil {
t.Fatal(err)
Expand Down