HelloWorldTest.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using UnityEngine;
  17. using System.Threading.Tasks;
  18. using System;
  19. using Grpc.Core;
  20. using Helloworld;
  21. class HelloWorldTest
  22. {
  23. // Can be run from commandline.
  24. // Example command:
  25. // "/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -nographics -executeMethod HelloWorldTest.RunHelloWorld -logfile"
  26. public static void RunHelloWorld()
  27. {
  28. Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
  29. Debug.Log("==============================================================");
  30. Debug.Log("Starting tests");
  31. Debug.Log("==============================================================");
  32. Debug.Log("Application.platform: " + Application.platform);
  33. Debug.Log("Environment.OSVersion: " + Environment.OSVersion);
  34. var reply = Greet("Unity");
  35. Debug.Log("Greeting: " + reply.Message);
  36. Debug.Log("==============================================================");
  37. Debug.Log("Tests finished successfully.");
  38. Debug.Log("==============================================================");
  39. }
  40. public static HelloReply Greet(string greeting)
  41. {
  42. const int Port = 30051;
  43. Server server = new Server
  44. {
  45. Services = { Greeter.BindService(new GreeterImpl()) },
  46. Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
  47. };
  48. server.Start();
  49. Channel channel = new Channel("127.0.0.1:30051", ChannelCredentials.Insecure);
  50. var client = new Greeter.GreeterClient(channel);
  51. var reply = client.SayHello(new HelloRequest { Name = greeting });
  52. channel.ShutdownAsync().Wait();
  53. server.ShutdownAsync().Wait();
  54. return reply;
  55. }
  56. class GreeterImpl : Greeter.GreeterBase
  57. {
  58. // Server side handler of the SayHello RPC
  59. public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
  60. {
  61. return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
  62. }
  63. }
  64. }