You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
3.0 KiB

  1. /*
  2. Copyright 2016 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testutil
  14. import (
  15. "net"
  16. "strconv"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/codes"
  19. "google.golang.org/grpc/status"
  20. )
  21. // A Server is an in-process gRPC server, listening on a system-chosen port on
  22. // the local loopback interface. Servers are for testing only and are not
  23. // intended to be used in production code.
  24. //
  25. // To create a server, make a new Server, register your handlers, then call
  26. // Start:
  27. //
  28. // srv, err := NewServer()
  29. // ...
  30. // mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler)
  31. // ....
  32. // srv.Start()
  33. //
  34. // Clients should connect to the server with no security:
  35. //
  36. // conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
  37. // ...
  38. type Server struct {
  39. Addr string
  40. l net.Listener
  41. Gsrv *grpc.Server
  42. }
  43. // NewServer creates a new Server. The Server will be listening for gRPC connections
  44. // at the address named by the Addr field, without TLS.
  45. func NewServer(opts ...grpc.ServerOption) (*Server, error) {
  46. l, err := net.Listen("tcp", "127.0.0.1:0")
  47. if err != nil {
  48. return nil, err
  49. }
  50. s := &Server{
  51. Addr: l.Addr().String(),
  52. l: l,
  53. Gsrv: grpc.NewServer(opts...),
  54. }
  55. return s, nil
  56. }
  57. // Start causes the server to start accepting incoming connections.
  58. // Call Start after registering handlers.
  59. func (s *Server) Start() {
  60. go s.Gsrv.Serve(s.l)
  61. }
  62. // Close shuts down the server.
  63. func (s *Server) Close() {
  64. s.Gsrv.Stop()
  65. s.l.Close()
  66. }
  67. // PageBounds converts an incoming page size and token from an RPC request into
  68. // slice bounds and the outgoing next-page token.
  69. //
  70. // PageBounds assumes that the complete, unpaginated list of items exists as a
  71. // single slice. In addition to the page size and token, PageBounds needs the
  72. // length of that slice.
  73. //
  74. // PageBounds's first two return values should be used to construct a sub-slice of
  75. // the complete, unpaginated slice. E.g. if the complete slice is s, then
  76. // s[from:to] is the desired page. Its third return value should be set as the
  77. // NextPageToken field of the RPC response.
  78. func PageBounds(pageSize int, pageToken string, length int) (from, to int, nextPageToken string, err error) {
  79. from, to = 0, length
  80. if pageToken != "" {
  81. from, err = strconv.Atoi(pageToken)
  82. if err != nil {
  83. return 0, 0, "", status.Errorf(codes.InvalidArgument, "bad page token: %v", err)
  84. }
  85. if from >= length {
  86. return length, length, "", nil
  87. }
  88. }
  89. if pageSize > 0 && from+pageSize < length {
  90. to = from + pageSize
  91. nextPageToken = strconv.Itoa(to)
  92. }
  93. return from, to, nextPageToken, nil
  94. }