2011-08-21 12:38:51 +00:00
|
|
|
package client
|
|
|
|
|
2011-08-23 09:49:22 +00:00
|
|
|
import (
|
2016-06-10 18:37:51 +00:00
|
|
|
"runtime"
|
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"
|
2016-06-10 18:37:51 +00:00
|
|
|
|
|
|
|
"github.com/fluffle/goirc/state"
|
|
|
|
"github.com/golang/mock/gomock"
|
2011-08-23 09:49:22 +00:00
|
|
|
)
|
2011-08-21 12:38:51 +00:00
|
|
|
|
2013-09-30 13:43:29 +00:00
|
|
|
type checker struct {
|
|
|
|
t *testing.T
|
|
|
|
c chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func callCheck(t *testing.T) checker {
|
|
|
|
return checker{t: t, c: make(chan struct{})}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c checker) call() {
|
|
|
|
c.c <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c checker) assertNotCalled(fmt string, args ...interface{}) {
|
|
|
|
select {
|
|
|
|
case <-c.c:
|
|
|
|
c.t.Errorf(fmt, args...)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c checker) assertWasCalled(fmt string, args ...interface{}) {
|
|
|
|
select {
|
|
|
|
case <-c.c:
|
|
|
|
case <-time.After(time.Millisecond):
|
|
|
|
// Usually need to wait for goroutines to settle :-/
|
|
|
|
c.t.Errorf(fmt, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-10 13:16:14 +00:00
|
|
|
// NOTE: including a second argument at all prevents calling c.postConnect()
|
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")
|
2015-04-13 18:05:42 +00:00
|
|
|
c.initialise()
|
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
|
2013-04-08 17:02:34 +00:00
|
|
|
// If a second argument is passed to setUp, we tell postConnect not to
|
|
|
|
// start the various goroutines that shuttle data around.
|
|
|
|
c.postConnect(len(start) == 0)
|
|
|
|
// Sleep 1ms to allow background routines to start.
|
2013-09-30 13:43:29 +00:00
|
|
|
<-time.After(time.Millisecond)
|
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.nc.ExpectNothing()
|
2016-09-16 18:40:27 +00:00
|
|
|
s.c.Close()
|
2011-11-06 04:56:46 +00:00
|
|
|
s.ctrl.Finish()
|
2011-08-24 12:53:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 18:40:27 +00:00
|
|
|
// Practically the same as the above test, but Close is called implicitly
|
2011-08-23 09:53:52 +00:00
|
|
|
// 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
|
2013-09-30 13:43:29 +00:00
|
|
|
dcon := callCheck(t)
|
2013-03-10 13:16:14 +00:00
|
|
|
c.HandleFunc(DISCONNECTED, func(conn *Conn, line *Line) {
|
2013-09-30 13:43:29 +00:00
|
|
|
dcon.call()
|
2013-02-16 00:17:31 +00:00
|
|
|
})
|
|
|
|
|
2011-08-23 09:53:52 +00:00
|
|
|
// Simulate EOF from server
|
2011-11-06 04:56:46 +00:00
|
|
|
s.nc.Close()
|
2011-08-23 09:53:52 +00:00
|
|
|
|
2013-09-30 13:43:29 +00:00
|
|
|
// Verify that disconnected handler was called
|
|
|
|
dcon.assertWasCalled("Conn did not call disconnected handlers.")
|
2011-08-23 09:53:52 +00:00
|
|
|
|
|
|
|
// Verify that the connection no longer thinks it's connected
|
2013-09-30 12:55:55 +00:00
|
|
|
if c.Connected() {
|
2011-08-23 09:53:52 +00:00
|
|
|
t.Errorf("Conn still thinks it's connected to the server.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-04-01 16:54:33 +00:00
|
|
|
me := c.cfg.Me
|
|
|
|
if me.Nick != "test" || me.Ident != "test" ||
|
2013-02-18 01:36:17 +00:00
|
|
|
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 {
|
2013-09-27 23:15:54 +00:00
|
|
|
if _, ok := c.intHandlers.set[strings.ToLower(k)]; !ok {
|
2013-02-16 00:17:31 +00:00
|
|
|
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 {
|
2013-09-27 23:15:54 +00:00
|
|
|
if _, ok := c.intHandlers.set[strings.ToLower(k)]; !ok {
|
2013-02-16 00:17:31 +00:00
|
|
|
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))
|
|
|
|
}
|
2015-04-01 16:54:33 +00:00
|
|
|
if neu := c.Me(); neu.Nick != me.Nick || neu.Ident != me.Ident ||
|
|
|
|
neu.Name != me.Name || neu.Host != me.Host {
|
|
|
|
t.Errorf("Enabling state tracking erased information about me!")
|
|
|
|
}
|
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
|
2015-01-02 12:58:50 +00:00
|
|
|
if c.st == nil {
|
|
|
|
t.Errorf("State tracker not enabled correctly.")
|
|
|
|
}
|
2015-04-01 16:54:33 +00:00
|
|
|
if me = c.cfg.Me; me.Nick != "test" || me.Ident != "test" ||
|
2013-02-18 01:36:17 +00:00
|
|
|
me.Name != "Testing IRC" || me.Host != "" {
|
2011-11-10 15:06:44 +00:00
|
|
|
t.Errorf("Enabling state tracking did not replace Me 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-16 11:29:56 +00:00
|
|
|
c.st = st
|
2015-01-02 12:58:50 +00:00
|
|
|
gomock.InOrder(
|
|
|
|
st.EXPECT().Me().Return(me),
|
|
|
|
st.EXPECT().Wipe(),
|
|
|
|
)
|
2011-11-10 15:06:44 +00:00
|
|
|
c.DisableStateTracking()
|
2015-01-02 12:58:50 +00:00
|
|
|
if c.st != nil || !c.cfg.Me.Equals(me) {
|
2011-11-10 15:06:44 +00:00
|
|
|
t.Errorf("State tracker not disabled correctly.")
|
|
|
|
}
|
2013-02-16 00:17:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-04-02 08:35:58 +00:00
|
|
|
func TestSendExitsOnDie(t *testing.T) {
|
2013-04-08 17:02:34 +00:00
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
2011-11-11 07:50:31 +00:00
|
|
|
c, s := setUp(t, false)
|
2013-04-08 17:02:34 +00:00
|
|
|
defer s.tearDown()
|
2011-11-11 07:50:31 +00:00
|
|
|
|
|
|
|
// 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.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited := callCheck(t)
|
2014-09-05 14:39:56 +00:00
|
|
|
// send() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(1)
|
2011-11-11 07:50:31 +00:00
|
|
|
go func() {
|
|
|
|
c.send()
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.call()
|
2011-11-11 07:50:31 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// 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.
|
2013-04-08 17:02:34 +00:00
|
|
|
// This sneakily uses the fact that the other two goroutines that would
|
|
|
|
// normally be waiting for die to close are not running, so we only send
|
2016-09-16 18:40:27 +00:00
|
|
|
// to the goroutine started above. Normally Close() closes c.die and
|
2013-04-08 17:02:34 +00:00
|
|
|
// signals to all three goroutines (send, ping, runLoop) to exit.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertNotCalled("Exited before signal sent.")
|
2013-04-08 17:02:34 +00:00
|
|
|
c.die <- struct{}{}
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertWasCalled("Didn't exit after signal.")
|
2011-11-11 07:50:31 +00:00
|
|
|
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
|
|
|
|
2015-04-02 08:35:58 +00:00
|
|
|
func TestSendExitsOnWriteError(t *testing.T) {
|
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
|
|
|
c, s := setUp(t, false)
|
|
|
|
// We can't use tearDown here because we're testing shutdown conditions
|
|
|
|
// (and so need to EXPECT() a call to st.Wipe() in the right place)
|
|
|
|
defer s.ctrl.Finish()
|
|
|
|
|
|
|
|
// We want to test that the a goroutine calling send will exit correctly.
|
|
|
|
exited := callCheck(t)
|
|
|
|
// send() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
c.send()
|
|
|
|
exited.call()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Send a line to be sure things are good.
|
|
|
|
c.out <- "SENT AFTER START"
|
|
|
|
s.nc.Expect("SENT AFTER START")
|
|
|
|
|
|
|
|
// Now, close the underlying socket to cause write() to return an error.
|
2016-09-16 18:40:27 +00:00
|
|
|
// This will call Close() => a call to st.Wipe() will happen.
|
2015-04-02 08:35:58 +00:00
|
|
|
exited.assertNotCalled("Exited before signal sent.")
|
|
|
|
s.nc.Close()
|
|
|
|
// Sending more on c.out shouldn't reach the network, but we need to send
|
|
|
|
// *something* to trigger a call to write() that will fail.
|
|
|
|
c.out <- "SENT AFTER END"
|
|
|
|
exited.assertWasCalled("Didn't exit after signal.")
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:01:36 +00:00
|
|
|
func TestSendDeadlockOnFullBuffer(t *testing.T) {
|
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
|
|
|
c, s := setUp(t, false)
|
|
|
|
// We can't use tearDown here because we're testing a deadlock condition
|
2016-09-16 18:40:27 +00:00
|
|
|
// and if tearDown tries to call Close() it will deadlock some more
|
|
|
|
// because send() is holding the conn mutex via Close() already.
|
2015-11-02 21:01:36 +00:00
|
|
|
defer s.ctrl.Finish()
|
|
|
|
|
|
|
|
// We want to test that the a goroutine calling send will exit correctly.
|
|
|
|
loopExit := callCheck(t)
|
|
|
|
sendExit := callCheck(t)
|
|
|
|
// send() and runLoop() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(2)
|
|
|
|
|
|
|
|
// The deadlock arises when a handler being called from conn.dispatch() in
|
|
|
|
// runLoop() tries to write to conn.out to send a message back to the IRC
|
|
|
|
// server, but the buffer is full. If at the same time send() is
|
2016-09-16 18:40:27 +00:00
|
|
|
// calling conn.Close() and waiting in there for runLoop() to call
|
2015-11-02 21:01:36 +00:00
|
|
|
// conn.wg.Done(), it will not empty the buffer of conn.out => deadlock.
|
|
|
|
//
|
|
|
|
// We simulate this by artifically filling conn.out. We must use a
|
|
|
|
// goroutine to put in one more line than the buffer can hold, because
|
|
|
|
// send() will read a line from conn.out on its first loop iteration:
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 33; i++ {
|
|
|
|
c.out <- "FILL BUFFER WITH CRAP"
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Then we add a handler that tries to write a line to conn.out:
|
|
|
|
c.HandleFunc(PRIVMSG, func(conn *Conn, line *Line) {
|
|
|
|
conn.Raw(line.Raw)
|
|
|
|
})
|
|
|
|
// And trigger it by starting runLoop and inserting a line into conn.in:
|
|
|
|
go func() {
|
|
|
|
c.runLoop()
|
|
|
|
loopExit.call()
|
|
|
|
}()
|
|
|
|
c.in <- &Line{Cmd: PRIVMSG, Raw: "WRITE THAT CAUSES DEADLOCK"}
|
|
|
|
|
|
|
|
// At this point the handler should be blocked on a write to conn.out,
|
|
|
|
// preventing runLoop from looping and thus noticing conn.die is closed.
|
|
|
|
//
|
2016-09-16 18:40:27 +00:00
|
|
|
// The next part is to force send() to call conn.Close(), which can
|
2015-11-02 21:01:36 +00:00
|
|
|
// be done by closing the fake net.Conn so that it returns an error on
|
|
|
|
// calls to Write():
|
|
|
|
s.nc.ExpectNothing()
|
|
|
|
s.nc.Close()
|
|
|
|
|
|
|
|
// Now when send is started it will read one line from conn.out and try
|
|
|
|
// to write it to the socket. It should immediately receive an error and
|
2016-09-16 18:40:27 +00:00
|
|
|
// call conn.Close(), triggering the deadlock as it waits forever for
|
2015-11-02 21:01:36 +00:00
|
|
|
// runLoop to call conn.wg.Done.
|
|
|
|
go func() {
|
|
|
|
c.send()
|
|
|
|
sendExit.call()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make sure that things are definitely deadlocked.
|
|
|
|
<-time.After(time.Millisecond)
|
|
|
|
|
|
|
|
// Verify that the connection no longer thinks it's connected, i.e.
|
2016-09-16 18:40:27 +00:00
|
|
|
// conn.Close() has definitely been called. We can't call
|
|
|
|
// conn.Connected() here because conn.Close() holds the mutex.
|
2015-11-02 21:01:36 +00:00
|
|
|
if c.connected {
|
|
|
|
t.Errorf("Conn still thinks it's connected to the server.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect both loops to terminate cleanly. If either of them don't
|
|
|
|
// then we have successfully deadlocked :-(
|
|
|
|
loopExit.assertWasCalled("runLoop did not exit cleanly.")
|
|
|
|
sendExit.assertWasCalled("send did not exit cleanly.")
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:35:23 +00:00
|
|
|
func TestRecv(t *testing.T) {
|
2013-04-08 17:02:34 +00:00
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
2011-11-11 08:35:23 +00:00
|
|
|
c, s := setUp(t, false)
|
2013-04-08 17:02:34 +00:00
|
|
|
// We can't use tearDown here because we're testing shutdown conditions
|
|
|
|
// (and so need to EXPECT() a call to st.Wipe() in the right place)
|
2011-11-11 08:35:23 +00:00
|
|
|
defer s.ctrl.Finish()
|
|
|
|
|
|
|
|
// 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.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited := callCheck(t)
|
2014-09-05 14:39:56 +00:00
|
|
|
// recv() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(1)
|
2011-11-11 08:35:23 +00:00
|
|
|
go func() {
|
|
|
|
c.recv()
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.call()
|
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
|
2014-07-06 18:26:34 +00:00
|
|
|
// (not that there are many, ParseLine is forgiving).
|
2011-11-11 08:35:23 +00:00
|
|
|
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.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertNotCalled("Exited before socket close.")
|
2011-11-11 08:35:23 +00:00
|
|
|
s.nc.Close()
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertWasCalled("Didn't exit on socket close.")
|
2011-11-11 08:35:23 +00:00
|
|
|
|
|
|
|
// 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) {
|
2013-04-08 17:02:34 +00:00
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
2011-11-15 22:17:29 +00:00
|
|
|
c, s := setUp(t, false)
|
2013-04-08 17:02:34 +00:00
|
|
|
defer s.tearDown()
|
2011-11-15 22:17:29 +00:00
|
|
|
|
2016-06-10 18:37:51 +00:00
|
|
|
res := time.Millisecond
|
|
|
|
|
|
|
|
// Windows has a timer resolution of 15.625ms by default.
|
|
|
|
// This means the test will be slower on windows, but
|
|
|
|
// should at least stop most of the flakiness...
|
|
|
|
// https://github.com/fluffle/goirc/issues/88
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
res = 15625 * time.Microsecond
|
|
|
|
}
|
|
|
|
|
2011-11-15 22:17:29 +00:00
|
|
|
// Set a low ping frequency for testing.
|
2016-06-10 18:37:51 +00:00
|
|
|
c.cfg.PingFreq = 10 * res
|
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 {
|
2016-06-10 18:37:51 +00:00
|
|
|
case <-time.After(res):
|
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.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited := callCheck(t)
|
2014-09-05 14:39:56 +00:00
|
|
|
// ping() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(1)
|
2011-11-15 22:17:29 +00:00
|
|
|
go func() {
|
|
|
|
c.ping()
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.call()
|
2011-11-15 22:17:29 +00:00
|
|
|
}()
|
|
|
|
|
2016-06-10 18:37:51 +00:00
|
|
|
// The first ping should be after 10*res ms,
|
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.")
|
|
|
|
}
|
|
|
|
|
2016-06-10 18:37:51 +00:00
|
|
|
<-time.After(c.cfg.PingFreq)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
|
2016-06-10 18:37:51 +00:00
|
|
|
t.Errorf("Line not output after %s.", c.cfg.PingFreq)
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 18:37:51 +00:00
|
|
|
// Reader waits for res ms and we call it a few times above.
|
|
|
|
<-time.After(7 * res)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s != "" {
|
2016-06-10 18:37:51 +00:00
|
|
|
t.Errorf("Line output <%s after last ping.", 7*res)
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 18:37:51 +00:00
|
|
|
// This is a short window in which the ping should happen
|
2012-06-06 16:12:07 +00:00
|
|
|
// This may result in flaky tests; sorry (and file a bug) if so.
|
2016-06-10 18:37:51 +00:00
|
|
|
<-time.After(2 * res)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
|
2016-06-10 18:37:51 +00:00
|
|
|
t.Errorf("Line not output after another %s.", 2*res)
|
2011-11-15 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now kill the ping loop.
|
2013-04-08 17:02:34 +00:00
|
|
|
// This sneakily uses the fact that the other two goroutines that would
|
|
|
|
// normally be waiting for die to close are not running, so we only send
|
2016-09-16 18:40:27 +00:00
|
|
|
// to the goroutine started above. Normally Close() closes c.die and
|
2013-04-08 17:02:34 +00:00
|
|
|
// signals to all three goroutines (send, ping, runLoop) to exit.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertNotCalled("Exited before signal sent.")
|
2013-04-08 17:02:34 +00:00
|
|
|
c.die <- struct{}{}
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertWasCalled("Didn't exit after signal.")
|
2016-06-10 18:37:51 +00:00
|
|
|
// Make sure we're no longer pinging by waiting >2x PingFreq
|
|
|
|
<-time.After(2*c.cfg.PingFreq + res)
|
2011-11-15 22:17:29 +00:00
|
|
|
if s := reader(); s != "" {
|
|
|
|
t.Errorf("Line output after ping stopped.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 09:20:28 +00:00
|
|
|
func TestRunLoop(t *testing.T) {
|
2013-04-08 17:02:34 +00:00
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
2011-11-11 09:20:28 +00:00
|
|
|
c, s := setUp(t, false)
|
2013-04-08 17:02:34 +00:00
|
|
|
defer s.tearDown()
|
2011-11-11 09:20:28 +00:00
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// Set up a handler to detect whether 001 handler is called
|
2013-09-30 13:43:29 +00:00
|
|
|
h001 := callCheck(t)
|
2013-02-16 10:33:15 +00:00
|
|
|
c.HandleFunc("001", func(conn *Conn, line *Line) {
|
2013-09-30 13:43:29 +00:00
|
|
|
h001.call()
|
2013-02-16 00:17:31 +00:00
|
|
|
})
|
2013-09-30 13:43:29 +00:00
|
|
|
h002 := callCheck(t)
|
2013-02-16 00:17:31 +00:00
|
|
|
// Set up a handler to detect whether 002 handler is called
|
2013-02-16 10:33:15 +00:00
|
|
|
c.HandleFunc("002", func(conn *Conn, line *Line) {
|
2013-09-30 13:43:29 +00:00
|
|
|
h002.call()
|
2013-02-16 00:17:31 +00:00
|
|
|
})
|
|
|
|
|
2014-07-06 18:26:34 +00:00
|
|
|
l1 := ParseLine(":irc.server.org 001 test :First test line.")
|
2011-11-11 09:20:28 +00:00
|
|
|
c.in <- l1
|
2013-09-30 13:43:29 +00:00
|
|
|
h001.assertNotCalled("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.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited := callCheck(t)
|
2014-09-05 14:39:56 +00:00
|
|
|
// runLoop() will decrement the WaitGroup, so we must increment it.
|
|
|
|
c.wg.Add(1)
|
2011-11-11 09:20:28 +00:00
|
|
|
go func() {
|
|
|
|
c.runLoop()
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.call()
|
2011-11-11 09:20:28 +00:00
|
|
|
}()
|
2013-09-30 13:43:29 +00:00
|
|
|
h001.assertWasCalled("001 handler not called after runLoop started.")
|
2011-11-11 09:20:28 +00:00
|
|
|
|
|
|
|
// Send another line, just to be sure :-)
|
2013-09-30 13:43:29 +00:00
|
|
|
h002.assertNotCalled("002 handler called before expected.")
|
2014-07-06 18:26:34 +00:00
|
|
|
l2 := ParseLine(":irc.server.org 002 test :Second test line.")
|
2011-11-11 09:20:28 +00:00
|
|
|
c.in <- l2
|
2013-09-30 13:43:29 +00:00
|
|
|
h002.assertWasCalled("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.
|
2013-04-08 17:02:34 +00:00
|
|
|
// This sneakily uses the fact that the other two goroutines that would
|
|
|
|
// normally be waiting for die to close are not running, so we only send
|
2016-09-16 18:40:27 +00:00
|
|
|
// to the goroutine started above. Normally Close() closes c.die and
|
2013-04-08 17:02:34 +00:00
|
|
|
// signals to all three goroutines (send, ping, runLoop) to exit.
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertNotCalled("Exited before signal sent.")
|
2013-04-08 17:02:34 +00:00
|
|
|
c.die <- struct{}{}
|
2013-09-30 13:43:29 +00:00
|
|
|
exited.assertWasCalled("Didn't exit after signal.")
|
2011-11-11 09:20:28 +00:00
|
|
|
|
|
|
|
// Sending more on c.in shouldn't dispatch any further events
|
|
|
|
c.in <- l1
|
2013-09-30 13:43:29 +00:00
|
|
|
h001.assertNotCalled("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) {
|
2013-04-08 17:02:34 +00:00
|
|
|
// Passing a second value to setUp stops goroutines from starting
|
2011-11-11 10:17:17 +00:00
|
|
|
c, s := setUp(t, false)
|
2013-04-08 17:02:34 +00:00
|
|
|
// We can't use tearDown here because we're testing shutdown conditions
|
|
|
|
// (and so need to EXPECT() a call to st.Wipe() in the right place)
|
2011-11-11 10:17:17 +00:00
|
|
|
defer s.ctrl.Finish()
|
|
|
|
|
|
|
|
// Write should just write a line to the socket.
|
2015-04-02 08:35:58 +00:00
|
|
|
if err := c.write("yo momma"); err != nil {
|
|
|
|
t.Errorf("Write returned unexpected error %v", err)
|
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
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
|
2015-04-02 08:35:58 +00:00
|
|
|
if err := c.write("she so useless"); err != nil {
|
|
|
|
t.Errorf("Write returned unexpected error %v", err)
|
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
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.
|
2013-04-08 17:02:34 +00:00
|
|
|
s.nc.Close()
|
2015-04-02 08:35:58 +00:00
|
|
|
if err := c.write("she can't pass unit tests"); err == nil {
|
|
|
|
t.Errorf("Expected write to return error after socket close.")
|
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
}
|
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 10:33:15 +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
|
2013-03-10 15:51:43 +00:00
|
|
|
// be minimal but it's guaranteed that it won't be zero. Use 20us as a fuzz.
|
2013-02-16 00:17:31 +00:00
|
|
|
if l := c.rateLimit(60); l != 0 ||
|
2013-03-10 15:51:43 +00:00
|
|
|
abs(c.badness-2500*time.Millisecond) > 20*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 10:33:15 +00:00
|
|
|
if l := c.rateLimit(720); l != 8*time.Second ||
|
2013-03-10 15:51:43 +00:00
|
|
|
abs(c.badness-10500*time.Millisecond) > 20*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
|
|
|
}
|
|
|
|
}
|