Refactor unittests

This commit is contained in:
Sergey Matveev 2014-05-14 16:29:54 +04:00
parent 6d2c47a7af
commit 49c1eed2e7
2 changed files with 84 additions and 100 deletions

View File

@ -26,33 +26,30 @@ import (
// Testing network connection that satisfies net.Conn interface // Testing network connection that satisfies net.Conn interface
// Can send predefined messages and store all written ones // Can send predefined messages and store all written ones
type TestingConn struct { type TestingConn struct {
msgs []string inbound chan string
msg_ptr int outbound chan string
incoming []string
closed bool closed bool
} }
func NewTestingConn(msgs ...string) *TestingConn { func NewTestingConn() *TestingConn {
msgs_crlf := []string{} inbound := make(chan string, 8)
for _, msg := range msgs { outbound := make(chan string, 8)
msgs_crlf = append(msgs_crlf, msg+"\r\n") return &TestingConn{inbound: inbound, outbound: outbound}
}
return &TestingConn{msgs: msgs_crlf, msg_ptr: -1}
} }
func (conn TestingConn) Error() string { func (conn TestingConn) Error() string {
return "i am out" return "i am finished"
} }
func (conn *TestingConn) Read(b []byte) (n int, err error) { func (conn *TestingConn) Read(b []byte) (n int, err error) {
conn.msg_ptr++ msg := <-conn.inbound
if len(conn.msgs) == conn.msg_ptr { if msg == "" {
return 0, TestingConn{} return 0, conn
} }
for n, bt := range []byte(conn.msgs[conn.msg_ptr]) { for n, bt := range []byte(msg + CRLF) {
b[n] = bt b[n] = bt
} }
return len(conn.msgs[conn.msg_ptr]), nil return len(msg), nil
} }
type MyAddr struct{} type MyAddr struct{}
@ -65,12 +62,13 @@ func (a MyAddr) Network() string {
} }
func (conn *TestingConn) Write(b []byte) (n int, err error) { func (conn *TestingConn) Write(b []byte) (n int, err error) {
conn.incoming = append(conn.incoming, string(b)) conn.outbound <- string(b)
return 0, nil return len(b), nil
} }
func (conn *TestingConn) Close() error { func (conn *TestingConn) Close() error {
conn.closed = true conn.closed = true
//conn.incoming <- ""
return nil return nil
} }
@ -97,52 +95,55 @@ func (conn TestingConn) SetWriteDeadline(t time.Time) error {
// New client creation test. It must send an event about new client, // New client creation test. It must send an event about new client,
// two predefined messages from it and deletion one // two predefined messages from it and deletion one
func TestNewClient(t *testing.T) { func TestNewClient(t *testing.T) {
conn := NewTestingConn("foo", "bar") conn := NewTestingConn()
sink := make(chan ClientEvent) sink := make(chan ClientEvent)
client := NewClient("foohost", conn) client := NewClient("foohost", conn)
go client.Processor(sink) go client.Processor(sink)
event := <-sink event := <-sink
if event.event_type != EVENT_NEW { if event.event_type != EVENT_NEW {
t.Fail() t.Fatal("no NEW event")
} }
conn.inbound <- "foo"
event = <-sink event = <-sink
if (event.event_type != EVENT_MSG) || (event.text != "foo") { if (event.event_type != EVENT_MSG) || (event.text != "foo") {
t.Fail() t.Fatal("no first MSG")
} }
conn.inbound <- "bar"
event = <-sink event = <-sink
if (event.event_type != EVENT_MSG) || (event.text != "bar") { if (event.event_type != EVENT_MSG) || (event.text != "bar") {
t.Fail() t.Fatal("no second MSG")
} }
conn.inbound <- ""
event = <-sink event = <-sink
if event.event_type != EVENT_DEL { if event.event_type != EVENT_DEL {
t.Fail() t.Fatal("no client termination")
} }
} }
// Test replies formatting // Test replies formatting
func TestClientReplies(t *testing.T) { func TestClientReplies(t *testing.T) {
conn := NewTestingConn("foo", "bar") conn := NewTestingConn()
client := NewClient("foohost", conn) client := NewClient("foohost", conn)
client.nickname = "мойник" client.nickname = "мойник"
client.Reply("hello") client.Reply("hello")
if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost hello\r\n") { if r := <-conn.outbound; r != ":foohost hello\r\n" {
t.Fatal("did not recieve hello message") t.Fatal("did not recieve hello message")
} }
client.ReplyParts("200", "foo", "bar") client.ReplyParts("200", "foo", "bar")
if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 200 foo :bar\r\n") { if r := <-conn.outbound; r != ":foohost 200 foo :bar\r\n" {
t.Fatal("did not recieve 200 message") t.Fatal("did not recieve 200 message")
} }
client.ReplyNicknamed("200", "foo", "bar") client.ReplyNicknamed("200", "foo", "bar")
if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 200 мойник foo :bar\r\n") { if r := <-conn.outbound; r != ":foohost 200 мойник foo :bar\r\n" {
t.Fatal("did not recieve nicknamed message") t.Fatal("did not recieve nicknamed message")
} }
client.ReplyNotEnoughParameters("CMD") client.ReplyNotEnoughParameters("CMD")
if (len(conn.incoming) != 4) || (conn.incoming[3] != ":foohost 461 мойник CMD :Not enough parameters\r\n") { if r := <-conn.outbound; r != ":foohost 461 мойник CMD :Not enough parameters\r\n" {
t.Fatal("did not recieve 461 message") t.Fatal("did not recieve 461 message")
} }
} }

View File

@ -18,12 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestRegistrationWorkflow(t *testing.T) { func TestRegistrationWorkflow(t *testing.T) {
@ -32,87 +30,71 @@ func TestRegistrationWorkflow(t *testing.T) {
go daemon.Processor(events) go daemon.Processor(events)
conn := NewTestingConn() conn := NewTestingConn()
client := NewClient("foohost", conn) client := NewClient("foohost", conn)
go client.Processor(events)
events <- ClientEvent{client, EVENT_NEW, ""} conn.inbound <- "UNEXISTENT CMD" // should recieve nothing on this
events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"} conn.inbound <- "NICK"
time.Sleep(100)
if len(conn.incoming) > 0 { if r := <-conn.outbound; r != ":foohost 431 :No nickname given\r\n" {
t.Fail() t.Fatal("431 for NICK")
} }
events <- ClientEvent{client, EVENT_MSG, "NICK"} conn.inbound <- "NICK meinick\r\nUSER\r\n"
time.Sleep(100) if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
if (len(conn.incoming) != 1) || (conn.incoming[0] != ":foohost 431 :No nickname given\r\n") { t.Fatal("461 for USER", r)
t.Fail() }
if (client.nickname != "meinick") || client.registered {
t.Fatal("NICK saved")
} }
events <- ClientEvent{client, EVENT_MSG, "NICK meinick"} conn.inbound <- "USER 1 2 3\r\n"
time.Sleep(100) if r := <-conn.outbound; r != ":foohost 461 meinick USER :Not enough parameters\r\n" {
if (len(conn.incoming) != 1) || (client.nickname != "meinick") || client.registered { t.Fatal("461 again for USER")
t.Fail()
}
events <- ClientEvent{client, EVENT_MSG, "USER"}
time.Sleep(100)
if (len(conn.incoming) != 2) || (conn.incoming[1] != ":foohost 461 meinick USER :Not enough parameters\r\n") {
t.Fail()
}
events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3"}
time.Sleep(100)
if (len(conn.incoming) != 3) || (conn.incoming[2] != ":foohost 461 meinick USER :Not enough parameters\r\n") {
t.Fail()
} }
daemon.SendLusers(client) daemon.SendLusers(client)
if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 0 users") { if r := <-conn.outbound; !strings.Contains(r, "There are 0 users") {
t.Fail() t.Fatal("LUSERS")
} }
events <- ClientEvent{client, EVENT_MSG, "USER 1 2 3 :4 5"} conn.inbound <- "USER 1 2 3 :4 5\r\n"
time.Sleep(100) if r := <-conn.outbound; !strings.Contains(r, ":foohost 001") {
if (len(conn.incoming) < 4) || (client.username != "1") || (client.realname != "4 5") { t.Fatal("001 after registration")
t.Fail() }
if r := <-conn.outbound; !strings.Contains(r, ":foohost 002") {
t.Fatal("002 after registration")
}
if r := <-conn.outbound; !strings.Contains(r, ":foohost 003") {
t.Fatal("003 after registration")
}
if r := <-conn.outbound; !strings.Contains(r, ":foohost 004") {
t.Fatal("004 after registration")
}
if r := <-conn.outbound; !strings.Contains(r, ":foohost 251") {
t.Fatal("251 after registration")
}
if r := <-conn.outbound; !strings.Contains(r, ":foohost 422") {
t.Fatal("422 after registration")
}
if (client.username != "1") || (client.realname != "4 5") || !client.registered {
t.Fatal("client register")
} }
statuses := map[int]bool{1: false, 2: false, 3: false, 4: false, 251: false, 422: false} conn.inbound <- "AWAY\r\n"
for _, msg := range conn.incoming { conn.inbound <- "UNEXISTENT CMD\r\n"
for k, _ := range statuses { if r := <-conn.outbound; r != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
if strings.HasPrefix(msg, fmt.Sprintf(":foohost %03d", k)) { t.Fatal("reply for unexistent command")
statuses[k] = true
}
}
}
for _, v := range statuses {
if !v {
t.Fail()
}
}
if !client.registered {
t.Fail()
}
events <- ClientEvent{client, EVENT_MSG, "UNEXISTENT CMD"}
time.Sleep(100)
if conn.incoming[len(conn.incoming)-1] != ":foohost 421 meinick UNEXISTENT :Unknown command\r\n" {
t.Fail()
}
events <- ClientEvent{client, EVENT_MSG, "AWAY"}
time.Sleep(100)
if conn.incoming[len(conn.incoming)-1] == ":foohost 421 meinick AWAY :Unknown command\r\n" {
t.Fail()
} }
daemon.SendLusers(client) daemon.SendLusers(client)
if !strings.Contains(conn.incoming[len(conn.incoming)-1], "There are 1 users") { if r := <-conn.outbound; !strings.Contains(r, "There are 1 users") {
t.Fail() t.Fatal("1 users logged in")
} }
events <- ClientEvent{client, EVENT_MSG, "QUIT"} conn.inbound <- "QUIT\r\nUNEXISTENT CMD\r\n"
time.Sleep(100) <-conn.outbound
if !conn.closed { if !conn.closed {
t.Fail() t.Fatal("closed connection on QUIT")
} }
} }
@ -123,18 +105,19 @@ func TestMotd(t *testing.T) {
} }
defer os.Remove(fd.Name()) defer os.Remove(fd.Name())
fd.Write([]byte("catched\n")) fd.Write([]byte("catched\n"))
daemon := NewDaemon("foohost", fd.Name(), nil, nil)
conn := NewTestingConn() conn := NewTestingConn()
client := NewClient("foohost", conn) client := NewClient("foohost", conn)
daemon := NewDaemon("foohost", fd.Name(), nil, nil)
daemon.SendMotd(client) daemon.SendMotd(client)
catched := false if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 375") {
for _, msg := range conn.incoming { t.Fatal("MOTD start")
if strings.Contains(msg, "372 * :- catched") {
catched = true
}
} }
if !catched { if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") {
t.Fail() t.Fatal("MOTD contents")
}
if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") {
t.Fatal("MOTD end", r)
} }
} }