Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | ddbf6c4 | 2017-06-08 14:42:19 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 4 | * |
Jan Tattermusch | ddbf6c4 | 2017-06-08 14:42:19 +0200 | [diff] [blame] | 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 |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 8 | * |
Jan Tattermusch | ddbf6c4 | 2017-06-08 14:42:19 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 10 | * |
Jan Tattermusch | ddbf6c4 | 2017-06-08 14:42:19 +0200 | [diff] [blame] | 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. |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | package grpc |
| 20 | |
| 21 | import ( |
Doug Fawley | 04ea820 | 2018-11-12 13:30:41 -0800 | [diff] [blame] | 22 | "context" |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 23 | "net" |
Menghan Li | e826825 | 2016-06-09 16:19:18 -0700 | [diff] [blame] | 24 | "reflect" |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 25 | "strings" |
| 26 | "testing" |
Shangpeng Sun | 473b142 | 2017-10-18 18:27:24 -0400 | [diff] [blame] | 27 | "time" |
Menghan Li | d46a365 | 2017-09-07 14:30:05 -0700 | [diff] [blame] | 28 | |
dfawley | 11b5827 | 2018-07-11 11:22:45 -0700 | [diff] [blame] | 29 | "google.golang.org/grpc/internal/transport" |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Menghan Li | e826825 | 2016-06-09 16:19:18 -0700 | [diff] [blame] | 32 | type emptyServiceServer interface{} |
| 33 | |
| 34 | type testServer struct{} |
| 35 | |
Doug Fawley | 0a391ff | 2019-01-07 14:24:56 -0800 | [diff] [blame] | 36 | func (s) TestStopBeforeServe(t *testing.T) { |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 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() |
Daniel Nephin | 4e393e0 | 2017-12-18 12:23:42 -0500 | [diff] [blame] | 52 | if got, want := errorDesc(err), "use of closed"; !strings.Contains(got, want) { |
Ben Darnell | bab01e8 | 2016-05-16 19:11:17 -0400 | [diff] [blame] | 53 | t.Errorf("Close() error = %q, want %q", got, want) |
| 54 | } |
| 55 | } |
Menghan Li | e826825 | 2016-06-09 16:19:18 -0700 | [diff] [blame] | 56 | |
Doug Fawley | 0a391ff | 2019-01-07 14:24:56 -0800 | [diff] [blame] | 57 | func (s) TestGracefulStop(t *testing.T) { |
Shangpeng Sun | 473b142 | 2017-10-18 18:27:24 -0400 | [diff] [blame] | 58 | |
| 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 Fawley | 0a391ff | 2019-01-07 14:24:56 -0800 | [diff] [blame] | 77 | func (s) TestGetServiceInfo(t *testing.T) { |
Menghan Li | 26d2db5 | 2016-06-23 16:37:55 -0700 | [diff] [blame] | 78 | testSd := ServiceDesc{ |
| 79 | ServiceName: "grpc.testing.EmptyService", |
Menghan Li | 451a2e4 | 2016-06-16 15:10:13 -0700 | [diff] [blame] | 80 | HandlerType: (*emptyServiceServer)(nil), |
Menghan Li | 26d2db5 | 2016-06-23 16:37:55 -0700 | [diff] [blame] | 81 | Methods: []MethodDesc{ |
| 82 | { |
| 83 | MethodName: "EmptyCall", |
| 84 | Handler: nil, |
| 85 | }, |
| 86 | }, |
| 87 | Streams: []StreamDesc{ |
| 88 | { |
| 89 | StreamName: "EmptyStream", |
| 90 | Handler: nil, |
Menghan Li | 0ea9f97 | 2016-07-06 11:47:40 -0700 | [diff] [blame] | 91 | ServerStreams: false, |
Menghan Li | 26d2db5 | 2016-06-23 16:37:55 -0700 | [diff] [blame] | 92 | ClientStreams: true, |
| 93 | }, |
| 94 | }, |
| 95 | Metadata: []int{0, 2, 1, 3}, |
Menghan Li | 451a2e4 | 2016-06-16 15:10:13 -0700 | [diff] [blame] | 96 | } |
Menghan Li | 26d2db5 | 2016-06-23 16:37:55 -0700 | [diff] [blame] | 97 | |
| 98 | server := NewServer() |
| 99 | server.RegisterService(&testSd, &testServer{}) |
| 100 | |
| 101 | info := server.GetServiceInfo() |
Tamir Duberstein | ac90a02 | 2016-07-27 10:49:12 -0400 | [diff] [blame] | 102 | want := map[string]ServiceInfo{ |
Tamir Duberstein | 9aa8038 | 2016-07-27 10:46:58 -0400 | [diff] [blame] | 103 | "grpc.testing.EmptyService": { |
Menghan Li | bc88856 | 2016-07-11 13:09:50 -0700 | [diff] [blame] | 104 | Methods: []MethodInfo{ |
Tamir Duberstein | 9aa8038 | 2016-07-27 10:46:58 -0400 | [diff] [blame] | 105 | { |
Menghan Li | bc88856 | 2016-07-11 13:09:50 -0700 | [diff] [blame] | 106 | Name: "EmptyCall", |
| 107 | IsClientStream: false, |
| 108 | IsServerStream: false, |
| 109 | }, |
Tamir Duberstein | 9aa8038 | 2016-07-27 10:46:58 -0400 | [diff] [blame] | 110 | { |
Menghan Li | bc88856 | 2016-07-11 13:09:50 -0700 | [diff] [blame] | 111 | Name: "EmptyStream", |
| 112 | IsClientStream: true, |
| 113 | IsServerStream: false, |
| 114 | }}, |
Menghan Li | 26d2db5 | 2016-06-23 16:37:55 -0700 | [diff] [blame] | 115 | Metadata: []int{0, 2, 1, 3}, |
| 116 | }, |
| 117 | } |
| 118 | |
| 119 | if !reflect.DeepEqual(info, want) { |
Tamir Duberstein | ac90a02 | 2016-07-27 10:49:12 -0400 | [diff] [blame] | 120 | t.Errorf("GetServiceInfo() = %+v, want %+v", info, want) |
Menghan Li | 451a2e4 | 2016-06-16 15:10:13 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
Joshua Humphries | 57640c0 | 2018-03-20 20:02:32 -0400 | [diff] [blame] | 123 | |
Doug Fawley | 0a391ff | 2019-01-07 14:24:56 -0800 | [diff] [blame] | 124 | func (s) TestStreamContext(t *testing.T) { |
Joshua Humphries | 57640c0 | 2018-03-20 20:02:32 -0400 | [diff] [blame] | 125 | expectedStream := &transport.Stream{} |
| 126 | ctx := NewContextWithServerTransportStream(context.Background(), expectedStream) |
dfawley | fc37cf1 | 2018-04-26 17:38:15 -0700 | [diff] [blame] | 127 | s := ServerTransportStreamFromContext(ctx) |
Joshua Humphries | 57640c0 | 2018-03-20 20:02:32 -0400 | [diff] [blame] | 128 | 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 | } |