From 389f5247f5b23b63b264c908936f675255c2c681 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Wed, 24 Aug 2011 13:53:28 +0100 Subject: [PATCH] Helpers for testing whether Conn errors are triggered. --- client/connection_test.go | 26 ++++++++++++++++++++++++++ client/handlers_test.go | 19 +++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/client/connection_test.go b/client/connection_test.go index 7a2e8ba..7490af3 100644 --- a/client/connection_test.go +++ b/client/connection_test.go @@ -8,6 +8,8 @@ import ( func setUp(t *testing.T) (*mockNetConn, *Conn) { c := New("test", "test", "Testing IRC") + c.State = t + m := MockNetConn(t) c.sock = m c.Flood = true // Tests can take a while otherwise @@ -21,6 +23,30 @@ func tearDown(m *mockNetConn, c *Conn) { 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) { _, c := setUp(t) diff --git a/client/handlers_test.go b/client/handlers_test.go index 5e29694..497e946 100644 --- a/client/handlers_test.go +++ b/client/handlers_test.go @@ -2,7 +2,6 @@ package client import ( "testing" - "time" ) // 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. 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() // 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 c.h_NICK(parseLine(":user1!ident1@host1.com NICK :somebody")) + c.ExpectNoErrors() + m.ExpectNothing() + if c.GetNick("user1") != nil { 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. c.h_NICK(parseLine(":blah!moo@cows.com NICK :milk")) - - // With the current error handling, we could block on reading the Err - // 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() - } + c.ExpectError() + m.ExpectNothing() } // Test the handler for CTCP messages