Skip to content

Commit

Permalink
Merge pull request #204 from Conorbro/bbe-tcp-fix
Browse files Browse the repository at this point in the history
Fixed timeout bug
  • Loading branch information
brian-brazil authored Aug 4, 2017
2 parents bd8cba5 + a055413 commit 9b4c39b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
4 changes: 2 additions & 2 deletions tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"net"
"regexp"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/config"
Expand Down Expand Up @@ -73,7 +72,7 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome
Help: "Indicates if probe failed due to regex",
})
registry.MustRegister(probeFailedDueToRegex)
deadline := time.Now().Add(module.Timeout)
deadline, _ := ctx.Deadline()
conn, err := dialTCP(ctx, target, module, registry)
if err != nil {
log.Errorf("Error dialing TCP: %v", err)
Expand Down Expand Up @@ -114,6 +113,7 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome
}
}
if scanner.Err() != nil {
log.Errorf("Error reading from connection: %v", scanner.Err().Error())
return false
}
if match == nil {
Expand Down
20 changes: 6 additions & 14 deletions tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTCPConnection(t *testing.T) {
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
registry := prometheus.NewRegistry()
if !probeTCP(testCTX, ln.Addr().String(), Module{Timeout: time.Second}, registry) {
if !probeTCP(testCTX, ln.Addr().String(), Module{}, registry) {
t.Fatalf("TCP module failed, expected success.")
}
<-ch
Expand All @@ -54,7 +54,7 @@ func TestTCPConnectionFails(t *testing.T) {
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if probeTCP(testCTX, ":0", Module{Timeout: time.Second}, registry) {
if probeTCP(testCTX, ":0", Module{}, registry) {
t.Fatalf("TCP module suceeded, expected failure.")
}
}
Expand All @@ -70,7 +70,6 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) {
defer cancel()

module := Module{
Timeout: time.Second,
TCP: TCPProbe{
QueryResponse: []QueryResponse{
{Send: "NICK prober"},
Expand Down Expand Up @@ -137,9 +136,8 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) {

testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

time.Sleep(time.Millisecond * 100)
module := Module{
Timeout: time.Second,
TCP: TCPProbe{
QueryResponse: []QueryResponse{
{
Expand Down Expand Up @@ -206,7 +204,6 @@ func TestTCPConnectionProtocol(t *testing.T) {

// Force IPv4
module := Module{
Timeout: time.Second,
TCP: TCPProbe{
PreferredIPProtocol: "ip4",
},
Expand All @@ -228,8 +225,7 @@ func TestTCPConnectionProtocol(t *testing.T) {

// Force IPv6
module = Module{
Timeout: time.Second,
TCP: TCPProbe{},
TCP: TCPProbe{},
}

registry = prometheus.NewRegistry()
Expand All @@ -248,7 +244,6 @@ func TestTCPConnectionProtocol(t *testing.T) {

// Prefer IPv4
module = Module{
Timeout: time.Second,
TCP: TCPProbe{
PreferredIPProtocol: "ip4",
},
Expand All @@ -270,7 +265,6 @@ func TestTCPConnectionProtocol(t *testing.T) {

// Prefer IPv6
module = Module{
Timeout: time.Second,
TCP: TCPProbe{
PreferredIPProtocol: "ip6",
},
Expand All @@ -292,8 +286,7 @@ func TestTCPConnectionProtocol(t *testing.T) {

// Prefer nothing
module = Module{
Timeout: time.Second,
TCP: TCPProbe{},
TCP: TCPProbe{},
}

registry = prometheus.NewRegistry()
Expand All @@ -312,8 +305,7 @@ func TestTCPConnectionProtocol(t *testing.T) {

// No protocol
module = Module{
Timeout: time.Second,
TCP: TCPProbe{},
TCP: TCPProbe{},
}

registry = prometheus.NewRegistry()
Expand Down

0 comments on commit 9b4c39b

Please sign in to comment.