blob: fcfde30706c3e6369eaade5e1c50f10343b2b07c [file] [log] [blame]
Ben Darnellbab01e82016-05-16 19:11:17 -04001/*
2 *
Jan Tattermuschddbf6c42017-06-08 14:42:19 +02003 * Copyright 2016 gRPC authors.
Ben Darnellbab01e82016-05-16 19:11:17 -04004 *
Jan Tattermuschddbf6c42017-06-08 14:42:19 +02005 * 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
Ben Darnellbab01e82016-05-16 19:11:17 -04008 *
Jan Tattermuschddbf6c42017-06-08 14:42:19 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Ben Darnellbab01e82016-05-16 19:11:17 -040010 *
Jan Tattermuschddbf6c42017-06-08 14:42:19 +020011 * 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.
Ben Darnellbab01e82016-05-16 19:11:17 -040016 *
17 */
18
19package grpc
20
21import (
Doug Fawley04ea8202018-11-12 13:30:41 -080022 "context"
Ben Darnellbab01e82016-05-16 19:11:17 -040023 "net"
Menghan Lie8268252016-06-09 16:19:18 -070024 "reflect"
Ben Darnellbab01e82016-05-16 19:11:17 -040025 "strings"
26 "testing"
Shangpeng Sun473b1422017-10-18 18:27:24 -040027 "time"
Menghan Lid46a3652017-09-07 14:30:05 -070028
dfawley11b58272018-07-11 11:22:45 -070029 "google.golang.org/grpc/internal/transport"
Ben Darnellbab01e82016-05-16 19:11:17 -040030)
31
Menghan Lie8268252016-06-09 16:19:18 -070032type emptyServiceServer interface{}
33
34type testServer struct{}
35
Doug Fawley0a391ff2019-01-07 14:24:56 -080036func (s) TestStopBeforeServe(t *testing.T) {
Ben Darnellbab01e82016-05-16 19:11:17 -040037 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()
Daniel Nephin4e393e02017-12-18 12:23:42 -050052 if got, want := errorDesc(err), "use of closed"; !strings.Contains(got, want) {
Ben Darnellbab01e82016-05-16 19:11:17 -040053 t.Errorf("Close() error = %q, want %q", got, want)
54 }
55}
Menghan Lie8268252016-06-09 16:19:18 -070056
Doug Fawley0a391ff2019-01-07 14:24:56 -080057func (s) TestGracefulStop(t *testing.T) {
Shangpeng Sun473b1422017-10-18 18:27:24 -040058
59 lis, err := net.Listen("tcp", "localhost:0")
60 if err != nil {
61 t.Fatalf("failed to create listener: %v", err)
62 }
63
64 server := NewServer()
65 go func() {
66 // make sure Serve() is called
67 time.Sleep(time.Millisecond * 500)
68 server.GracefulStop()
69 }()
70
71 err = server.Serve(lis)
72 if err != nil {
73 t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
74 }
75}
76
Doug Fawley0a391ff2019-01-07 14:24:56 -080077func (s) TestGetServiceInfo(t *testing.T) {
Menghan Li26d2db52016-06-23 16:37:55 -070078 testSd := ServiceDesc{
79 ServiceName: "grpc.testing.EmptyService",
Menghan Li451a2e42016-06-16 15:10:13 -070080 HandlerType: (*emptyServiceServer)(nil),
Menghan Li26d2db52016-06-23 16:37:55 -070081 Methods: []MethodDesc{
82 {
83 MethodName: "EmptyCall",
84 Handler: nil,
85 },
86 },
87 Streams: []StreamDesc{
88 {
89 StreamName: "EmptyStream",
90 Handler: nil,
Menghan Li0ea9f972016-07-06 11:47:40 -070091 ServerStreams: false,
Menghan Li26d2db52016-06-23 16:37:55 -070092 ClientStreams: true,
93 },
94 },
95 Metadata: []int{0, 2, 1, 3},
Menghan Li451a2e42016-06-16 15:10:13 -070096 }
Menghan Li26d2db52016-06-23 16:37:55 -070097
98 server := NewServer()
99 server.RegisterService(&testSd, &testServer{})
100
101 info := server.GetServiceInfo()
Tamir Dubersteinac90a022016-07-27 10:49:12 -0400102 want := map[string]ServiceInfo{
Tamir Duberstein9aa80382016-07-27 10:46:58 -0400103 "grpc.testing.EmptyService": {
Menghan Libc888562016-07-11 13:09:50 -0700104 Methods: []MethodInfo{
Tamir Duberstein9aa80382016-07-27 10:46:58 -0400105 {
Menghan Libc888562016-07-11 13:09:50 -0700106 Name: "EmptyCall",
107 IsClientStream: false,
108 IsServerStream: false,
109 },
Tamir Duberstein9aa80382016-07-27 10:46:58 -0400110 {
Menghan Libc888562016-07-11 13:09:50 -0700111 Name: "EmptyStream",
112 IsClientStream: true,
113 IsServerStream: false,
114 }},
Menghan Li26d2db52016-06-23 16:37:55 -0700115 Metadata: []int{0, 2, 1, 3},
116 },
117 }
118
119 if !reflect.DeepEqual(info, want) {
Tamir Dubersteinac90a022016-07-27 10:49:12 -0400120 t.Errorf("GetServiceInfo() = %+v, want %+v", info, want)
Menghan Li451a2e42016-06-16 15:10:13 -0700121 }
122}
Joshua Humphries57640c02018-03-20 20:02:32 -0400123
Doug Fawley0a391ff2019-01-07 14:24:56 -0800124func (s) TestStreamContext(t *testing.T) {
Joshua Humphries57640c02018-03-20 20:02:32 -0400125 expectedStream := &transport.Stream{}
126 ctx := NewContextWithServerTransportStream(context.Background(), expectedStream)
dfawleyfc37cf12018-04-26 17:38:15 -0700127 s := ServerTransportStreamFromContext(ctx)
Joshua Humphries57640c02018-03-20 20:02:32 -0400128 stream, ok := s.(*transport.Stream)
129 if !ok || expectedStream != stream {
130 t.Fatalf("GetStreamFromContext(%v) = %v, %t, want: %v, true", ctx, stream, ok, expectedStream)
131 }
132}