Helpers for testing whether Conn errors are triggered.

This commit is contained in:
Alex Bramley 2011-08-24 13:53:28 +01:00
parent 6815c19bb3
commit 389f5247f5
2 changed files with 33 additions and 12 deletions

View File

@ -8,6 +8,8 @@ import (
func setUp(t *testing.T) (*mockNetConn, *Conn) { func setUp(t *testing.T) (*mockNetConn, *Conn) {
c := New("test", "test", "Testing IRC") c := New("test", "test", "Testing IRC")
c.State = t
m := MockNetConn(t) m := MockNetConn(t)
c.sock = m c.sock = m
c.Flood = true // Tests can take a while otherwise c.Flood = true // Tests can take a while otherwise
@ -21,6 +23,30 @@ func tearDown(m *mockNetConn, c *Conn) {
m.Close() m.Close()
} }
func (conn *Conn) ExpectError() {
// With the current error handling, we could block on reading the Err
// channel, so ensure we don't wait forever with a 5ms timeout.
t := conn.State.(*testing.T)
timer := time.NewTimer(5e6)
select {
case <-timer.C:
t.Errorf("Error expected on Err channel, none received.")
case <-conn.Err:
timer.Stop()
}
}
func (conn *Conn) ExpectNoErrors() {
t := conn.State.(*testing.T)
timer := time.NewTimer(5e6)
select {
case <-timer.C:
case err := <-conn.Err:
timer.Stop()
t.Errorf("No error expected on Err channel, received:\n\t%s", err)
}
}
func TestShutdown(t *testing.T) { func TestShutdown(t *testing.T) {
_, c := setUp(t) _, c := setUp(t)

View File

@ -2,7 +2,6 @@ package client
import ( import (
"testing" "testing"
"time"
) )
// This test performs a simple end-to-end verification of correct line parsing // This test performs a simple end-to-end verification of correct line parsing
@ -89,7 +88,8 @@ func TestNICK(t *testing.T) {
// Call handler with a NICK line changing "our" nick to test1. // Call handler with a NICK line changing "our" nick to test1.
c.h_NICK(parseLine(":test!test@somehost.com NICK :test1")) c.h_NICK(parseLine(":test!test@somehost.com NICK :test1"))
// Should generate no response to server // Should generate no errors and no response to server
c.ExpectNoErrors()
m.ExpectNothing() m.ExpectNothing()
// Verify that our Nick has changed // Verify that our Nick has changed
@ -102,6 +102,9 @@ func TestNICK(t *testing.T) {
// Call handler with a NICK line changing user1 to somebody // Call handler with a NICK line changing user1 to somebody
c.h_NICK(parseLine(":user1!ident1@host1.com NICK :somebody")) c.h_NICK(parseLine(":user1!ident1@host1.com NICK :somebody"))
c.ExpectNoErrors()
m.ExpectNothing()
if c.GetNick("user1") != nil { if c.GetNick("user1") != nil {
t.Errorf("Still have a valid Nick for 'user1'.") t.Errorf("Still have a valid Nick for 'user1'.")
} }
@ -111,16 +114,8 @@ func TestNICK(t *testing.T) {
// Send a NICK line for an unknown nick. // Send a NICK line for an unknown nick.
c.h_NICK(parseLine(":blah!moo@cows.com NICK :milk")) c.h_NICK(parseLine(":blah!moo@cows.com NICK :milk"))
c.ExpectError()
// With the current error handling, we could block on reading the Err m.ExpectNothing()
// channel, so ensure we don't wait forever with a 5ms timeout.
timer := time.NewTimer(5e6)
select {
case <-timer.C:
t.Errorf("No error received for bad NICK line.")
case <-c.Err:
timer.Stop()
}
} }
// Test the handler for CTCP messages // Test the handler for CTCP messages