server_test.go 3.2 KB

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