fayec/test/client_test.go

185 lines
3.4 KiB
Go
Raw Normal View History

package test
2018-09-06 15:59:56 +00:00
import (
"context"
"fmt"
2018-09-06 16:40:00 +00:00
"github.com/pkg/errors"
. "github.com/thesyncim/faye"
2018-09-06 15:59:56 +00:00
"github.com/thesyncim/faye/extensions"
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/subscription"
2018-09-06 15:59:56 +00:00
"log"
"os"
"os/exec"
"reflect"
"runtime"
2018-09-06 15:59:56 +00:00
"sync"
"testing"
"time"
)
var once sync.Once
2018-09-06 15:59:56 +00:00
2018-09-06 16:40:00 +00:00
var unauthorizedErr = errors.New("500::unauthorized channel")
func init() {
setup(nil)
}
func setup(t *testing.T) context.CancelFunc {
//jump to test dir
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
cmd := exec.CommandContext(ctx,
"npm", "start")
2018-09-06 15:59:56 +00:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
log.Fatal(err)
2018-09-06 15:59:56 +00:00
}
var mcancel context.CancelFunc = func() {
2018-09-06 15:59:56 +00:00
if runtime.GOOS == "windows" {
exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprint(cmd.Process.Pid)).Run()
2018-09-06 15:59:56 +00:00
}
cancel()
}
2018-09-06 15:59:56 +00:00
return mcancel
2018-09-06 15:59:56 +00:00
}
2018-09-06 16:40:00 +00:00
func TestServerSubscribeAndPublish10Messages(t *testing.T) {
2018-09-06 15:59:56 +00:00
debug := extensions.NewDebugExtension(os.Stdout)
client, err := NewClient("ws://localhost:8000/faye", WithExtension(debug.InExtension, debug.OutExtension))
2018-09-06 15:59:56 +00:00
if err != nil {
t.Fatal(err)
}
2018-09-06 16:40:00 +00:00
var delivered int
var done sync.WaitGroup
done.Add(10)
var sub *subscription.Subscription
2018-09-06 15:59:56 +00:00
go func() {
sub, err = client.Subscribe("/test")
if err != nil {
t.Fatal(err)
}
err = sub.OnMessage(func(channel string, data message.Data) {
delivered++
done.Done()
2018-09-06 15:59:56 +00:00
if data != "hello world" {
t.Fatalf("expecting: `hello world` got : %s", data)
}
})
if err != nil {
t.Fatal(err)
}
2018-09-06 15:59:56 +00:00
}()
//give some time for setup
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
err := client.Publish("/test", "hello world")
2018-09-06 15:59:56 +00:00
if err != nil {
t.Fatal(err)
}
2018-09-06 15:59:56 +00:00
}
done.Wait()
err = sub.Unsubscribe()
2018-09-06 15:59:56 +00:00
if err != nil {
t.Fatal(err)
}
2018-09-06 16:40:00 +00:00
//try to publish one more message
err = client.Publish("/test", "hello world")
2018-09-06 16:40:00 +00:00
if err != nil {
t.Fatal(err)
}
2018-09-06 15:59:56 +00:00
if delivered != 10 {
t.Fatal("message received after client unsubscribe")
}
}
func TestSubscribeUnauthorizedChannel(t *testing.T) {
debug := extensions.NewDebugExtension(os.Stdout)
client, err := NewClient("ws://localhost:8000/faye", WithExtension(debug.InExtension, debug.OutExtension))
2018-09-06 15:59:56 +00:00
if err != nil {
t.Fatal(err)
}
_, err = client.Subscribe("/unauthorized")
if err != nil {
if err.Error() != unauthorizedErr.Error() {
t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error())
}
return
2018-09-06 15:59:56 +00:00
}
t.Fatal("expecting error")
2018-09-06 15:59:56 +00:00
}
func TestWildcardSubscription(t *testing.T) {
debug := extensions.NewDebugExtension(os.Stdout)
client, err := NewClient("ws://localhost:8000/faye", WithExtension(debug.InExtension, debug.OutExtension))
if err != nil {
t.Fatal(err)
}
var done sync.WaitGroup
done.Add(20)
sub, err := client.Subscribe("/wildcard/**")
if err != nil {
t.Fatal(err)
}
var recv = map[string]int{}
go func() {
err := sub.OnMessage(func(channel string, msg message.Data) {
done.Done()
recv[channel]++
})
if err != nil {
t.Fatal(err)
}
}()
//give some time for setup
time.Sleep(time.Second)
for _, channel := range []string{"/wildcard/foo", "/wildcard/bar"} {
for i := 0; i < 10; i++ {
err := client.Publish(channel, "hello world")
if err != nil {
t.Fatal(err)
}
}
}
done.Wait()
expected := map[string]int{
"/wildcard/foo": 10,
"/wildcard/bar": 10}
if !reflect.DeepEqual(recv, expected) {
t.Fatal(recv)
}
}