-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWcfTestsBase.cs
97 lines (79 loc) · 3.09 KB
/
WcfTestsBase.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Net.Sockets;
using FluentAssertions;
using IntegrationTests.Helpers;
using Xunit.Abstractions;
using static OpenTelemetry.Proto.Trace.V1.Span.Types;
namespace IntegrationTests;
public abstract class WcfTestsBase : TestHelper, IDisposable
{
private readonly string _testAppName;
private ProcessHelper? _serverProcess;
protected WcfTestsBase(string testAppName, ITestOutputHelper output)
: base(testAppName, output)
{
_testAppName = testAppName;
}
public void Dispose()
{
if (_serverProcess?.Process == null)
{
return;
}
if (_serverProcess.Process.HasExited)
{
Output.WriteLine($"WCF server process finished. Exit code: {_serverProcess.Process.ExitCode}.");
}
else
{
_serverProcess.Process.Kill();
}
Output.WriteLine("ProcessId: " + _serverProcess.Process.Id);
Output.WriteLine("Exit Code: " + _serverProcess.Process.ExitCode);
Output.WriteResult(_serverProcess);
}
protected async Task SubmitsTracesInternal(string clientPackageVersion)
{
EnvironmentTools.IsWindowsAdministrator().Should().BeTrue(); // WCF Server needs admin
var collector = new MockSpansCollector(Output);
SetExporter(collector);
var serverHelper = new WcfServerTestHelper(Output);
_serverProcess = serverHelper.RunWcfServer(collector);
await WaitForServer();
RunTestApplication(new TestSettings
{
PackageVersion = clientPackageVersion
});
collector.Expect("OpenTelemetry.Instrumentation.Wcf", span => span.Kind == SpanKind.Server, "Server 1");
collector.Expect("OpenTelemetry.Instrumentation.Wcf", span => span.Kind == SpanKind.Client, "Client 1");
collector.Expect("OpenTelemetry.Instrumentation.Wcf", span => span.Kind == SpanKind.Server, "Server 2");
collector.Expect("OpenTelemetry.Instrumentation.Wcf", span => span.Kind == SpanKind.Client, "Client 2");
collector.Expect($"TestApplication.{_testAppName}", span => span.Kind == SpanKind.Internal, "Custom parent");
collector.Expect($"TestApplication.{_testAppName}", span => span.Kind == SpanKind.Internal, "Custom sibling");
collector.ExpectCollected(WcfClientInstrumentation.ValidateExpectedSpanHierarchy);
collector.AssertExpectations();
}
private async Task WaitForServer()
{
const int tcpPort = 9090;
using var tcpClient = new TcpClient();
var retries = 0;
Output.WriteLine("Waiting for WCF Server to open ports.");
while (retries < 60)
{
try
{
await tcpClient.ConnectAsync("127.0.0.1", tcpPort);
Output.WriteLine("WCF Server is running.");
return;
}
catch (Exception)
{
retries++;
await Task.Delay(500);
}
}
Assert.Fail("WCF Server did not open the port.");
}
}