server_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. package grpc
  19. import (
  20. "context"
  21. "net"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "time"
  26. "google.golang.org/grpc/internal/transport"
  27. )
  28. type emptyServiceServer interface{}
  29. type testServer struct{}
  30. func (s) TestStopBeforeServe(t *testing.T) {
  31. lis, err := net.Listen("tcp", "localhost:0")
  32. if err != nil {
  33. t.Fatalf("failed to create listener: %v", err)
  34. }
  35. server := NewServer()
  36. server.Stop()
  37. err = server.Serve(lis)
  38. if err != ErrServerStopped {
  39. t.Fatalf("server.Serve() error = %v, want %v", err, ErrServerStopped)
  40. }
  41. // server.Serve is responsible for closing the listener, even if the
  42. // server was already stopped.
  43. err = lis.Close()
  44. if got, want := errorDesc(err), "use of closed"; !strings.Contains(got, want) {
  45. t.Errorf("Close() error = %q, want %q", got, want)
  46. }
  47. }
  48. func (s) TestGracefulStop(t *testing.T) {
  49. lis, err := net.Listen("tcp", "localhost:0")
  50. if err != nil {
  51. t.Fatalf("failed to create listener: %v", err)
  52. }
  53. server := NewServer()
  54. go func() {
  55. // make sure Serve() is called
  56. time.Sleep(time.Millisecond * 500)
  57. server.GracefulStop()
  58. }()
  59. err = server.Serve(lis)
  60. if err != nil {
  61. t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
  62. }
  63. }
  64. func (s) TestGetServiceInfo(t *testing.T) {
  65. testSd := ServiceDesc{
  66. ServiceName: "grpc.testing.EmptyService",
  67. HandlerType: (*emptyServiceServer)(nil),
  68. Methods: []MethodDesc{
  69. {
  70. MethodName: "EmptyCall",
  71. Handler: nil,
  72. },
  73. },
  74. Streams: []StreamDesc{
  75. {
  76. StreamName: "EmptyStream",
  77. Handler: nil,
  78. ServerStreams: false,
  79. ClientStreams: true,
  80. },
  81. },
  82. Metadata: []int{0, 2, 1, 3},
  83. }
  84. server := NewServer()
  85. server.RegisterService(&testSd, &testServer{})
  86. info := server.GetServiceInfo()
  87. want := map[string]ServiceInfo{
  88. "grpc.testing.EmptyService": {
  89. Methods: []MethodInfo{
  90. {
  91. Name: "EmptyCall",
  92. IsClientStream: false,
  93. IsServerStream: false,
  94. },
  95. {
  96. Name: "EmptyStream",
  97. IsClientStream: true,
  98. IsServerStream: false,
  99. }},
  100. Metadata: []int{0, 2, 1, 3},
  101. },
  102. }
  103. if !reflect.DeepEqual(info, want) {
  104. t.Errorf("GetServiceInfo() = %+v, want %+v", info, want)
  105. }
  106. }
  107. func (s) TestStreamContext(t *testing.T) {
  108. expectedStream := &transport.Stream{}
  109. ctx := NewContextWithServerTransportStream(context.Background(), expectedStream)
  110. s := ServerTransportStreamFromContext(ctx)
  111. stream, ok := s.(*transport.Stream)
  112. if !ok || expectedStream != stream {
  113. t.Fatalf("GetStreamFromContext(%v) = %v, %t, want: %v, true", ctx, stream, ok, expectedStream)
  114. }
  115. }