|
1 | 1 | defmodule Util.GrpcXTest do
|
2 | 2 | use ExUnit.Case
|
| 3 | + import Mock |
3 | 4 |
|
4 | 5 | alias Util.GrpcX
|
5 | 6 |
|
6 | 7 | setup_all do
|
7 |
| - server_pid = start_helloworld_server() |
8 |
| - clients_pid = start_clients() |
| 8 | + {:ok, server_pid} = start_helloworld_server() |
| 9 | + {:ok, clients_pid} = start_clients() |
9 | 10 |
|
10 | 11 | on_exit(fn ->
|
11 | 12 | Process.exit(server_pid, :kill)
|
12 | 13 | Process.exit(clients_pid, :kill)
|
13 | 14 | end)
|
14 | 15 | end
|
15 | 16 |
|
16 |
| - test "connecting to an existing services works and returns an {:ok, reply} tuple" do |
| 17 | + test "it can connect to existing services, result is in form {:ok, reply}" do |
17 | 18 | req = Helloworld.HelloRequest.new(name: "shiroyasha")
|
18 | 19 |
|
19 | 20 | assert {:ok, reply} = GrpcX.call(:hello_service, :say_hello, req)
|
20 | 21 | assert reply.message == "Hello shiroyasha"
|
21 | 22 | end
|
22 | 23 |
|
| 24 | + test "it raises an error if you pass an unknown service name" do |
| 25 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 26 | + |
| 27 | + assert {:error, err} = GrpcX.call(:hellooooooo, :say_hello, req) |
| 28 | + assert err == "GrpcX client with name='hellooooooo' not registered in GrpcX" |
| 29 | + end |
| 30 | + |
| 31 | + test "it raises an error if you request an unknown rpc method" do |
| 32 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 33 | + |
| 34 | + assert {:error, {:unknown_rpc, err}} = GrpcX.call(:hello_service, :describe, req) |
| 35 | + |
| 36 | + assert err == "no RPC method named='describe'" |
| 37 | + end |
| 38 | + |
| 39 | + test "it timeouts long calls" do |
| 40 | + req = Helloworld.HelloRequest.new(name: "please take a long time") |
| 41 | + |
| 42 | + assert {:error, err} = GrpcX.call(:hello_service, :say_hello, req, timeout: 500) |
| 43 | + assert err == %GRPC.RPCError{message: "Deadline expired", status: 4} |
| 44 | + end |
| 45 | + |
| 46 | + test "it reports connection errors" do |
| 47 | + req = Helloworld.HelloRequest.new(name: "please take a long time") |
| 48 | + |
| 49 | + assert {:error, err} = GrpcX.call(:not_running_service, :say_hello, req) |
| 50 | + assert err == "Error when opening connection: :timeout" |
| 51 | + end |
| 52 | + |
| 53 | + describe "when metrics are disabled" do |
| 54 | + test_with_mock "no increments are submitted", Watchman, [:passthrough], [] do |
| 55 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 56 | + GrpcX.call(:hello_service_not_measured, :say_hello, req) |
| 57 | + |
| 58 | + assert_not_called(Watchman.increment(:_)) |
| 59 | + end |
| 60 | + |
| 61 | + test_with_mock "no benchmarks are submitted", Watchman, [:passthrough], [] do |
| 62 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 63 | + GrpcX.call(:hello_service_not_measured, :say_hello, req) |
| 64 | + |
| 65 | + assert_not_called(Watchman.benchmark(:_, :_)) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "when metrics are enabled" do |
| 70 | + test_with_mock "it measures number of connections", Watchman, [:passthrough], [] do |
| 71 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 72 | + GrpcX.call(:hello_service, :say_hello, req) |
| 73 | + |
| 74 | + assert_called(Watchman.increment("grpc.hello_service.say_hello.connect.count")) |
| 75 | + end |
| 76 | + |
| 77 | + test_with_mock "it measures number of requests", Watchman, [:passthrough], [] do |
| 78 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 79 | + GrpcX.call(:hello_service, :say_hello, req) |
| 80 | + |
| 81 | + assert_called(Watchman.increment("grpc.hello_service.say_hello.request.count")) |
| 82 | + end |
| 83 | + |
| 84 | + test_with_mock "it measures connection failures", Watchman, [:passthrough], [] do |
| 85 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 86 | + GrpcX.call(:not_running_service, :say_hello, req) |
| 87 | + |
| 88 | + assert_called(Watchman.increment("grpc.not_running_service.say_hello.connect.error.count")) |
| 89 | + end |
| 90 | + |
| 91 | + test_with_mock "it measures response successes", Watchman, [:passthrough], [] do |
| 92 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 93 | + GrpcX.call(:hello_service, :say_hello, req) |
| 94 | + |
| 95 | + assert_called(Watchman.increment("grpc.hello_service.say_hello.response.success.count")) |
| 96 | + end |
| 97 | + |
| 98 | + test_with_mock "it measures response errors", Watchman, [:passthrough], [] do |
| 99 | + req = Helloworld.HelloRequest.new(name: "please fail") |
| 100 | + GrpcX.call(:hello_service, :say_hello, req) |
| 101 | + |
| 102 | + assert_called(Watchman.increment("grpc.hello_service.say_hello.response.error.count")) |
| 103 | + end |
| 104 | + |
| 105 | + test_with_mock "it measures the duration of the rpc call", Watchman, [:passthrough], [] do |
| 106 | + req = Helloworld.HelloRequest.new(name: "shiroyasha") |
| 107 | + GrpcX.call(:hello_service, :say_hello, req) |
| 108 | + |
| 109 | + assert_called(Watchman.benchmark("grpc.hello_service.say_hello.duration", :_)) |
| 110 | + end |
| 111 | + end |
| 112 | + |
23 | 113 | def start_helloworld_server() do
|
24 |
| - spawn_link(fn -> |
25 |
| - GRPC.Server.start(Helloworld.Greeter.Server, 50_051) |
26 |
| - end) |
| 114 | + {:ok, pid, _} = GRPC.Server.start(Helloworld.Greeter.Server, 50_052) |
| 115 | + {:ok, pid} |
27 | 116 | end
|
28 | 117 |
|
29 | 118 | def start_clients() do
|
30 | 119 | clients = [
|
31 |
| - Util.GrpcX.Client.new(:hello_service, "localhost:50051", Helloworld.Greeter.Stub) |
| 120 | + Util.GrpcX.Client.new(:hello_service, "localhost:50052", Helloworld.Greeter.Stub), |
| 121 | + Util.GrpcX.Client.new(:not_running_service, "localhost:60000", Helloworld.Greeter.Stub), |
| 122 | + Util.GrpcX.Client.new( |
| 123 | + :hello_service_not_measured, |
| 124 | + "localhost:50052", |
| 125 | + Helloworld.Greeter.Stub, |
| 126 | + publish_metrics: false |
| 127 | + ) |
32 | 128 | ]
|
33 | 129 |
|
34 |
| - {:ok, client_pid} = Util.GrpcX.start_link(clients) |
| 130 | + Util.GrpcX.start_link(clients) |
35 | 131 | end
|
36 | 132 | end
|
0 commit comments