Fix IRC client tests
This commit is contained in:
parent
48496b4285
commit
58caacd269
@ -7,18 +7,16 @@ import (
|
||||
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var c *Client
|
||||
var conn *mockConn
|
||||
|
||||
func init() {
|
||||
initTestClient()
|
||||
func testClient() *Client {
|
||||
return NewClient("test", "testing")
|
||||
}
|
||||
|
||||
func initTestClient() {
|
||||
c = NewClient("test", "testing")
|
||||
conn = &mockConn{hook: make(chan string, 3)}
|
||||
func testClientSend() (*Client, chan string) {
|
||||
c := testClient()
|
||||
conn := &mockConn{hook: make(chan string, 16)}
|
||||
c.conn = conn
|
||||
go c.send()
|
||||
return c, conn.hook
|
||||
}
|
||||
|
||||
type mockConn struct {
|
||||
@ -36,106 +34,120 @@ func (c *mockConn) Close() error {
|
||||
}
|
||||
|
||||
func TestPass(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.writePass("pass")
|
||||
assert.Equal(t, "PASS pass\r\n", <-conn.hook)
|
||||
assert.Equal(t, "PASS pass\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestNick(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Nick("test2")
|
||||
assert.Equal(t, "test2", c.GetNick())
|
||||
assert.Equal(t, "NICK test2\r\n", <-conn.hook)
|
||||
assert.Equal(t, "NICK test2\r\n", <-out)
|
||||
|
||||
c.writeNick("nick")
|
||||
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
|
||||
assert.Equal(t, "NICK nick\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestUser(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.writeUser("user", "rn")
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestOper(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Oper("name", "pass")
|
||||
assert.Equal(t, "OPER name pass\r\n", <-conn.hook)
|
||||
assert.Equal(t, "OPER name pass\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestMode(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Mode("#chan", "+o", "user")
|
||||
assert.Equal(t, "MODE #chan +o user\r\n", <-conn.hook)
|
||||
assert.Equal(t, "MODE #chan +o user\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestQuit(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.connected = true
|
||||
c.Quit()
|
||||
assert.Equal(t, "QUIT\r\n", <-conn.hook)
|
||||
assert.Equal(t, "QUIT\r\n", <-out)
|
||||
_, ok := <-c.quit
|
||||
assert.Equal(t, false, ok)
|
||||
|
||||
initTestClient()
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Join("#a")
|
||||
assert.Equal(t, "JOIN #a\r\n", <-conn.hook)
|
||||
assert.Equal(t, "JOIN #a\r\n", <-out)
|
||||
c.Join("#b", "#c")
|
||||
assert.Equal(t, "JOIN #b,#c\r\n", <-conn.hook)
|
||||
assert.Equal(t, "JOIN #b,#c\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestPart(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Part("#a")
|
||||
assert.Equal(t, "PART #a\r\n", <-conn.hook)
|
||||
assert.Equal(t, "PART #a\r\n", <-out)
|
||||
c.Part("#b", "#c")
|
||||
assert.Equal(t, "PART #b,#c\r\n", <-conn.hook)
|
||||
assert.Equal(t, "PART #b,#c\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestTopic(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Topic("#chan")
|
||||
assert.Equal(t, "TOPIC #chan\r\n", <-conn.hook)
|
||||
assert.Equal(t, "TOPIC #chan\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestInvite(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Invite("user", "#chan")
|
||||
assert.Equal(t, "INVITE user #chan\r\n", <-conn.hook)
|
||||
assert.Equal(t, "INVITE user #chan\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestKick(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Kick("#chan", "user")
|
||||
assert.Equal(t, "KICK #chan user\r\n", <-conn.hook)
|
||||
assert.Equal(t, "KICK #chan user\r\n", <-out)
|
||||
c.Kick("#chan", "a", "b")
|
||||
assert.Equal(t, "KICK #chan a,b\r\n", <-conn.hook)
|
||||
assert.Equal(t, "KICK #chan a,b\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestPrivmsg(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Privmsg("user", "the message")
|
||||
assert.Equal(t, "PRIVMSG user :the message\r\n", <-conn.hook)
|
||||
assert.Equal(t, "PRIVMSG user :the message\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestNotice(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Notice("user", "the message")
|
||||
assert.Equal(t, "NOTICE user :the message\r\n", <-conn.hook)
|
||||
assert.Equal(t, "NOTICE user :the message\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestWhois(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Whois("user")
|
||||
assert.Equal(t, "WHOIS user\r\n", <-conn.hook)
|
||||
assert.Equal(t, "WHOIS user\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestAway(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.Away("not here")
|
||||
assert.Equal(t, "AWAY :not here\r\n", <-conn.hook)
|
||||
assert.Equal(t, "AWAY :not here\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.nick = "nick"
|
||||
c.Username = "user"
|
||||
c.Realname = "rn"
|
||||
c.register()
|
||||
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
|
||||
assert.Equal(t, "NICK nick\r\n", <-out)
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
||||
|
||||
c.Password = "pass"
|
||||
c.register()
|
||||
assert.Equal(t, "PASS pass\r\n", <-conn.hook)
|
||||
assert.Equal(t, "NICK nick\r\n", <-conn.hook)
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
|
||||
assert.Equal(t, "PASS pass\r\n", <-out)
|
||||
assert.Equal(t, "NICK nick\r\n", <-out)
|
||||
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
||||
}
|
||||
|
@ -78,45 +78,50 @@ func (i *mockIrcd) handle(conn net.Conn) {
|
||||
}
|
||||
|
||||
func TestConnect(t *testing.T) {
|
||||
c := testClient()
|
||||
c.Connect("127.0.0.1:45678")
|
||||
assert.Equal(t, c.Host, "127.0.0.1")
|
||||
assert.Equal(t, c.Server, "127.0.0.1:45678")
|
||||
waitConnAndClose(t)
|
||||
initTestClient()
|
||||
waitConnAndClose(t, c)
|
||||
}
|
||||
|
||||
func TestConnectTLS(t *testing.T) {
|
||||
c := testClient()
|
||||
c.TLS = true
|
||||
c.Connect("127.0.0.1:45679")
|
||||
assert.Equal(t, c.Host, "127.0.0.1")
|
||||
assert.Equal(t, c.Server, "127.0.0.1:45679")
|
||||
waitConnAndClose(t)
|
||||
initTestClient()
|
||||
waitConnAndClose(t, c)
|
||||
}
|
||||
|
||||
func TestConnectDefaultPorts(t *testing.T) {
|
||||
c := testClient()
|
||||
c.Connect("127.0.0.1")
|
||||
assert.Equal(t, "127.0.0.1:6667", c.Server)
|
||||
initTestClient()
|
||||
|
||||
c = testClient()
|
||||
c.TLS = true
|
||||
c.Connect("127.0.0.1")
|
||||
assert.Equal(t, "127.0.0.1:6697", c.Server)
|
||||
initTestClient()
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
c, out := testClientSend()
|
||||
c.write("test")
|
||||
assert.Equal(t, "test\r\n", <-conn.hook)
|
||||
assert.Equal(t, "test\r\n", <-out)
|
||||
c.Write("test")
|
||||
assert.Equal(t, "test\r\n", <-conn.hook)
|
||||
assert.Equal(t, "test\r\n", <-out)
|
||||
c.writef("test %d", 2)
|
||||
assert.Equal(t, "test 2\r\n", <-conn.hook)
|
||||
assert.Equal(t, "test 2\r\n", <-out)
|
||||
c.Writef("test %d", 2)
|
||||
assert.Equal(t, "test 2\r\n", <-conn.hook)
|
||||
assert.Equal(t, "test 2\r\n", <-out)
|
||||
}
|
||||
|
||||
func TestRecv(t *testing.T) {
|
||||
c := testClient()
|
||||
conn := &mockConn{hook: make(chan string, 16)}
|
||||
c.conn = conn
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
buf.WriteString("CMD\r\n")
|
||||
buf.WriteString("PING :test\r\n")
|
||||
@ -124,12 +129,12 @@ func TestRecv(t *testing.T) {
|
||||
c.reader = bufio.NewReader(buf)
|
||||
|
||||
c.ready.Add(1)
|
||||
go c.send()
|
||||
close(c.quit)
|
||||
go c.recv()
|
||||
|
||||
assert.Equal(t, "PONG :test\r\n", <-conn.hook)
|
||||
assert.Equal(t, &Message{Command: "CMD"}, <-c.Messages)
|
||||
initTestClient()
|
||||
}
|
||||
|
||||
func TestRecvRecoversPanic(t *testing.T) {
|
||||
@ -137,17 +142,19 @@ func TestRecvRecoversPanic(t *testing.T) {
|
||||
assert.Nil(t, recover())
|
||||
}()
|
||||
|
||||
c := testClient()
|
||||
|
||||
buf := bytes.NewBuffer([]byte("CMD\r\n"))
|
||||
c.reader = bufio.NewReader(buf)
|
||||
close(c.Messages)
|
||||
c.recv()
|
||||
|
||||
c.Messages = make(chan *Message, 32)
|
||||
}
|
||||
|
||||
func TestRecvTriggersReconnect(t *testing.T) {
|
||||
c.reader = bufio.NewReader(&bytes.Buffer{})
|
||||
c := testClient()
|
||||
c.conn = &mockConn{}
|
||||
c.ready.Add(1)
|
||||
c.reader = bufio.NewReader(&bytes.Buffer{})
|
||||
done := make(chan struct{})
|
||||
ok := false
|
||||
go func() {
|
||||
@ -167,7 +174,7 @@ func TestRecvTriggersReconnect(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
defer initTestClient()
|
||||
c := testClient()
|
||||
c.close()
|
||||
ok := false
|
||||
done := make(chan struct{})
|
||||
@ -187,7 +194,7 @@ func TestClose(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func waitConnAndClose(t *testing.T) {
|
||||
func waitConnAndClose(t *testing.T, c *Client) {
|
||||
done := make(chan struct{})
|
||||
quit := make(chan struct{})
|
||||
go func() {
|
||||
|
Loading…
Reference in New Issue
Block a user