OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / server_test.go
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 package grpc
20
21 import (
22         "net"
23         "reflect"
24         "strings"
25         "testing"
26         "time"
27
28         "google.golang.org/grpc/test/leakcheck"
29 )
30
31 type emptyServiceServer interface{}
32
33 type testServer struct{}
34
35 func TestStopBeforeServe(t *testing.T) {
36         defer leakcheck.Check(t)
37         lis, err := net.Listen("tcp", "localhost:0")
38         if err != nil {
39                 t.Fatalf("failed to create listener: %v", err)
40         }
41
42         server := NewServer()
43         server.Stop()
44         err = server.Serve(lis)
45         if err != ErrServerStopped {
46                 t.Fatalf("server.Serve() error = %v, want %v", err, ErrServerStopped)
47         }
48
49         // server.Serve is responsible for closing the listener, even if the
50         // server was already stopped.
51         err = lis.Close()
52         if got, want := ErrorDesc(err), "use of closed"; !strings.Contains(got, want) {
53                 t.Errorf("Close() error = %q, want %q", got, want)
54         }
55 }
56
57 func TestGracefulStop(t *testing.T) {
58         defer leakcheck.Check(t)
59
60         lis, err := net.Listen("tcp", "localhost:0")
61         if err != nil {
62                 t.Fatalf("failed to create listener: %v", err)
63         }
64
65         server := NewServer()
66         go func() {
67                 // make sure Serve() is called
68                 time.Sleep(time.Millisecond * 500)
69                 server.GracefulStop()
70         }()
71
72         err = server.Serve(lis)
73         if err != nil {
74                 t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
75         }
76 }
77
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{
84                         {
85                                 MethodName: "EmptyCall",
86                                 Handler:    nil,
87                         },
88                 },
89                 Streams: []StreamDesc{
90                         {
91                                 StreamName:    "EmptyStream",
92                                 Handler:       nil,
93                                 ServerStreams: false,
94                                 ClientStreams: true,
95                         },
96                 },
97                 Metadata: []int{0, 2, 1, 3},
98         }
99
100         server := NewServer()
101         server.RegisterService(&testSd, &testServer{})
102
103         info := server.GetServiceInfo()
104         want := map[string]ServiceInfo{
105                 "grpc.testing.EmptyService": {
106                         Methods: []MethodInfo{
107                                 {
108                                         Name:           "EmptyCall",
109                                         IsClientStream: false,
110                                         IsServerStream: false,
111                                 },
112                                 {
113                                         Name:           "EmptyStream",
114                                         IsClientStream: true,
115                                         IsServerStream: false,
116                                 }},
117                         Metadata: []int{0, 2, 1, 3},
118                 },
119         }
120
121         if !reflect.DeepEqual(info, want) {
122                 t.Errorf("GetServiceInfo() = %+v, want %+v", info, want)
123         }
124 }