fayec/test/client_test.go

137 lines
2.7 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"
"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 setup(t *testing.T) context.CancelFunc {
//jump to test dir
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
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 {
t.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) {
shutdown := setup(t)
2018-09-06 15:59:56 +00:00
defer shutdown()
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)
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
})
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(data message.Data) {
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++ {
id, err := client.Publish("/test", "hello world")
if err != nil {
t.Fatal(err)
}
log.Println(id, i)
}
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
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 := setup(t)
2018-09-06 15:59:56 +00:00
defer shutdown()
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
}