Testing a gRPC service

前端 未结 8 501
旧巷少年郎
旧巷少年郎 2020-12-12 15:05

I\'d like to test a gRPC service written in Go. The example I\'m using is the Hello World server example from the grpc-go repo.

The protobuf definition is as follows

相关标签:
8条回答
  • 2020-12-12 15:27

    you can use karate-grpc to test grpc service, you only need to post your proto jar and grpc server ip/port. karate-grpc build based on karate and polyglot.

    One hello-world example:

    Feature: grpc helloworld example by grpc dynamic client
    
      Background:
        * def Client = Java.type('com.github.thinkerou.karate.GrpcClient')
        * def client = Client.create('localhost', 50051)
    
      Scenario: do it
        * def payload = read('helloworld.json')
        * def response = client.call('helloworld.Greeter/SayHello', payload)
        * def response = JSON.parse(response)
        * print response
        * match response[0].message == 'Hello thinkerou'
        * def message = response[0].message
    
        * def payload = read('again-helloworld.json')
        * def response = client.call('helloworld.Greeter/AgainSayHello', payload)
        * def response = JSON.parse(response)
        * match response[0].details == 'Details Hello thinkerou in BeiJing'
    

    About the example of karate-grpc comment:

    And it will generate beautiful report, like:

    More details please see: https://thinkerou.com/karate-grpc/

    0 讨论(0)
  • 2020-12-12 15:35

    BTW: as a new contributor, I cannot add to comments. So I am adding a new answer here.

    I can confirm that the @Omar approach works for testing a non-streaming gRPC service by testing via the interface without a running service.

    However this approach will not work for streams. Since gRPC supports bidirectional streams, it is necessary to fire-up the service and connected to it via the network layer to do testing for streams.

    The approach that @joscas takes works for gRPC streams (even though the helloworld sample code does not use streams) using a goroutine to start the service. However, I noticed that on Mac OS X 10.11.6 that it does not release the port used by the service consistently when called from a goroutine (As I understand, the service will block the goroutine and perhaps does not exit cleanly). By firing up a separate process for the service to run in, using 'exec.Command', and killing it before finishing, the port is released consistently.

    I uploaded a working test file for a gRPC service using streams to github: https://github.com/mmcc007/go/blob/master/examples/route_guide/server/server_test.go

    You can see the tests running on travis: https://travis-ci.org/mmcc007/go

    Please let me know if any suggestions on how to improve testing for gRPC services.

    0 讨论(0)
提交回复
热议问题