X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=room_test.go;h=8618fb9a3228509666a86d3058a65e427e224793;hp=35c699a7d8a9d01325bdfa2b80d6429f35a0fb69;hb=9f6cca6c56d12478afb026ca4bd4ec3a688d6c9b;hpb=60bd9cec0bea6457b64299e14de4c61c8e9082cf diff --git a/room_test.go b/room_test.go index 35c699a..8618fb9 100644 --- a/room_test.go +++ b/room_test.go @@ -1,3 +1,21 @@ +/* +goircd -- minimalistic simple Internet Relay Chat (IRC) server +Copyright (C) 2014-2016 Sergey Matveev + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + package main import ( @@ -5,54 +23,65 @@ import ( "testing" ) -func no_nickchan(t *testing.T, c *TestingConn) { +func noNickchan(t *testing.T, c *TestingConn) { if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 401") { t.Fatal("no nick/channel", r) } } -func no_chan(t *testing.T, c *TestingConn) { +func noChan(t *testing.T, c *TestingConn) { if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 403") { t.Fatal("no channel", r) } } -func not_enough_params(t *testing.T, c *TestingConn) { +func notEnoughParams(t *testing.T, c *TestingConn) { if r := <-c.outbound; !strings.HasPrefix(r, ":foohost 461") { t.Fatal("not enough params", r) } } func TestTwoUsers(t *testing.T) { - log_sink := make(chan LogEvent, 8) - state_sink := make(chan StateEvent, 8) - daemon := NewDaemon("foohost", "", log_sink, state_sink) + logSink = make(chan LogEvent, 8) + stateSink = make(chan StateEvent, 8) + host := "foohost" + hostname = &host events := make(chan ClientEvent) - go daemon.Processor(events) + roomsM.Lock() + rooms = make(map[string]*Room) + roomSinks = make(map[*Room]chan ClientEvent) + roomsM.Unlock() + clients = make(map[*Client]struct{}) + finished := make(chan struct{}) + go Processor(events, finished) + defer func() { + events <- ClientEvent{eventType: EventTerm} + <-finished + }() conn1 := NewTestingConn() conn2 := NewTestingConn() - client1 := NewClient("foohost", conn1) - client2 := NewClient("foohost", conn2) + client1 := NewClient(conn1) + client2 := NewClient(conn2) go client1.Processor(events) go client2.Processor(events) - conn1.inbound <- "NICK nick1\r\nUSER foo1 bar1 baz1 :Long name1\r\n" - conn2.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2\r\n" + conn1.inbound <- "NICK nick1\r\nUSER foo1 bar1 baz1 :Long name1" + conn2.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2" for i := 0; i < 6; i++ { <-conn1.outbound <-conn2.outbound } - daemon.SendLusers(client1) + SendLusers(client1) if r := <-conn1.outbound; !strings.Contains(r, "There are 2 users") { t.Fatal("LUSERS", r) } conn1.inbound <- "WHOIS" - not_enough_params(t, conn1) + notEnoughParams(t, conn1) conn1.inbound <- "WHOIS nick3" - no_nickchan(t, conn1) + noNickchan(t, conn1) conn1.inbound <- "WHOIS nick2" if r := <-conn1.outbound; r != ":foohost 311 nick1 nick2 foo2 Unknown * :Long name2\r\n" { t.Fatal("first WHOIS 311", r) @@ -73,9 +102,9 @@ func TestTwoUsers(t *testing.T) { } conn1.inbound <- "WHO" - not_enough_params(t, conn1) + notEnoughParams(t, conn1) conn1.inbound <- "WHO #fooroom" - no_chan(t, conn1) + noChan(t, conn1) conn1.inbound <- "JOIN #foo" conn2.inbound <- "JOIN #foo" @@ -86,38 +115,53 @@ func TestTwoUsers(t *testing.T) { conn1.inbound <- "PRIVMSG nick2 :Hello" conn1.inbound <- "PRIVMSG #foo :world" conn1.inbound <- "NOTICE #foo :world" - if r := <-conn2.outbound; r != ":nick1!foo1@someclient PRIVMSG nick2 :Hello\r\n" { - t.Fatal("first message", r) + m1 := <-conn2.outbound + m2 := <-conn2.outbound + mNeeded := ":nick1!foo1@someclient PRIVMSG nick2 :Hello\r\n" + if !(m1 == mNeeded || m2 == mNeeded) { + t.Fatal("first message", m1, m2) + } + if m2 == mNeeded { + m2 = <-conn2.outbound } - if r := <-conn2.outbound; r != ":nick1!foo1@someclient PRIVMSG #foo :world\r\n" { - t.Fatal("second message", r) + if m2 != ":nick1!foo1@someclient PRIVMSG #foo :world\r\n" { + t.Fatal("second message", m2) } - if r := <-conn2.outbound; r != ":nick1!foo1@someclient NOTICE #foo :world\r\n" { - t.Fatal("third message", r) + if m2 = <-conn2.outbound; m2 != ":nick1!foo1@someclient NOTICE #foo :world\r\n" { + t.Fatal("third message", m2) } } func TestJoin(t *testing.T) { - log_sink := make(chan LogEvent, 8) - state_sink := make(chan StateEvent, 8) - daemon := NewDaemon("foohost", "", log_sink, state_sink) + logSink = make(chan LogEvent, 8) + stateSink = make(chan StateEvent, 8) + host := "foohost" + hostname = &host events := make(chan ClientEvent) - go daemon.Processor(events) + rooms = make(map[string]*Room) + clients = make(map[*Client]struct{}) + roomSinks = make(map[*Room]chan ClientEvent) + finished := make(chan struct{}) + go Processor(events, finished) + defer func() { + events <- ClientEvent{eventType: EventTerm} + <-finished + }() conn := NewTestingConn() - client := NewClient("foohost", conn) + client := NewClient(conn) go client.Processor(events) - conn.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2\r\n" + conn.inbound <- "NICK nick2\r\nUSER foo2 bar2 baz2 :Long name2" for i := 0; i < 6; i++ { <-conn.outbound } conn.inbound <- "JOIN" - not_enough_params(t, conn) + notEnoughParams(t, conn) conn.inbound <- "JOIN bla/bla/bla" - no_chan(t, conn) + noChan(t, conn) conn.inbound <- "JOIN bla:bla:bla" - no_chan(t, conn) + noChan(t, conn) conn.inbound <- "JOIN #foo" if r := <-conn.outbound; r != ":foohost 331 nick2 #foo :No topic is set\r\n" { @@ -132,7 +176,7 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":foohost 366 nick2 #foo :End of NAMES list\r\n" { t.Fatal("no end of NAMES list", r) } - if r := <-log_sink; (r.what != "joined") || (r.where != "#foo") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "joined") || (r.where != "#foo") || (r.who != "nick2") || (r.meta != true) { t.Fatal("invalid join log event", r) } @@ -140,16 +184,18 @@ func TestJoin(t *testing.T) { for i := 0; i < 4*2; i++ { <-conn.outbound } - if _, ok := daemon.rooms["#bar"]; !ok { + roomsM.RLock() + if _, ok := rooms["#bar"]; !ok { t.Fatal("#bar does not exist") } - if _, ok := daemon.rooms["#baz"]; !ok { + if _, ok := rooms["#baz"]; !ok { t.Fatal("#baz does not exist") } - if r := <-log_sink; (r.what != "joined") || (r.where != "#bar") || (r.who != "nick2") || (r.meta != true) { + roomsM.RUnlock() + if r := <-logSink; (r.what != "joined") || (r.where != "#bar") || (r.who != "nick2") || (r.meta != true) { t.Fatal("invalid join log event #bar", r) } - if r := <-log_sink; (r.what != "joined") || (r.where != "#baz") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "joined") || (r.where != "#baz") || (r.who != "nick2") || (r.meta != true) { t.Fatal("invalid join log event #baz", r) } @@ -157,22 +203,24 @@ func TestJoin(t *testing.T) { for i := 0; i < 4*2; i++ { <-conn.outbound } - if daemon.rooms["#barenc"].key != "key1" { + roomsM.RLock() + if *rooms["#barenc"].key != "key1" { t.Fatal("no room with key1") } - if daemon.rooms["#bazenc"].key != "key2" { + if *rooms["#bazenc"].key != "key2" { t.Fatal("no room with key2") } - if r := <-log_sink; (r.what != "joined") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { + roomsM.RUnlock() + if r := <-logSink; (r.what != "joined") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("invalid join log event #barenc", r) } - if r := <-log_sink; (r.what != "joined") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "joined") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("invalid join log event #bazenc", r) } - if r := <-state_sink; (r.topic != "") || (r.where != "#barenc") || (r.key != "key1") { + if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "key1") { t.Fatal("set channel key1 state", r) } - if r := <-state_sink; (r.topic != "") || (r.where != "#bazenc") || (r.key != "key2") { + if r := <-stateSink; (r.topic != "") || (r.where != "#bazenc") || (r.key != "key2") { t.Fatal("set channel key2 state", r) } @@ -180,13 +228,15 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":nick2!foo2@someclient MODE #barenc -k\r\n" { t.Fatal("remove #barenc key", r) } - if daemon.rooms["#barenc"].key != "" { + roomsM.RLock() + if *rooms["#barenc"].key != "" { t.Fatal("removing key from #barenc") } - if r := <-log_sink; (r.what != "removed channel key") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { + roomsM.RUnlock() + if r := <-logSink; (r.what != "removed channel key") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("removed channel key log", r) } - if r := <-state_sink; (r.topic != "") || (r.where != "#barenc") || (r.key != "") { + if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "") { t.Fatal("removed channel key state", r) } @@ -194,12 +244,12 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":foohost 442 #bazenc :You are not on that channel\r\n" { t.Fatal("not on that channel", r) } - if r := <-log_sink; (r.what != "left") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "left") || (r.where != "#bazenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("left #bazenc log", r) } - conn.inbound <- "MODE #barenc +b" - if r := <-conn.outbound; r != ":foohost 472 nick2 +b :Unknown MODE flag\r\n" { + conn.inbound <- "MODE #barenc +i" + if r := <-conn.outbound; r != ":foohost 472 nick2 +i :Unknown MODE flag\r\n" { t.Fatal("unknown MODE flag", r) } @@ -207,10 +257,10 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":nick2!foo2@someclient MODE #barenc +k newkey\r\n" { t.Fatal("+k MODE setting", r) } - if r := <-log_sink; (r.what != "set channel key to newkey") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "set channel key to newkey") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("set channel key", r) } - if r := <-state_sink; (r.topic != "") || (r.where != "#barenc") || (r.key != "newkey") { + if r := <-stateSink; (r.topic != "") || (r.where != "#barenc") || (r.key != "newkey") { t.Fatal("set channel newkey state", r) } @@ -218,10 +268,10 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":nick2!foo2@someclient TOPIC #barenc :New topic\r\n" { t.Fatal("set TOPIC", r) } - if r := <-log_sink; (r.what != "set topic to New topic") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { + if r := <-logSink; (r.what != "set topic to New topic") || (r.where != "#barenc") || (r.who != "nick2") || (r.meta != true) { t.Fatal("set TOPIC log", r) } - if r := <-state_sink; (r.topic != "New topic") || (r.where != "#barenc") || (r.key != "newkey") { + if r := <-stateSink; (r.topic != "New topic") || (r.where != "#barenc") || (r.key != "newkey") { t.Fatal("set channel TOPIC state", r) } @@ -232,5 +282,4 @@ func TestJoin(t *testing.T) { if r := <-conn.outbound; r != ":foohost 315 nick2 #barenc :End of /WHO list\r\n" { t.Fatal("end of WHO", r) } - }