3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 "google.golang.org/grpc/test/leakcheck"
31 type emptyServiceServer interface{}
33 type testServer struct{}
35 func TestStopBeforeServe(t *testing.T) {
36 defer leakcheck.Check(t)
37 lis, err := net.Listen("tcp", "localhost:0")
39 t.Fatalf("failed to create listener: %v", err)
44 err = server.Serve(lis)
45 if err != ErrServerStopped {
46 t.Fatalf("server.Serve() error = %v, want %v", err, ErrServerStopped)
49 // server.Serve is responsible for closing the listener, even if the
50 // server was already stopped.
52 if got, want := ErrorDesc(err), "use of closed"; !strings.Contains(got, want) {
53 t.Errorf("Close() error = %q, want %q", got, want)
57 func TestGracefulStop(t *testing.T) {
58 defer leakcheck.Check(t)
60 lis, err := net.Listen("tcp", "localhost:0")
62 t.Fatalf("failed to create listener: %v", err)
67 // make sure Serve() is called
68 time.Sleep(time.Millisecond * 500)
72 err = server.Serve(lis)
74 t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
78 func TestGetServiceInfo(t *testing.T) {
79 defer leakcheck.Check(t)
80 testSd := ServiceDesc{
81 ServiceName: "grpc.testing.EmptyService",
82 HandlerType: (*emptyServiceServer)(nil),
83 Methods: []MethodDesc{
85 MethodName: "EmptyCall",
89 Streams: []StreamDesc{
91 StreamName: "EmptyStream",
97 Metadata: []int{0, 2, 1, 3},
100 server := NewServer()
101 server.RegisterService(&testSd, &testServer{})
103 info := server.GetServiceInfo()
104 want := map[string]ServiceInfo{
105 "grpc.testing.EmptyService": {
106 Methods: []MethodInfo{
109 IsClientStream: false,
110 IsServerStream: false,
114 IsClientStream: true,
115 IsServerStream: false,
117 Metadata: []int{0, 2, 1, 3},
121 if !reflect.DeepEqual(info, want) {
122 t.Errorf("GetServiceInfo() = %+v, want %+v", info, want)