Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | #endregion |
| 33 | |
| 34 | using System; |
| 35 | using System.Collections.Generic; |
| 36 | using System.Diagnostics; |
| 37 | using System.IO; |
| 38 | using System.Linq; |
| 39 | using System.Text.RegularExpressions; |
| 40 | using System.Threading.Tasks; |
| 41 | |
| 42 | using CommandLine; |
| 43 | using CommandLine.Text; |
| 44 | using Grpc.Core; |
| 45 | using Grpc.Core.Utils; |
| 46 | using Grpc.Testing; |
| 47 | using NUnit.Framework; |
| 48 | |
| 49 | namespace Grpc.IntegrationTesting |
| 50 | { |
| 51 | public class QpsWorker |
| 52 | { |
| 53 | private class ServerOptions |
| 54 | { |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 55 | [Option("driver_port", Default = 0)] |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 56 | public int DriverPort { get; set; } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ServerOptions options; |
| 60 | |
| 61 | private QpsWorker(ServerOptions options) |
| 62 | { |
| 63 | this.options = options; |
| 64 | } |
| 65 | |
| 66 | public static void Run(string[] args) |
| 67 | { |
Jan Tattermusch | 90cbde8 | 2016-08-08 14:33:28 +0800 | [diff] [blame] | 68 | var parserResult = Parser.Default.ParseArguments<ServerOptions>(args) |
| 69 | .WithNotParsed((x) => Environment.Exit(1)) |
| 70 | .WithParsed(options => |
| 71 | { |
| 72 | var workerServer = new QpsWorker(options); |
| 73 | workerServer.RunAsync().Wait(); |
| 74 | }); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 77 | private async Task RunAsync() |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 78 | { |
| 79 | string host = "0.0.0.0"; |
| 80 | int port = options.DriverPort; |
| 81 | |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 82 | var tcs = new TaskCompletionSource<object>(); |
| 83 | var workerServiceImpl = new WorkerServiceImpl(() => { Task.Run(() => tcs.SetResult(null)); }); |
| 84 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 85 | var server = new Server |
| 86 | { |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 87 | Services = { WorkerService.BindService(workerServiceImpl) }, |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 88 | Ports = { new ServerPort(host, options.DriverPort, ServerCredentials.Insecure )} |
| 89 | }; |
| 90 | int boundPort = server.Ports.Single().BoundPort; |
| 91 | Console.WriteLine("Running qps worker server on " + string.Format("{0}:{1}", host, boundPort)); |
| 92 | server.Start(); |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 93 | await tcs.Task; |
| 94 | await server.ShutdownAsync(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |