Tests pass.

This commit is contained in:
Andrey Petrov 2014-12-26 12:20:44 -08:00
parent 5dad20d241
commit 999e1919e7
2 changed files with 12 additions and 8 deletions

View File

@ -19,7 +19,7 @@ type Channel struct {
broadcast chan Message
commands Commands
closed bool
closeOnce *sync.Once
closeOnce sync.Once
}
// Create new channel and start broadcasting goroutine.
@ -34,6 +34,7 @@ func NewChannel() *Channel {
}
}
// Close the channel and all the users it contains.
func (ch *Channel) Close() {
ch.closeOnce.Do(func() {
ch.closed = true
@ -47,6 +48,7 @@ func (ch *Channel) Close() {
// Handle a message, will block until done.
func (ch *Channel) handleMsg(m Message) {
logger.Printf("ch.handleMsg(%v)", m)
switch m := m.(type) {
case *CommandMsg:
cmd := *m
@ -88,10 +90,12 @@ func (ch *Channel) Serve() {
}
}
// Send message, buffered by a chan.
func (ch *Channel) Send(m Message) {
ch.broadcast <- m
}
// Join the channel as a user, will announce.
func (ch *Channel) Join(u *User) error {
if ch.closed {
return ErrChannelClosed
@ -105,6 +109,7 @@ func (ch *Channel) Join(u *User) error {
return nil
}
// Leave the channel as a user, will announce.
func (ch *Channel) Leave(u *User) error {
err := ch.users.Remove(u)
if err != nil {
@ -115,10 +120,12 @@ func (ch *Channel) Leave(u *User) error {
return nil
}
// Topic of the channel.
func (ch *Channel) Topic() string {
return ch.topic
}
// SetTopic will set the topic of the channel.
func (ch *Channel) SetTopic(s string) {
ch.topic = s
}

View File

@ -1,6 +1,7 @@
package chat
import (
"os"
"reflect"
"testing"
)
@ -21,10 +22,13 @@ func TestChannelServe(t *testing.T) {
func TestChannelJoin(t *testing.T) {
var expected, actual []byte
SetLogger(os.Stderr)
s := &MockScreen{}
u := NewUser("foo")
ch := NewChannel()
go ch.Serve()
defer ch.Close()
err := ch.Join(u)
@ -32,12 +36,6 @@ func TestChannelJoin(t *testing.T) {
t.Fatal(err)
}
m := <-ch.broadcast
if m.(*AnnounceMsg) == nil {
t.Fatal("Did not receive correct msg: %v", m)
}
ch.handleMsg(m)
u.ConsumeOne(s)
expected = []byte(" * foo joined. (Connected: 1)" + Newline)
s.Read(&actual)
@ -46,7 +44,6 @@ func TestChannelJoin(t *testing.T) {
}
ch.Send(NewSystemMsg("hello", u))
u.ConsumeOne(s)
expected = []byte("-> hello" + Newline)
s.Read(&actual)