fayec/test/setup_test.go

128 lines
2.5 KiB
Go
Raw Normal View History

2018-09-06 15:59:56 +00:00
package faye_test
import (
2018-09-06 16:40:00 +00:00
"github.com/pkg/errors"
2018-09-06 15:59:56 +00:00
"github.com/thesyncim/faye"
"github.com/thesyncim/faye/extensions"
"github.com/thesyncim/faye/message"
"log"
"os"
"os/exec"
"sync"
"testing"
"time"
)
type cancelFn func()
2018-09-06 16:40:00 +00:00
var unauthorizedErr = errors.New("500::unauthorized channel")
2018-09-06 15:59:56 +00:00
func setup(t *testing.T) (cancelFn, error) {
cmd := exec.Command("npm", "start")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return nil, err
}
var cancel = func() {
2018-09-06 16:40:00 +00:00
cmd.Process.Signal(os.Kill)
2018-09-06 15:59:56 +00:00
log.Println("canceled")
}
go func() {
select {
case <-time.After(time.Second * 30):
cancel()
2018-09-06 16:40:00 +00:00
t.Fatal("test failed")
2018-09-06 15:59:56 +00:00
os.Exit(1)
}
}()
return cancel, nil
}
2018-09-06 16:40:00 +00:00
func TestServerSubscribeAndPublish10Messages(t *testing.T) {
2018-09-06 15:59:56 +00:00
shutdown, err := setup(t)
if err != nil {
t.Fatal(err)
}
defer shutdown()
debug := extensions.NewDebugExtension(os.Stdout)
client, err := fayec.NewClient("ws://localhost:8000/faye", fayec.WithExtension(debug.InExtension, debug.OutExtension))
if err != nil {
t.Fatal(err)
}
2018-09-06 16:40:00 +00:00
var delivered int
var done sync.WaitGroup
done.Add(10)
2018-09-06 15:59:56 +00:00
client.OnPublishResponse("/test", func(message *message.Message) {
if !message.Successful {
t.Fatalf("failed to send message with id %s", message.Id)
}
2018-09-06 16:40:00 +00:00
delivered++
done.Done()
2018-09-06 15:59:56 +00:00
})
2018-09-06 16:40:00 +00:00
2018-09-06 15:59:56 +00:00
go func() {
client.Subscribe("/test", func(data message.Data) {
if data != "hello world" {
t.Fatalf("expecting: `hello world` got : %s", data)
}
})
}()
//give some time for setup
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
id, err := client.Publish("/test", "hello world")
if err != nil {
t.Fatal(err)
}
log.Println(id, i)
}
done.Wait()
err = client.Unsubscribe("/test")
if err != nil {
t.Fatal(err)
}
2018-09-06 16:40:00 +00:00
//try to publish one more message
id, err := client.Publish("/test", "hello world")
if err != nil {
t.Fatal(err)
}
log.Println(id)
2018-09-06 15:59:56 +00:00
if delivered != 10 {
t.Fatal("message received after client unsubscribe")
}
}
func TestSubscribeUnauthorizedChannel(t *testing.T) {
shutdown, err := setup(t)
if err != nil {
t.Fatal(err)
}
defer shutdown()
debug := extensions.NewDebugExtension(os.Stdout)
client, err := fayec.NewClient("ws://localhost:8000/faye", fayec.WithExtension(debug.InExtension, debug.OutExtension))
if err != nil {
t.Fatal(err)
}
err = client.Subscribe("/unauthorized", func(data message.Data) {
t.Fatal("received message on unauthorized channel")
})
2018-09-06 16:40:00 +00:00
if err.Error() != unauthorizedErr.Error() {
t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error())
2018-09-06 15:59:56 +00:00
}
}