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

View File

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