mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-02 07:58:31 +00:00
Add test that posts to /pub_msg, test infrastructure
This commit is contained in:
parent
730ce39f72
commit
0be3693c99
6 changed files with 193 additions and 62 deletions
|
@ -1,31 +1,152 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/satori/go.uuid"
|
||||
"golang.org/x/net/websocket"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func CountOpenFDs() uint64 {
|
||||
func TCountOpenFDs() uint64 {
|
||||
ary, _ := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid()))
|
||||
return uint64(len(ary))
|
||||
}
|
||||
|
||||
const IgnoreReceivedArguments = 1+2i
|
||||
func TReceiveExpectedMessage(tb testing.TB, conn *websocket.Conn, messageId int, command Command, arguments interface{}) (ClientMessage, bool) {
|
||||
var msg ClientMessage
|
||||
var fail bool
|
||||
err := FFZCodec.Receive(conn, &msg)
|
||||
if err != nil {
|
||||
tb.Error(err)
|
||||
return msg, false
|
||||
}
|
||||
if msg.MessageID != messageId {
|
||||
tb.Error("Message ID was wrong. Expected", messageId, ", got", msg.MessageID, ":", msg)
|
||||
fail = true
|
||||
}
|
||||
if msg.Command != command {
|
||||
tb.Error("Command was wrong. Expected", command, ", got", msg.Command, ":", msg)
|
||||
fail = true
|
||||
}
|
||||
if arguments != IgnoreReceivedArguments {
|
||||
if msg.Arguments != arguments {
|
||||
tb.Error("Arguments are wrong. Expected", arguments, ", got", msg.Arguments, ":", msg)
|
||||
}
|
||||
}
|
||||
return msg, !fail
|
||||
}
|
||||
|
||||
func TSendMessage(tb testing.TB, conn *websocket.Conn, messageId int, command Command, arguments interface{}) bool {
|
||||
err := FFZCodec.Send(conn, ClientMessage{MessageID: messageId, Command: command, Arguments: arguments})
|
||||
if err != nil {
|
||||
tb.Error(err)
|
||||
}
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestSubscriptionAndPublish(t *testing.T) {
|
||||
var doneWg sync.WaitGroup
|
||||
var readyWg sync.WaitGroup
|
||||
|
||||
const TestChannelName = "testchannel"
|
||||
const TestCommand = "testdata"
|
||||
const TestData = "123456789"
|
||||
|
||||
GenerateKeys("/tmp/test_naclkeys.json", "2", "+ZMqOmxhaVrCV5c0OMZ09QoSGcJHuqQtJrwzRD+JOjE=")
|
||||
DumpCache()
|
||||
conf := &Config{
|
||||
UseSSL: false,
|
||||
NaclKeysFile: "/tmp/test_naclkeys.json",
|
||||
SocketOrigin: "localhost:2002",
|
||||
}
|
||||
serveMux := http.NewServeMux()
|
||||
SetupServerAndHandle(conf, nil, serveMux)
|
||||
|
||||
server := httptest.NewUnstartedServer(serveMux)
|
||||
server.Start()
|
||||
|
||||
wsUrl := fmt.Sprintf("ws://%s/", server.Listener.Addr().String())
|
||||
originUrl := fmt.Sprintf("http://%s", server.Listener.Addr().String())
|
||||
publishUrl := fmt.Sprintf("http://%s/pub_msg", server.Listener.Addr().String())
|
||||
|
||||
conn, err := websocket.Dial(wsUrl, "", originUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
doneWg.Add(1)
|
||||
readyWg.Add(1)
|
||||
|
||||
go func(conn *websocket.Conn) {
|
||||
TSendMessage(t, conn, 1, HelloCommand, []interface{}{"ffz_0.0-test", uuid.NewV4().String()})
|
||||
TReceiveExpectedMessage(t, conn, 1, SuccessCommand, IgnoreReceivedArguments)
|
||||
TSendMessage(t, conn, 2, "sub", TestChannelName)
|
||||
TReceiveExpectedMessage(t, conn, 2, SuccessCommand, nil)
|
||||
|
||||
readyWg.Done()
|
||||
|
||||
TReceiveExpectedMessage(t, conn, -1, TestCommand, TestData)
|
||||
|
||||
conn.Close()
|
||||
doneWg.Done()
|
||||
}(conn)
|
||||
|
||||
readyWg.Wait()
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("cmd", TestCommand)
|
||||
argsBytes, _ := json.Marshal(TestData)
|
||||
form.Set("args", string(argsBytes))
|
||||
form.Set("channel", TestChannelName)
|
||||
form.Set("scope", MsgTargetTypeChat.Name())
|
||||
|
||||
sealedForm, err := SealRequest(form)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
server.CloseClientConnections()
|
||||
panic("halting test")
|
||||
}
|
||||
|
||||
resp, err := http.PostForm(publishUrl, sealedForm)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
server.CloseClientConnections()
|
||||
panic("halting test")
|
||||
}
|
||||
|
||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
respStr := string(respBytes)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Error("Publish failed: ", resp.StatusCode, respStr)
|
||||
server.CloseClientConnections()
|
||||
panic("halting test")
|
||||
}
|
||||
|
||||
doneWg.Wait()
|
||||
server.Close()
|
||||
}
|
||||
|
||||
func BenchmarkThousandUserSubscription(b *testing.B) {
|
||||
var doneWg sync.WaitGroup
|
||||
var readyWg sync.WaitGroup
|
||||
|
||||
const TestChannelName = "testchannel"
|
||||
const TestCommand = "testdata"
|
||||
const TestData = "123456789"
|
||||
|
||||
GenerateKeys("/tmp/test_naclkeys.json", "2", "+ZMqOmxhaVrCV5c0OMZ09QoSGcJHuqQtJrwzRD+JOjE=")
|
||||
DumpCache()
|
||||
conf := &Config{
|
||||
UseSSL: false,
|
||||
NaclKeysFile: "/tmp/test_naclkeys.json",
|
||||
|
@ -40,7 +161,7 @@ func BenchmarkThousandUserSubscription(b *testing.B) {
|
|||
wsUrl := fmt.Sprintf("ws://%s/", server.Listener.Addr().String())
|
||||
originUrl := fmt.Sprintf("http://%s", server.Listener.Addr().String())
|
||||
|
||||
message := ClientMessage{MessageID: -1, Command: "testdata", Arguments: "123456789"}
|
||||
message := ClientMessage{MessageID: -1, Command: "testdata", Arguments: TestData}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(b.N)
|
||||
|
@ -48,7 +169,7 @@ func BenchmarkThousandUserSubscription(b *testing.B) {
|
|||
var limit syscall.Rlimit
|
||||
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit)
|
||||
|
||||
limit.Cur = CountOpenFDs() + uint64(b.N)*2 + 100
|
||||
limit.Cur = TCountOpenFDs() + uint64(b.N)*2 + 100
|
||||
|
||||
if limit.Cur > limit.Max {
|
||||
b.Skip("Open file limit too low")
|
||||
|
@ -67,60 +188,17 @@ func BenchmarkThousandUserSubscription(b *testing.B) {
|
|||
doneWg.Add(1)
|
||||
readyWg.Add(1)
|
||||
go func(i int, conn *websocket.Conn) {
|
||||
var err error
|
||||
var msg ClientMessage
|
||||
err = FFZCodec.Send(conn, ClientMessage{MessageID: 1, Command: HelloCommand, Arguments: []interface{}{"ffz_test", uuid.NewV4().String()}})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
err = FFZCodec.Send(conn, ClientMessage{MessageID: 2, Command: "sub", Arguments: TestChannelName})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
err = FFZCodec.Receive(conn, &msg)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
if msg.MessageID != 1 {
|
||||
b.Error("Got out-of-order message ID", msg)
|
||||
}
|
||||
if msg.Command != SuccessCommand {
|
||||
b.Error("Command was not a success", msg)
|
||||
}
|
||||
err = FFZCodec.Receive(conn, &msg)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
if msg.MessageID != 2 {
|
||||
b.Error("Got out-of-order message ID", msg)
|
||||
}
|
||||
if msg.Command != SuccessCommand {
|
||||
b.Error("Command was not a success", msg)
|
||||
}
|
||||
TSendMessage(b, conn, 1, HelloCommand, []interface{}{"ffz_0.0-test", uuid.NewV4().String()})
|
||||
TSendMessage(b, conn, 2, "sub", TestChannelName)
|
||||
|
||||
TReceiveExpectedMessage(b, conn, 1, SuccessCommand, IgnoreReceivedArguments)
|
||||
TReceiveExpectedMessage(b, conn, 2, SuccessCommand, nil)
|
||||
|
||||
fmt.Println(i, " ready")
|
||||
readyWg.Done()
|
||||
|
||||
err = FFZCodec.Receive(conn, &msg)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
if msg.MessageID != -1 {
|
||||
fmt.Println(msg)
|
||||
b.Error("Client did not get expected messageID of -1")
|
||||
}
|
||||
if msg.Command != TestCommand {
|
||||
fmt.Println(msg)
|
||||
b.Error("Client did not get expected command")
|
||||
}
|
||||
str, err := msg.ArgumentsAsString()
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
if str != "123456789" {
|
||||
fmt.Println(msg)
|
||||
b.Error("Client did not get expected data")
|
||||
}
|
||||
TReceiveExpectedMessage(b, conn, -1, TestCommand, TestData)
|
||||
|
||||
conn.Close()
|
||||
doneWg.Done()
|
||||
}(i, conn)
|
||||
|
@ -131,7 +209,8 @@ func BenchmarkThousandUserSubscription(b *testing.B) {
|
|||
fmt.Println("publishing...")
|
||||
if PublishToChat(TestChannelName, message) != b.N {
|
||||
b.Error("not enough sent")
|
||||
b.FailNow()
|
||||
server.CloseClientConnections()
|
||||
panic("halting test instead of waiting")
|
||||
}
|
||||
doneWg.Wait()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue