You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
741 B
43 lines
741 B
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type testKey struct{}
|
|
|
|
func TestServer(t *testing.T) {
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, testKey{}, "test")
|
|
srv := NewServer()
|
|
|
|
if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
|
|
t.Fatal(e, err)
|
|
}
|
|
|
|
go func() {
|
|
// start server
|
|
if err := srv.Start(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
time.Sleep(time.Second)
|
|
testClient(t, srv)
|
|
srv.Stop(ctx)
|
|
}
|
|
|
|
func testClient(t *testing.T, srv *Server) {
|
|
u, err := srv.Endpoint()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// new a gRPC client
|
|
conn, err := DialInsecure(context.Background(), WithEndpoint(u.Host))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Close()
|
|
}
|
|
|