207 lines
6.1 KiB
Go
Raw Normal View History

2021-12-01 15:43:13 +00:00
//go:build !unit
// +build !unit
// run this test via :go test -tags=integration .
package nbhttp
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"sync"
"testing"
"time"
"github.com/lesismal/llib/std/crypto/tls"
"github.com/lesismal/nbio/nbhttp"
"github.com/lesismal/nbio/nbhttp/websocket"
)
var (
count = 3
chWait = make(chan struct{}, count)
text = "hello world"
addr = "localhost:8889"
svr *nbhttp.Server
)
func onWebsocket(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.NewUpgrader()
upgrader.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) {
c.WriteMessage(messageType, data)
c.SetReadDeadline(time.Now().Add(nbhttp.DefaultKeepaliveTime))
})
upgrader.OnClose(func(c *websocket.Conn, err error) {
fmt.Println("OnClose:", c.RemoteAddr().String(), err)
})
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
panic(err)
}
wsConn := conn.(*websocket.Conn)
fmt.Println("OnOpen:", wsConn.RemoteAddr().String())
}
func server(ctx context.Context, started *sync.WaitGroup, startupError *error) {
cert, err := tls.X509KeyPair(rsaCertPEM, rsaKeyPEM)
if err != nil {
log.Fatalf("tls.X509KeyPair failed: %v", err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
mux := &http.ServeMux{}
mux.HandleFunc("/wss", onWebsocket)
svr = nbhttp.NewServer(nbhttp.Config{
Network: "tcp",
AddrsTLS: []string{addr},
TLSConfig: tlsConfig,
Handler: mux,
})
err = svr.Start()
if err != nil {
fmt.Printf("nbio.Start failed: %v\n", err)
*startupError = err
started.Done()
return
}
started.Done()
defer svr.Stop()
<-ctx.Done()
log.Println("exit")
}
func client() error {
u := url.URL{Scheme: "wss", Host: addr, Path: "/wss"}
log.Printf("connecting to %s", u.String())
// engine := nbhttp.NewEngine(nbhttp.Config{
// TLSConfig: ,
// })
// err := engine.Start()
// if err != nil {
// fmt.Printf("nbio.Start failed: %v\n", err)
// return err
// }
dialer := &websocket.Dialer{
Engine: svr.Engine,
Upgrader: func() *websocket.Upgrader {
u := websocket.NewUpgrader()
u.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) {
c.WriteMessage(messageType, data)
chWait <- struct{}{}
})
return u
}(),
DialTimeout: time.Second * 3,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
c, res, err := dialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
if res.Body != nil {
res.Body.Close()
}
defer c.Close()
err = c.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
log.Fatalf("write: %v", err)
return err
}
for i := 0; i < count; i++ {
<-chWait
}
return nil
}
func TestServerSimulation(t *testing.T) {
waitGrp := sync.WaitGroup{}
ctx, cancelFunc := context.WithCancel(context.Background())
waitGrp.Add(1)
var err error
go server(ctx, &waitGrp, &err)
waitGrp.Wait()
if err != nil {
t.Errorf("client should not have errorred: %s", err)
cancelFunc()
return
}
err = client()
if err != nil {
t.Errorf("client should not have errorred: %s", err)
}
err = client()
if err != nil {
t.Errorf("client should not have errorred: %s", err)
}
cancelFunc()
}
var rsaCertPEM = []byte(`-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUJeohtgk8nnt8ofratXJg7kUJsI4wDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDEyMDcwODIyNThaFw0zMDEy
MDUwODIyNThaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCy+ZrIvwwiZv4bPmvKx/637ltZLwfgh3ouiEaTchGu
IQltthkqINHxFBqqJg44TUGH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