|
1 |
| -using UnityEngine; |
2 |
| -using System.Threading.Tasks; |
3 |
| -using System; |
4 |
| -using Grpc.Core; |
5 |
| -using Helloworld; |
6 |
| - |
7 |
| -class HelloWorldTest |
8 |
| -{ |
9 |
| - // Can be run from commandline. |
10 |
| - // Example command: |
11 |
| - // "/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -nographics -executeMethod HelloWorldTest.RunHelloWorld -logfile" |
12 |
| - public static void RunHelloWorld() |
13 |
| - { |
14 |
| - Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); |
15 |
| - |
16 |
| - Debug.Log("=============================================================="); |
17 |
| - Debug.Log("Starting tests"); |
18 |
| - Debug.Log("=============================================================="); |
19 |
| - |
20 |
| - Debug.Log("Application.platform: " + Application.platform); |
21 |
| - Debug.Log("Environment.OSVersion: " + Environment.OSVersion); |
22 |
| - |
23 |
| - var reply = Greet("Unity"); |
24 |
| - Debug.Log("Greeting: " + reply.Message); |
25 |
| - |
26 |
| - Debug.Log("=============================================================="); |
27 |
| - Debug.Log("Tests finished successfully."); |
28 |
| - Debug.Log("=============================================================="); |
29 |
| - } |
30 |
| - |
31 |
| - public static HelloReply Greet(string greeting) |
32 |
| - { |
33 |
| - const int Port = 50051; |
34 |
| - |
35 |
| - Server server = new Server |
36 |
| - { |
37 |
| - Services = { Greeter.BindService(new GreeterImpl()) }, |
38 |
| - Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } |
39 |
| - }; |
40 |
| - server.Start(); |
41 |
| - |
42 |
| - Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); |
43 |
| - |
44 |
| - var client = new Greeter.GreeterClient(channel); |
45 |
| - |
46 |
| - var reply = client.SayHello(new HelloRequest { Name = greeting }); |
47 |
| - |
48 |
| - channel.ShutdownAsync().Wait(); |
49 |
| - |
50 |
| - server.ShutdownAsync().Wait(); |
51 |
| - |
52 |
| - return reply; |
53 |
| - } |
54 |
| - |
55 |
| - class GreeterImpl : Greeter.GreeterBase |
56 |
| - { |
57 |
| - // Server side handler of the SayHello RPC |
58 |
| - public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) |
59 |
| - { |
60 |
| - return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); |
61 |
| - } |
62 |
| - } |
63 |
| -} |
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The 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 | +#endregion |
| 18 | + |
| 19 | +using UnityEngine; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using System; |
| 22 | +using Grpc.Core; |
| 23 | +using Helloworld; |
| 24 | + |
| 25 | +class HelloWorldTest |
| 26 | +{ |
| 27 | + // Can be run from commandline. |
| 28 | + // Example command: |
| 29 | + // "/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -nographics -executeMethod HelloWorldTest.RunHelloWorld -logfile" |
| 30 | + public static void RunHelloWorld() |
| 31 | + { |
| 32 | + Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None); |
| 33 | + |
| 34 | + Debug.Log("=============================================================="); |
| 35 | + Debug.Log("Starting tests"); |
| 36 | + Debug.Log("=============================================================="); |
| 37 | + |
| 38 | + Debug.Log("Application.platform: " + Application.platform); |
| 39 | + Debug.Log("Environment.OSVersion: " + Environment.OSVersion); |
| 40 | + |
| 41 | + var reply = Greet("Unity"); |
| 42 | + Debug.Log("Greeting: " + reply.Message); |
| 43 | + |
| 44 | + Debug.Log("=============================================================="); |
| 45 | + Debug.Log("Tests finished successfully."); |
| 46 | + Debug.Log("=============================================================="); |
| 47 | + } |
| 48 | + |
| 49 | + public static HelloReply Greet(string greeting) |
| 50 | + { |
| 51 | + const int Port = 50051; |
| 52 | + |
| 53 | + Server server = new Server |
| 54 | + { |
| 55 | + Services = { Greeter.BindService(new GreeterImpl()) }, |
| 56 | + Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } |
| 57 | + }; |
| 58 | + server.Start(); |
| 59 | + |
| 60 | + Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); |
| 61 | + |
| 62 | + var client = new Greeter.GreeterClient(channel); |
| 63 | + |
| 64 | + var reply = client.SayHello(new HelloRequest { Name = greeting }); |
| 65 | + |
| 66 | + channel.ShutdownAsync().Wait(); |
| 67 | + |
| 68 | + server.ShutdownAsync().Wait(); |
| 69 | + |
| 70 | + return reply; |
| 71 | + } |
| 72 | + |
| 73 | + class GreeterImpl : Greeter.GreeterBase |
| 74 | + { |
| 75 | + // Server side handler of the SayHello RPC |
| 76 | + public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) |
| 77 | + { |
| 78 | + return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments