2020-10-30 23:27:33 +03:00
|
|
|
package tcp_test
|
|
|
|
|
|
|
|
import (
|
2020-11-03 02:24:07 +03:00
|
|
|
"context"
|
2020-10-30 23:27:33 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-16 02:49:58 +03:00
|
|
|
tcp "github.com/unistack-org/micro-server-tcp/v3"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testHandler struct {
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPServer(t *testing.T) {
|
2020-11-03 02:24:07 +03:00
|
|
|
ctx := context.Background()
|
2020-11-05 22:38:29 +03:00
|
|
|
|
|
|
|
logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.TraceLevel))
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2020-11-05 22:38:29 +03:00
|
|
|
if err := reg.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
brk := broker.NewBroker(broker.Register(reg))
|
2020-11-05 22:38:29 +03:00
|
|
|
if err := brk.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-30 23:27:33 +03:00
|
|
|
// create server
|
2021-01-29 16:47:26 +03:00
|
|
|
srv := tcp.NewServer(server.Register(reg), server.Broker(brk), server.Address("127.0.0.1:65000"))
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
// create handler
|
|
|
|
h := &testHandler{done: make(chan struct{})}
|
|
|
|
|
|
|
|
// register handler
|
|
|
|
if err := srv.Handle(srv.NewHandler(h)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:38:29 +03:00
|
|
|
if err := srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-30 23:27:33 +03:00
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
2021-01-29 16:47:26 +03:00
|
|
|
service, err := reg.LookupService(ctx, server.DefaultName)
|
2020-10-30 23:27:33 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-h.done
|
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := net.DialTimeout("tcp", srv.Options().Address, 5*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
if _, err = c.Write([]byte("test")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testHandler) Serve(c net.Conn) {
|
|
|
|
var n int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
buf := make([]byte, 1024*8) // 8k buffer
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err = c.Read(buf)
|
|
|
|
if err != nil && err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2021-01-16 02:49:58 +03:00
|
|
|
logger.Fatal(context.TODO(), err.Error())
|
2020-10-30 23:27:33 +03:00
|
|
|
}
|
|
|
|
fmt.Printf("%s", buf[:n])
|
|
|
|
close(h.done)
|
|
|
|
}
|
|
|
|
}
|