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-10-27 01:24:35 +03:00
|
|
|
tcp "go.unistack.org/micro-server-tcp/v3"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2020-10-30 23:27:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testHandler struct {
|
2024-12-12 09:48:25 +03:00
|
|
|
done chan struct{}
|
|
|
|
cherr chan error
|
2020-10-30 23:27:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPServer(t *testing.T) {
|
2020-11-03 02:24:07 +03:00
|
|
|
ctx := context.Background()
|
2020-11-05 22:38:29 +03:00
|
|
|
|
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
|
2024-12-12 09:48:25 +03:00
|
|
|
h := &testHandler{cherr: make(chan error), done: make(chan struct{})}
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-08-01 12:51:38 +03:00
|
|
|
|
2024-12-12 09:48:25 +03:00
|
|
|
select {
|
|
|
|
case <-h.done:
|
|
|
|
break
|
|
|
|
case err := <-h.cherr:
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-01 12:51:38 +03:00
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-10-30 23:27:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-12-12 09:48:25 +03:00
|
|
|
h.cherr <- err
|
2020-10-30 23:27:33 +03:00
|
|
|
}
|
|
|
|
fmt.Printf("%s", buf[:n])
|
|
|
|
close(h.done)
|
|
|
|
}
|
|
|
|
}
|