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 Apr 4, 2018
1 parent c04dcbe commit 0f6e846
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 @@ -615,6 +615,44 @@ func newLocalListenerUDP(t *testing.T) (*net.UDPConn, *net.UDPAddr) {
return ln, udpAddr
}

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) {
var wg sync.WaitGroup
wg.Add(n)
Expand Down

0 comments on commit 0f6e846

Please sign in to comment.