-
Notifications
You must be signed in to change notification settings - Fork 787
/
Copy pathLinkerTests.cs
150 lines (128 loc) · 5.34 KB
/
LinkerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Grpc.AspNetCore.FunctionalTests.Linker.Helpers;
using Grpc.Tests.Shared;
using NUnit.Framework;
// Skip running load running tests in debug configuration
#if !DEBUG
namespace Grpc.AspNetCore.FunctionalTests.Linker
{
[TestFixture]
[Category("LongRunning")]
public class LinkerTests
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(60);
[Test]
public async Task RunWebsiteAndCallWithClient_Success()
{
var projectDirectory = typeof(LinkerTests).Assembly
.GetCustomAttributes<AssemblyMetadataAttribute>()
.Single(a => a.Key == "ProjectDirectory")
.Value;
var tempPath = Path.GetTempPath();
var linkerTestsClientPath = Path.Combine(tempPath, "LinkerTestsClient");
var linkerTestsWebsitePath = Path.Combine(tempPath, "LinkerTestsWebsite");
EnsureDeleted(linkerTestsClientPath);
EnsureDeleted(linkerTestsWebsitePath);
try
{
using var websiteProcess = new WebsiteProcess();
using var clientProcess = new DotNetProcess();
try
{
var publishWebsiteTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", linkerTestsWebsitePath);
var publishClientTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsClient\LinkerTestsClient.csproj", linkerTestsClientPath);
await Task.WhenAll(publishWebsiteTask, publishClientTask).TimeoutAfter(Timeout);
websiteProcess.Start(Path.Combine(linkerTestsWebsitePath, "LinkerTestsWebsite.dll"));
await websiteProcess.WaitForReadyAsync().TimeoutAfter(Timeout);
clientProcess.Start(Path.Combine(linkerTestsClientPath, $"LinkerTestsClient.dll {websiteProcess.ServerPort}"));
await clientProcess.WaitForExitAsync().TimeoutAfter(Timeout);
Assert.AreEqual(0, clientProcess.ExitCode);
}
finally
{
Console.WriteLine("Website output:");
Console.WriteLine(websiteProcess.GetOutput());
Console.WriteLine("Client output:");
Console.WriteLine(clientProcess.GetOutput());
}
}
finally
{
EnsureDeleted(linkerTestsClientPath);
EnsureDeleted(linkerTestsWebsitePath);
}
}
private void EnsureDeleted(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
private static async Task PublishAppAsync(string path, string outputPath)
{
var resolvedPath = Path.GetFullPath(path);
Console.WriteLine($"Publishing {resolvedPath}");
var process = new DotNetProcess();
try
{
process.Start($"publish {resolvedPath} -r {GetRuntimeIdentifier()} -c Release -o {outputPath} --self-contained");
await process.WaitForExitAsync().TimeoutAfter(Timeout);
}
catch (Exception ex)
{
throw new Exception("Error while publishing app.", ex);
}
finally
{
var exitCode = process.HasExited ? (int?)process.ExitCode : null;
process.Dispose();
var output = process.GetOutput();
Console.WriteLine("Publish output:");
Console.WriteLine(output);
if (exitCode != null && exitCode.Value != 0)
{
throw new Exception($"Non-zero exit code returned: {exitCode}");
}
}
}
private static string GetRuntimeIdentifier()
{
var architecture = RuntimeInformation.OSArchitecture.ToString().ToLower();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "win10-" + architecture;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "linux-" + architecture;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "osx-" + architecture;
}
throw new InvalidOperationException("Unrecognized operation system platform");
}
}
}
#endif