mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-13 07:37:17 +03:00
A bit of logging framework, and channel test.
This commit is contained in:
parent
1652511bf2
commit
bf3cc264e6
@ -30,19 +30,21 @@ func (ch *Channel) Send(m Message) {
|
||||
func (ch *Channel) Join(u *User) error {
|
||||
err := ch.users.Add(u)
|
||||
if err != nil {
|
||||
s := fmt.Sprintf("%s joined. (Connected: %d)", u.Name(), ch.users.Len())
|
||||
ch.Send(*NewMessage(s))
|
||||
return err
|
||||
}
|
||||
return err
|
||||
s := fmt.Sprintf("%s joined. (Connected: %d)", u.Name(), ch.users.Len())
|
||||
ch.Send(*NewMessage(s))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) Leave(u *User) error {
|
||||
err := ch.users.Remove(u)
|
||||
if err != nil {
|
||||
s := fmt.Sprintf("%s left.", u.Name())
|
||||
ch.Send(*NewMessage(s))
|
||||
return err
|
||||
}
|
||||
return err
|
||||
s := fmt.Sprintf("%s left.", u.Name())
|
||||
ch.Send(*NewMessage(s))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) Topic() string {
|
||||
|
@ -8,20 +8,24 @@ import (
|
||||
func TestChannel(t *testing.T) {
|
||||
s := &MockScreen{}
|
||||
out := make(chan Message)
|
||||
defer close(out)
|
||||
|
||||
go func() {
|
||||
for msg := range out {
|
||||
t.Logf("Broadcasted: %s", msg.String())
|
||||
s.Write([]byte(msg.Render(nil)))
|
||||
}
|
||||
}()
|
||||
|
||||
u := NewUser("foo", s)
|
||||
ch := NewChannel("", out)
|
||||
ch.Join(u)
|
||||
err := ch.Join(u)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
expected := []byte(" * foo joined. (Connected: 1)")
|
||||
if !reflect.DeepEqual(s.received, expected) {
|
||||
t.Errorf("Got: %s, Expected: %s", s.received, expected)
|
||||
t.Errorf("Got: `%s`, Expected: `%s`", s.received, expected)
|
||||
}
|
||||
}
|
||||
|
13
chat/doc.go
Normal file
13
chat/doc.go
Normal file
@ -0,0 +1,13 @@
|
||||
/*
|
||||
`chat` package is a server-agnostic implementation of a chat interface, built
|
||||
with the intention of using with the intention of using as the backend for
|
||||
ssh-chat.
|
||||
|
||||
This package should not know anything about sockets. It should expose io-style
|
||||
interfaces and channels for communicating with any method of transnport.
|
||||
|
||||
TODO: Add usage examples here.
|
||||
|
||||
*/
|
||||
|
||||
package chat
|
22
chat/logger.go
Normal file
22
chat/logger.go
Normal file
@ -0,0 +1,22 @@
|
||||
package chat
|
||||
|
||||
import "io"
|
||||
import stdlog "log"
|
||||
|
||||
var logger *stdlog.Logger
|
||||
|
||||
func SetLogger(w io.Writer) {
|
||||
flags := stdlog.Flags()
|
||||
prefix := "[chat] "
|
||||
logger = stdlog.New(w, prefix, flags)
|
||||
}
|
||||
|
||||
type nullWriter struct{}
|
||||
|
||||
func (nullWriter) Write(data []byte) (int, error) {
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
SetLogger(nullWriter{})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user