1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-05-14 11:33:20 +00:00

Fix issues/9 by implementing a client-side ping loop.

This commit is contained in:
Alex Bramley 2011-11-15 22:17:29 +00:00
parent 907560b599
commit 8fa5e5624e
2 changed files with 95 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/fluffle/golog/logging"
"github.com/fluffle/goirc/state"
"gomock.googlecode.com/hg/gomock"
"strings"
"testing"
"time"
)
@ -260,6 +261,7 @@ func TestRecv(t *testing.T) {
// channels manually for recv() to be able to call shutdown correctly.
<-c.cSend
<-c.cLoop
<-c.cPing
// Give things time to shake themselves out...
<-time.After(1e6)
if !exited {
@ -272,6 +274,74 @@ func TestRecv(t *testing.T) {
}
}
func TestPing(t *testing.T) {
// Passing a second value to setUp inhibits postConnect()
c, s := setUp(t, false)
// We can't use tearDown here, as it will cause a deadlock in shutdown()
// trying to send kill messages down channels to nonexistent goroutines.
defer s.ctrl.Finish()
// Set a low ping frequency for testing.
// This still increases testing time by a good few seconds :-/
c.PingFreq = 1
// reader is a helper to do a "non-blocking" read of c.out
reader := func() string {
select {
case <-time.After(1e6):
case s := <-c.out:
return s
}
return ""
}
if s := reader(); s != "" {
t.Errorf("Line output before ping started.")
}
// Start ping loop.
exited := false
go func() {
c.ping()
exited = true
}()
// The first ping should be after a second,
// so we don't expect anything now on c.in
if s := reader(); s != "" {
t.Errorf("Line output directly after ping started.")
}
<-time.After(1e9)
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
t.Errorf("Line not output after 1 second.")
}
<-time.After(1e7)
if s := reader(); s != "" {
t.Errorf("Line output under a second after last ping.")
}
<-time.After(1e9)
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
t.Errorf("Line not output after another second.")
}
// Now kill the ping loop.
if exited {
t.Errorf("Exited before signal sent.")
}
c.cPing <- true
<-time.After(1e9)
if s := reader(); s != "" {
t.Errorf("Line output after ping stopped.")
}
if !exited {
t.Errorf("Didn't exit after signal.")
}
}
func TestRunLoop(t *testing.T) {
// Passing a second value to setUp inhibits postConnect()
c, s := setUp(t, false)
@ -364,6 +434,7 @@ func TestWrite(t *testing.T) {
go func() {
<-c.cSend
<-c.cLoop
<-c.cPing
}()
s.nc.Close()