2011-08-21 12:38:51 +00:00
|
|
|
package client
|
|
|
|
|
2011-08-23 09:49:22 +00:00
|
|
|
import (
|
2011-11-11 07:50:31 +00:00
|
|
|
"bufio"
|
2012-06-26 10:20:57 +00:00
|
|
|
"code.google.com/p/gomock/gomock"
|
2011-11-06 04:56:46 +00:00
|
|
|
"github.com/fluffle/goirc/state"
|
2013-02-16 03:24:10 +00:00
|
|
|
"github.com/fluffle/golog/logging"
|
2011-11-15 22:17:29 +00:00
|
|
|
"strings"
|
2011-08-23 09:49:22 +00:00
|
|
|
"testing"
|
2011-08-23 09:53:52 +00:00
|
|
|
"time"
|
2011-08-23 09:49:22 +00:00
|
|
|
)
|
2011-08-21 12:38:51 +00:00
|
|
|
|
2011-11-06 04:56:46 +00:00
|
|
|
type testState struct {
|
|
|
|
ctrl *gomock.Controller
|
2013-02-16 11:29:56 +00:00
|
|
|
st *state.MockTracker
|
2011-11-06 04:56:46 +00:00
|
|
|
nc *mockNetConn
|
|
|
|
c *Conn
|
2011-09-29 21:54:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 07:50:31 +00:00
|
|
|
func setUp(t *testing.T, start ...bool) (*Conn, *testState) {
|
2011-11-06 04:56:46 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
2013-02-16 11:29:56 +00:00
|
|
|
st := state.NewMockTracker(ctrl)
|
2011-11-06 04:56:46 +00:00
|
|
|
nc := MockNetConn(t)
|
2013-02-18 01:42:44 +00:00
|
|
|
c := SimpleClient("test", "test", "Testing IRC")
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.SetLogLevel(logging.LogFatal)
|
2011-11-06 04:56:46 +00:00
|
|
|
|
2013-02-16 11:29:56 +00:00
|
|
|
c.st = st
|
2011-11-06 04:56:46 +00:00
|
|
|
c.sock = nc
|
2013-02-16 18:05:56 +00:00
|
|
|
c.cfg.Flood = true // Tests can take a while otherwise
|
2013-02-18 01:36:17 +00:00
|
|
|
c.connected = true
|
2011-11-11 07:50:31 +00:00
|
|
|
if len(start) == 0 {
|
|
|
|
// Hack to allow tests of send, recv, write etc.
|
|
|
|
// NOTE: the value of the boolean doesn't matter.
|
|
|
|
c.postConnect()
|
2013-02-16 03:24:10 +00:00
|
|
|
|
2011-11-11 10:49:33 +00:00
|
|
|
// Sleep 1ms to allow background routines to start.
|
|
|
|
<-time.After(1e6)
|
2011-11-11 07:50:31 +00:00
|
|
|
}
|
2011-08-24 12:53:28 +00:00
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
return c, &testState{ctrl, st, nc, c}
|
2011-08-21 12:38:51 +00:00
|
|
|
}
|
2011-08-22 22:23:29 +00:00
|
|
|
|
2011-11-06 04:56:46 +00:00
|
|
|
func (s *testState) tearDown() {
|
|
|
|
s.st.EXPECT().Wipe()
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
s.c.shutdown()
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
2011-11-06 04:56:46 +00:00
|
|
|
s.ctrl.Finish()
|
2011-08-24 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
2011-08-23 09:53:52 +00:00
|
|
|
// Practically the same as the above test, but shutdown is called implicitly
|
|
|
|
// by recv() getting an EOF from the mock connection.
|
|
|
|
func TestEOF(t *testing.T) {
|
2011-11-06 04:56:46 +00:00
|
|
|
c, s := setUp(t)
|
|
|
|
// Since we're not using tearDown() here, manually call Finish()
|
|
|
|
defer s.ctrl.Finish()
|
2011-08-23 09:53:52 +00:00
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// Set up a handler to detect whether disconnected handlers are called
|
|
|
|
dcon := false
|
2013-02-16 03:24:10 +00:00
|
|
|
c.HandleFunc(DISCONNECTED, func(conn *Conn, line *Line) {
|
2013-02-16 00:17:31 +00:00
|
|
|
dcon = true
|
|
|
|
})
|
|
|
|
|
2011-08-23 09:53:52 +00:00
|
|
|
// Simulate EOF from server
|
2011-11-06 04:56:46 +00:00
|
|
|
s.st.EXPECT().Wipe()
|
|
|
|
s.nc.Close()
|
2011-08-23 09:53:52 +00:00
|
|
|
|
2011-09-29 21:54:54 +00:00
|
|
|
// Since things happen in different internal goroutines, we need to wait
|
|
|
|
// 1 ms should be enough :-)
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
2011-08-23 09:53:52 +00:00
|
|
|
|
|
|
|
// Verify that the connection no longer thinks it's connected
|
2013-02-18 01:36:17 +00:00
|
|
|
if c.connected {
|
2011-08-23 09:53:52 +00:00
|
|
|
t.Errorf("Conn still thinks it's connected to the server.")
|
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
|
|
|
|
// Verify that disconnected handler was called
|
|
|
|
if !dcon {
|
|
|
|
t.Errorf("Conn did not call disconnected handlers.")
|
|
|
|
}
|
2011-08-23 09:53:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 15:06:44 +00:00
|
|
|
func TestClientAndStateTracking(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
2013-02-16 11:29:56 +00:00
|
|
|
st := state.NewMockTracker(ctrl)
|
2013-02-18 01:42:44 +00:00
|
|
|
c := SimpleClient("test", "test", "Testing IRC")
|
2011-11-10 15:06:44 +00:00
|
|
|
|
|
|
|
// Assert some basic things about the initial state of the Conn struct
|
2013-02-18 01:36:17 +00:00
|
|
|
if me := c.cfg.Me; me.Nick != "test" || me.Ident != "test" ||
|
|
|
|
me.Name != "Testing IRC" || me.Host != "" {
|
|
|
|
t.Errorf("Conn.cfg.Me not correctly initialised.")
|
2011-11-10 15:06:44 +00:00
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
// Check that the internal handlers are correctly set up
|
|
|
|
for k, _ := range intHandlers {
|
|
|
|
if _, ok := c.handlers.set[strings.ToLower(k)]; !ok {
|
|
|
|
t.Errorf("Missing internal handler for '%s'.", k)
|
|
|
|
}
|
2011-11-10 15:06:44 +00:00
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
|
|
|
|
// Now enable the state tracking code and check its handlers
|
2011-11-10 15:06:44 +00:00
|
|
|
c.EnableStateTracking()
|
2013-02-16 00:17:31 +00:00
|
|
|
for k, _ := range stHandlers {
|
|
|
|
if _, ok := c.handlers.set[strings.ToLower(k)]; !ok {
|
|
|
|
t.Errorf("Missing state handler for '%s'.", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(c.stRemovers) != len(stHandlers) {
|
|
|
|
t.Errorf("Incorrect number of Removers (%d != %d) when adding state handlers.",
|
|
|
|
len(c.stRemovers), len(stHandlers))
|
|
|
|
}
|
2011-11-10 15:06:44 +00:00
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// We're expecting the untracked me to be replaced by a tracked one
|
2013-02-18 01:36:17 +00:00
|
|
|
if me := c.cfg.Me; me.Nick != "test" || me.Ident != "test" ||
|
|
|
|
me.Name != "Testing IRC" || me.Host != "" {
|
2011-11-10 15:06:44 +00:00
|
|
|
t.Errorf("Enabling state tracking did not replace Me correctly.")
|
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
if c.st == nil || c.cfg.Me != c.st.Me() {
|
2011-11-10 15:06:44 +00:00
|
|
|
t.Errorf("State tracker not enabled correctly.")
|
|
|
|
}
|
2011-08-23 09:49:22 +00:00
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// Now, shim in the mock state tracker and test disabling state tracking
|
2013-02-18 01:36:17 +00:00
|
|
|
me := c.cfg.Me
|
2013-02-16 11:29:56 +00:00
|
|
|
c.st = st
|
2011-11-10 15:06:44 +00:00
|
|
|
st.EXPECT().Wipe()
|
|
|
|
c.DisableStateTracking()
|
2013-02-18 01:36:17 +00:00
|
|
|
if c.st != nil || c.cfg.Me != me {
|
2011-11-10 15:06:44 +00:00
|
|
|
t.Errorf("State tracker not disabled correctly.")
|
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
|
|
|
|
// Finally, check state tracking handlers were all removed correctly
|
|
|
|
for k, _ := range stHandlers {
|
|
|
|
if _, ok := c.handlers.set[strings.ToLower(k)]; ok && k != "NICK" {
|
|
|
|
// A bit leaky, because intHandlers adds a NICK handler.
|
|
|
|
t.Errorf("State handler for '%s' not removed correctly.", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(c.stRemovers) != 0 {
|
|
|
|
t.Errorf("stRemovers not zeroed correctly when removing state handlers.")
|
|
|
|
}
|
2011-11-10 15:06:44 +00:00
|
|
|
ctrl.Finish()
|
2011-08-23 09:49:22 +00:00
|
|
|
}
|
2011-11-11 07:50:31 +00:00
|
|
|
|
|
|
|
func TestSend(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()
|
|
|
|
|
|
|
|
// ... so we have to do some of it's work here.
|
|
|
|
c.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(c.sock),
|
|
|
|
bufio.NewWriter(c.sock))
|
|
|
|
|
|
|
|
// Assert that before send is running, nothing should be sent to the socket
|
|
|
|
// but writes to the buffered channel "out" should not block.
|
|
|
|
c.out <- "SENT BEFORE START"
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
|
|
|
|
// We want to test that the a goroutine calling send will exit correctly.
|
|
|
|
exited := false
|
|
|
|
go func() {
|
|
|
|
c.send()
|
|
|
|
exited = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send is now running in the background as if started by postConnect.
|
|
|
|
// This should read the line previously buffered in c.out, and write it
|
|
|
|
// to the socket connection.
|
|
|
|
s.nc.Expect("SENT BEFORE START")
|
|
|
|
|
|
|
|
// Send another line, just to be sure :-)
|
|
|
|
c.out <- "SENT AFTER START"
|
|
|
|
s.nc.Expect("SENT AFTER START")
|
|
|
|
|
|
|
|
// Now, use the control channel to exit send and kill the goroutine.
|
|
|
|
if exited {
|
|
|
|
t.Errorf("Exited before signal sent.")
|
|
|
|
}
|
|
|
|
c.cSend <- true
|
|
|
|
// Allow propagation time...
|
|
|
|
<-time.After(1e6)
|
|
|
|
if !exited {
|
|
|
|
t.Errorf("Didn't exit after signal.")
|
|
|
|
}
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
|
|
|
|
// Sending more on c.out shouldn't reach the network.
|
|
|
|
c.out <- "SENT AFTER END"
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
}
|
2011-11-11 08:35:23 +00:00
|
|
|
|
|
|
|
func TestRecv(t *testing.T) {
|
|
|
|
// Passing a second value to setUp inhibits postConnect()
|
|
|
|
c, s := setUp(t, false)
|
|
|
|
// We can't tearDown here as we need to explicitly test recv exiting.
|
|
|
|
// The same shutdown() caveat in TestSend above also applies.
|
|
|
|
defer s.ctrl.Finish()
|
|
|
|
|
|
|
|
// ... so we have to do some of it's work here.
|
|
|
|
c.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(c.sock),
|
|
|
|
bufio.NewWriter(c.sock))
|
|
|
|
|
|
|
|
// Send a line before recv is started up, to verify nothing appears on c.in
|
|
|
|
s.nc.Send(":irc.server.org 001 test :First test line.")
|
|
|
|
|
|
|
|
// reader is a helper to do a "non-blocking" read of c.in
|
|
|
|
reader := func() *Line {
|
|
|
|
select {
|
2013-02-16 00:17:31 +00:00
|
|
|
case <-time.After(time.Millisecond):
|
2011-11-11 08:35:23 +00:00
|
|
|
case l := <-c.in:
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if l := reader(); l != nil {
|
|
|
|
t.Errorf("Line parsed before recv started.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to test that the a goroutine calling recv will exit correctly.
|
|
|
|
exited := false
|
|
|
|
go func() {
|
|
|
|
c.recv()
|
|
|
|
exited = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Strangely, recv() needs some time to start up, but *only* when this test
|
|
|
|
// is run standalone with: client/_test/_testmain --test.run TestRecv
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
2011-11-11 08:35:23 +00:00
|
|
|
|
|
|
|
// Now, this should mean that we'll receive our parsed line on c.in
|
|
|
|
if l := reader(); l == nil || l.Cmd != "001" {
|
|
|
|
t.Errorf("Bad first line received on input channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a second line, just to be sure.
|
|
|
|
s.nc.Send(":irc.server.org 002 test :Second test line.")
|
|
|
|
if l := reader(); l == nil || l.Cmd != "002" {
|
|
|
|
t.Errorf("Bad second line received on input channel.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that recv does something useful with a line it can't parse
|
|
|
|
// (not that there are many, parseLine is forgiving).
|
|
|
|
s.nc.Send(":textwithnospaces")
|
|
|
|
if l := reader(); l != nil {
|
|
|
|
t.Errorf("Bad line still caused receive on input channel.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only way recv() exits is when the socket closes.
|
|
|
|
if exited {
|
|
|
|
t.Errorf("Exited before socket close.")
|
|
|
|
}
|
|
|
|
s.st.EXPECT().Wipe()
|
|
|
|
s.nc.Close()
|
|
|
|
|
|
|
|
// Since send and runloop aren't actually running, we need to empty their
|
|
|
|
// channels manually for recv() to be able to call shutdown correctly.
|
|
|
|
<-c.cSend
|
|
|
|
<-c.cLoop
|
2011-11-15 22:17:29 +00:00
|
|
|
<-c.cPing
|
2011-11-11 08:35:23 +00:00
|
|
|
// Give things time to shake themselves out...
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
2011-11-11 08:35:23 +00:00
|
|
|
if !exited {
|
|
|
|
t.Errorf("Didn't exit on socket close.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since s.nc is closed we can't attempt another send on it...
|
|
|
|
if l := reader(); l != nil {
|
|
|
|
t.Errorf("Line received on input channel after socket close.")
|
|
|
|
}
|
|
|
|
}
|
2011-11-11 09:20:28 +00:00
|
|
|
|
2011-11-15 22:17:29 +00:00
|
|
|
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.
|
2013-02-16 18:05:56 +00:00
|
|
|
c.cfg.PingFreq = 50 * time.Millisecond
|
2011-11-15 22:17:29 +00:00
|
|
|
|
|
|
|
// reader is a helper to do a "non-blocking" read of c.out
|
|
|
|
reader := func() string {
|
|
|
|
select {
|
2012-06-06 16:12:07 +00:00
|
|
|
case <-time.After(time.Millisecond):
|
2011-11-15 22:17:29 +00:00
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// The first ping should be after 50ms,
|
2011-11-15 22:17:29 +00:00
|
|
|
// so we don't expect anything now on c.in
|
|
|
|
if s := reader(); s != "" {
|
|
|
|
t.Errorf("Line output directly after ping started.")
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:12:07 +00:00
|
|
|
<-time.After(50 * time.Millisecond)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
|
2012-06-06 16:12:07 +00:00
|
|
|
t.Errorf("Line not output after 50ms.")
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:12:07 +00:00
|
|
|
// Reader waits for 1ms and we call it a few times above.
|
|
|
|
<-time.After(45 * time.Millisecond)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s != "" {
|
2012-06-06 16:12:07 +00:00
|
|
|
t.Errorf("Line output under 50ms after last ping.")
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:12:07 +00:00
|
|
|
// This is a short window (49-51ms) in which the ping should happen
|
|
|
|
// This may result in flaky tests; sorry (and file a bug) if so.
|
|
|
|
<-time.After(2 * time.Millisecond)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
|
2012-06-06 16:12:07 +00:00
|
|
|
t.Errorf("Line not output after another 2ms.")
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now kill the ping loop.
|
|
|
|
if exited {
|
|
|
|
t.Errorf("Exited before signal sent.")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cPing <- true
|
2012-06-06 16:12:07 +00:00
|
|
|
// Make sure we're no longer pinging by waiting ~2x PingFreq
|
|
|
|
<-time.After(105 * time.Millisecond)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s != "" {
|
|
|
|
t.Errorf("Line output after ping stopped.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exited {
|
|
|
|
t.Errorf("Didn't exit after signal.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 09:20:28 +00:00
|
|
|
func TestRunLoop(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()
|
|
|
|
|
|
|
|
// ... so we have to do some of it's work here.
|
|
|
|
c.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(c.sock),
|
|
|
|
bufio.NewWriter(c.sock))
|
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// Set up a handler to detect whether 001 handler is called
|
|
|
|
h001 := false
|
2013-02-16 03:24:10 +00:00
|
|
|
c.HandleFunc("001", func(conn *Conn, line *Line) {
|
2013-02-16 00:17:31 +00:00
|
|
|
h001 = true
|
|
|
|
})
|
|
|
|
// Set up a handler to detect whether 002 handler is called
|
|
|
|
h002 := false
|
2013-02-16 03:24:10 +00:00
|
|
|
c.HandleFunc("002", func(conn *Conn, line *Line) {
|
2013-02-16 00:17:31 +00:00
|
|
|
h002 = true
|
|
|
|
})
|
|
|
|
|
2011-11-11 09:20:28 +00:00
|
|
|
l1 := parseLine(":irc.server.org 001 test :First test line.")
|
|
|
|
c.in <- l1
|
2013-02-16 00:17:31 +00:00
|
|
|
if h001 {
|
|
|
|
t.Errorf("001 handler called before runLoop started.")
|
|
|
|
}
|
2011-11-11 09:20:28 +00:00
|
|
|
|
|
|
|
// We want to test that the a goroutine calling runLoop will exit correctly.
|
|
|
|
// Now, we can expect the call to Dispatch to take place as runLoop starts.
|
|
|
|
exited := false
|
|
|
|
go func() {
|
|
|
|
c.runLoop()
|
|
|
|
exited = true
|
|
|
|
}()
|
|
|
|
// Here, the opposite seemed to take place, with TestRunLoop failing when
|
|
|
|
// run as part of the suite but passing when run on it's own.
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
|
|
|
if !h001 {
|
|
|
|
t.Errorf("001 handler not called after runLoop started.")
|
|
|
|
}
|
2011-11-11 09:20:28 +00:00
|
|
|
|
|
|
|
// Send another line, just to be sure :-)
|
|
|
|
l2 := parseLine(":irc.server.org 002 test :Second test line.")
|
|
|
|
c.in <- l2
|
|
|
|
// It appears some sleeping is needed after all of these to ensure channel
|
|
|
|
// sends occur before the close signal is sent below...
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
|
|
|
if !h002 {
|
|
|
|
t.Errorf("002 handler not called while runLoop started.")
|
|
|
|
}
|
2011-11-11 09:20:28 +00:00
|
|
|
|
|
|
|
// Now, use the control channel to exit send and kill the goroutine.
|
|
|
|
if exited {
|
|
|
|
t.Errorf("Exited before signal sent.")
|
|
|
|
}
|
|
|
|
c.cLoop <- true
|
|
|
|
// Allow propagation time...
|
2013-02-16 00:17:31 +00:00
|
|
|
<-time.After(time.Millisecond)
|
2011-11-11 09:20:28 +00:00
|
|
|
if !exited {
|
|
|
|
t.Errorf("Didn't exit after signal.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sending more on c.in shouldn't dispatch any further events
|
2013-02-16 00:17:31 +00:00
|
|
|
h001 = false
|
2011-11-11 09:20:28 +00:00
|
|
|
c.in <- l1
|
2013-02-16 00:17:31 +00:00
|
|
|
if h001 {
|
|
|
|
t.Errorf("001 handler called after runLoop ended.")
|
|
|
|
}
|
2011-11-11 09:20:28 +00:00
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
|
|
|
|
func TestWrite(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()
|
|
|
|
|
|
|
|
// ... so we have to do some of it's work here.
|
|
|
|
c.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(c.sock),
|
|
|
|
bufio.NewWriter(c.sock))
|
|
|
|
|
|
|
|
// Write should just write a line to the socket.
|
|
|
|
c.write("yo momma")
|
|
|
|
s.nc.Expect("yo momma")
|
|
|
|
|
2013-02-16 18:05:56 +00:00
|
|
|
// Flood control is disabled -- setUp sets c.cfg.Flood = true -- so we should
|
2012-02-04 01:32:48 +00:00
|
|
|
// not have set c.badness at this point.
|
|
|
|
if c.badness != 0 {
|
2011-11-11 10:17:17 +00:00
|
|
|
t.Errorf("Flood control used when Flood = true.")
|
|
|
|
}
|
|
|
|
|
2013-02-16 18:05:56 +00:00
|
|
|
c.cfg.Flood = false
|
2011-11-11 10:17:17 +00:00
|
|
|
c.write("she so useless")
|
|
|
|
s.nc.Expect("she so useless")
|
|
|
|
|
2012-02-04 01:32:48 +00:00
|
|
|
// The lastsent time should have been updated very recently...
|
|
|
|
if time.Now().Sub(c.lastsent) > time.Millisecond {
|
2011-11-11 10:17:17 +00:00
|
|
|
t.Errorf("Flood control not used when Flood = false.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, test the error state by closing the socket then writing.
|
|
|
|
// This little function makes sure that all the blocking channels that are
|
|
|
|
// written to during the course of s.nc.Close() and c.write() are read from
|
|
|
|
// again, to prevent deadlocks when these are both called synchronously.
|
|
|
|
// XXX: This may well be a horrible hack.
|
|
|
|
go func() {
|
|
|
|
<-c.cSend
|
|
|
|
<-c.cLoop
|
2011-11-15 22:17:29 +00:00
|
|
|
<-c.cPing
|
2011-11-11 10:17:17 +00:00
|
|
|
}()
|
|
|
|
s.nc.Close()
|
|
|
|
s.st.EXPECT().Wipe()
|
|
|
|
c.write("she can't pass unit tests")
|
|
|
|
}
|
2011-11-11 11:17:18 +00:00
|
|
|
|
|
|
|
func TestRateLimit(t *testing.T) {
|
|
|
|
c, s := setUp(t)
|
|
|
|
defer s.tearDown()
|
|
|
|
|
2012-02-04 01:32:48 +00:00
|
|
|
if c.badness != 0 {
|
2011-11-11 11:17:18 +00:00
|
|
|
t.Errorf("Bad initial values for rate limit variables.")
|
|
|
|
}
|
|
|
|
|
2012-02-04 23:34:09 +00:00
|
|
|
// We'll be needing this later...
|
|
|
|
abs := func(i time.Duration) time.Duration {
|
2013-02-16 03:24:10 +00:00
|
|
|
if i < 0 {
|
2012-02-04 23:34:09 +00:00
|
|
|
return -i
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the changes to the time module, c.lastsent is now a time.Time.
|
|
|
|
// It's initialised on client creation to time.Now() which for the purposes
|
|
|
|
// of this test was probably around 1.2 ms ago. This is inconvenient.
|
|
|
|
// Making it >10s ago effectively clears out the inconsistency, as this
|
|
|
|
// makes elapsed > linetime and thus zeros c.badness and resets c.lastsent.
|
|
|
|
c.lastsent = time.Now().Add(-10 * time.Second)
|
|
|
|
if l := c.rateLimit(60); l != 0 || c.badness != 0 {
|
|
|
|
t.Errorf("Rate limit got non-zero badness from long-ago lastsent.")
|
2011-11-11 11:17:18 +00:00
|
|
|
}
|
2012-02-04 23:34:09 +00:00
|
|
|
|
2011-11-11 11:17:18 +00:00
|
|
|
// So, time at the nanosecond resolution is a bit of a bitch. Choosing 60
|
|
|
|
// characters as the line length means we should be increasing badness by
|
|
|
|
// 2.5 seconds minus the delta between the two ratelimit calls. This should
|
2012-02-04 23:34:09 +00:00
|
|
|
// be minimal but it's guaranteed that it won't be zero. Use 10us as a fuzz.
|
2013-02-16 00:17:31 +00:00
|
|
|
if l := c.rateLimit(60); l != 0 ||
|
2013-02-16 03:24:10 +00:00
|
|
|
abs(c.badness-2500*time.Millisecond) > 10*time.Microsecond {
|
2011-11-11 11:17:18 +00:00
|
|
|
t.Errorf("Rate limit calculating badness incorrectly.")
|
|
|
|
}
|
2012-02-04 23:34:09 +00:00
|
|
|
// At this point, we can tip over the badness scale, with a bit of help.
|
|
|
|
// 720 chars => +8 seconds of badness => 10.5 seconds => ratelimit
|
2013-02-16 03:24:10 +00:00
|
|
|
if l := c.rateLimit(720); l != 8*time.Second ||
|
|
|
|
abs(c.badness-10500*time.Millisecond) > 10*time.Microsecond {
|
2011-11-11 11:17:18 +00:00
|
|
|
t.Errorf("Rate limit failed to return correct limiting values.")
|
2012-02-04 23:34:09 +00:00
|
|
|
t.Errorf("l=%d, badness=%d", l, c.badness)
|
2011-11-11 11:17:18 +00:00
|
|
|
}
|
|
|
|
}
|