Skip to content

Commit

Permalink
Add test showing that a closed connection gets recreated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan McCall committed Mar 26, 2018
1 parent ea1eef8 commit 4c3815d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,44 @@ func TestTotal(t *testing.T) {
}
}

func TestReconnecting(t *testing.T) {
ln, udpAddr := newLocalListenerUDP(t)
defer ln.Close()

prefix := "test."

client := NewStatsdClient(udpAddr.String(), prefix)
client.reconnect_ticker = time.NewTicker(10 * time.Millisecond)

ch := make(chan string, 0)

s := map[string]int64{
"a:b:c": 5,
"d:e:f": 2,
}

go doListenUDP(t, ln, ch, len(s))

client.CreateSocket()
client.Close()

time.Sleep(15 * time.Millisecond)
for k, v := range s {
fmt.Println("sent", k, v)
client.Total(k, v)
}

timeout := time.After(30 * time.Millisecond)
for i := len(s); i > 0; i-- {
select {
case x := <-ch:
fmt.Println("received", x)
case <-timeout:
t.Fatal("Timed out")
}
}
}

func doListenUDP(t *testing.T, conn *net.UDPConn, ch chan string, n int) {
for n > 0 {
// Handle the connection in a new goroutine.
Expand Down

0 comments on commit 4c3815d

Please sign in to comment.