2017-10-25 18:42:44 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2018-08-24 15:36:20 +02:00
|
|
|
"sync"
|
2017-10-25 18:42:44 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-10-27 19:40:42 +02:00
|
|
|
"github.com/google/uuid"
|
2017-10-25 18:42:44 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMSGValidate(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2017-10-27 19:40:42 +02:00
|
|
|
|
2017-10-25 18:42:44 +02:00
|
|
|
msg := &Message{}
|
|
|
|
assert.False(msg.Validate())
|
2017-10-27 19:40:42 +02:00
|
|
|
|
2017-10-25 18:42:44 +02:00
|
|
|
msg.Subject = "login"
|
|
|
|
assert.False(msg.Validate())
|
|
|
|
|
|
|
|
msg.From = &Client{}
|
|
|
|
assert.True(msg.Validate())
|
|
|
|
|
|
|
|
msg.Subject = ""
|
|
|
|
assert.False(msg.Validate())
|
|
|
|
}
|
2017-10-27 19:40:42 +02:00
|
|
|
|
2018-08-24 15:36:20 +02:00
|
|
|
func TestMSGReplay(t *testing.T) {
|
2017-10-27 19:40:42 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
out := make(chan *Message, channelBufSize)
|
|
|
|
client := &Client{
|
|
|
|
id: uuid.New(),
|
|
|
|
out: out,
|
|
|
|
writeQuit: make(chan bool),
|
|
|
|
readQuit: make(chan bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
conversationID := uuid.New()
|
|
|
|
|
|
|
|
msg := &Message{
|
2018-08-24 15:36:20 +02:00
|
|
|
ID: conversationID,
|
|
|
|
Subject: "lola",
|
2017-10-27 19:40:42 +02:00
|
|
|
}
|
2018-08-24 15:36:20 +02:00
|
|
|
err := msg.Replay(nil)
|
|
|
|
assert.Error(err)
|
2017-10-27 19:40:42 +02:00
|
|
|
|
2018-08-24 15:36:20 +02:00
|
|
|
msg.From = client
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := msg.Replay("hi")
|
|
|
|
assert.NoError(err)
|
|
|
|
done <- true
|
|
|
|
}()
|
2017-10-27 19:40:42 +02:00
|
|
|
msg = <-out
|
2018-08-24 15:36:20 +02:00
|
|
|
<-done
|
2017-10-27 19:40:42 +02:00
|
|
|
|
|
|
|
assert.Equal(conversationID, msg.ID)
|
|
|
|
assert.Equal(uuid.Nil, msg.Session)
|
|
|
|
assert.Equal(client, msg.From)
|
2018-08-24 15:36:20 +02:00
|
|
|
assert.Equal("lola", msg.Subject)
|
|
|
|
assert.Equal("hi", msg.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMSGSession(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
srv := NewServer(nil, nil)
|
|
|
|
assert.NotNil(srv)
|
|
|
|
|
|
|
|
sessionID := uuid.New()
|
|
|
|
|
|
|
|
conversationID := uuid.New()
|
|
|
|
msg := &Message{
|
|
|
|
Session: sessionID,
|
|
|
|
ID: conversationID,
|
|
|
|
Subject: "lola",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := msg.ReplaySession("error")
|
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
msg.server = srv
|
|
|
|
err = msg.ReplaySession("error")
|
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
srv.sessionManager = NewSessionManager()
|
|
|
|
|
|
|
|
out1 := make(chan *Message, 3)
|
|
|
|
c1 := &Client{
|
|
|
|
id: uuid.New(),
|
|
|
|
out: out1,
|
|
|
|
server: srv,
|
|
|
|
}
|
|
|
|
|
|
|
|
out2 := make(chan *Message, 3)
|
|
|
|
c2 := &Client{
|
|
|
|
id: uuid.New(),
|
|
|
|
out: out2,
|
|
|
|
server: srv,
|
|
|
|
}
|
|
|
|
srv.addClient(c1)
|
|
|
|
srv.addClient(c2)
|
|
|
|
|
|
|
|
wgSession := sync.WaitGroup{}
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
client := func(out chan *Message) {
|
|
|
|
for msg := range out {
|
|
|
|
if msg.Subject == SessionMessageInit {
|
|
|
|
msg.ID = sessionID
|
|
|
|
msg.From.handleInput(msg)
|
|
|
|
wgSession.Done()
|
|
|
|
} else {
|
|
|
|
assert.Equal("lola", msg.Subject)
|
|
|
|
assert.Equal("hi", msg.Body)
|
|
|
|
assert.Equal(conversationID, msg.ID)
|
|
|
|
assert.Equal(sessionID, msg.Session)
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Add(2)
|
|
|
|
wgSession.Add(2)
|
|
|
|
go client(out1)
|
|
|
|
go client(out2)
|
|
|
|
wgSession.Wait()
|
|
|
|
|
|
|
|
err = msg.ReplaySession("hi")
|
|
|
|
assert.NoError(err)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
srv.delClient(c2)
|
|
|
|
srv.delClient(c1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMSGEverbody(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
srv := NewServer(nil, nil)
|
|
|
|
assert.NotNil(srv)
|
|
|
|
|
|
|
|
out1 := make(chan *Message, 2)
|
|
|
|
c1 := &Client{
|
|
|
|
id: uuid.New(),
|
|
|
|
out: out1,
|
|
|
|
server: srv,
|
|
|
|
}
|
|
|
|
|
|
|
|
out2 := make(chan *Message, 2)
|
|
|
|
c2 := &Client{
|
|
|
|
id: uuid.New(),
|
|
|
|
out: out2,
|
|
|
|
server: srv,
|
|
|
|
}
|
|
|
|
srv.addClient(c1)
|
|
|
|
srv.addClient(c2)
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
conversationID := uuid.New()
|
|
|
|
msg := &Message{
|
|
|
|
ID: conversationID,
|
|
|
|
Subject: "lola",
|
|
|
|
}
|
|
|
|
err := msg.ReplayEverybody("error")
|
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
client := func(out chan *Message) {
|
|
|
|
msg := <-out
|
|
|
|
assert.Equal("lola", msg.Subject)
|
|
|
|
assert.Equal("hi", msg.Body)
|
|
|
|
assert.Equal(conversationID, msg.ID)
|
|
|
|
assert.Equal(uuid.Nil, msg.Session)
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
wg.Add(2)
|
|
|
|
go client(out1)
|
|
|
|
go client(out2)
|
|
|
|
|
|
|
|
msg.server = srv
|
|
|
|
err = msg.ReplayEverybody("hi")
|
|
|
|
assert.NoError(err)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
srv.delClient(c2)
|
|
|
|
srv.delClient(c1)
|
2017-10-27 19:40:42 +02:00
|
|
|
}
|