admin管理员组文章数量:1125547
When I am running the .NET 8 Provider for verifying my contract, the server seems to just stop during the verify. I'm not getting any useful logs too so I can't tell why it's just stopping. I can, however, get a test to pass when I use a delay and hit the endpoint of the server through Postman during this delay and before the .Verify()
is hit.
public class ContractTests : IClassFixture<ApiFixture>
{
private readonly ITestOutputHelper _output;
private readonly ApiFixture _fixture;
private readonly Uri _pactBrokerBaseUri = new Uri("<pactBrokerUri>");
private readonly string _providerName = "provider";
private readonly string _consumerName = "consumer";
public ContractTests(ITestOutputHelper outputHelper, ApiFixture fixture)
{
_fixture = fixture;
_output = outputHelper;
}
[Fact]
public async Task EnsureProviderApiHonoursPactWithConsumer()
{
// Arrange
var config = new PactVerifierConfig
{
Outputters = new List<Pact.IOutput>
{
new XunitOutput(_output)
},
LogLevel = PactLogLevel.Debug,
};
// Act
IPactVerifier pactVerifier = new PactVerifier(_providerName, config);
var _pactVerifier = pactVerifier
.WithHttpEndpoint(_fixture.ServerUri)
.WithUriSource(
new Uri($"{_pactBrokerBaseUri}pacts/provider/{_providerName}/consumer/{_consumerName}/latest"));
// This is in place to allow a call to the server from Postman
await Task.Delay(TimeSpan.FromSeconds(30));
// Assert
_pactVerifier.Verify();
}
}
This test fails unless I hit the same endpoint using Postman during the delay time.
public class ApiFixture : IDisposable
{
private readonly IHost _server;
public Uri ServerUri { get; }
public ApiFixture()
{
ServerUri = new Uri("https://localhost:5066");
_server = CreateHostBuilder(ServerUri)
.Build();
_server.Start();
}
public static IHostBuilder CreateHostBuilder(Uri serverUri) =>
Host.CreateDefaultBuilder()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls(serverUri.ToString());
webBuilder
.UseStartup<TestStartup>();
});
public void Dispose()
{
_server.Dispose();
}
}
This server does run on the correct port and can be hit from Postman during the timeout but if I don't hit Postman it just stops during the .Verify()
The only error I get is:
Request Failed - error sending request for url (https://localhost:5066/api/v1/searchEndpoint)
and the logs just end with:
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connecting to [::1]:5066
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connected to [::1]:5066
WARN ThreadId(02) pact_matching::metrics:
When I am running the .NET 8 Provider for verifying my contract, the server seems to just stop during the verify. I'm not getting any useful logs too so I can't tell why it's just stopping. I can, however, get a test to pass when I use a delay and hit the endpoint of the server through Postman during this delay and before the .Verify()
is hit.
public class ContractTests : IClassFixture<ApiFixture>
{
private readonly ITestOutputHelper _output;
private readonly ApiFixture _fixture;
private readonly Uri _pactBrokerBaseUri = new Uri("<pactBrokerUri>");
private readonly string _providerName = "provider";
private readonly string _consumerName = "consumer";
public ContractTests(ITestOutputHelper outputHelper, ApiFixture fixture)
{
_fixture = fixture;
_output = outputHelper;
}
[Fact]
public async Task EnsureProviderApiHonoursPactWithConsumer()
{
// Arrange
var config = new PactVerifierConfig
{
Outputters = new List<Pact.IOutput>
{
new XunitOutput(_output)
},
LogLevel = PactLogLevel.Debug,
};
// Act
IPactVerifier pactVerifier = new PactVerifier(_providerName, config);
var _pactVerifier = pactVerifier
.WithHttpEndpoint(_fixture.ServerUri)
.WithUriSource(
new Uri($"{_pactBrokerBaseUri}pacts/provider/{_providerName}/consumer/{_consumerName}/latest"));
// This is in place to allow a call to the server from Postman
await Task.Delay(TimeSpan.FromSeconds(30));
// Assert
_pactVerifier.Verify();
}
}
This test fails unless I hit the same endpoint using Postman during the delay time.
public class ApiFixture : IDisposable
{
private readonly IHost _server;
public Uri ServerUri { get; }
public ApiFixture()
{
ServerUri = new Uri("https://localhost:5066");
_server = CreateHostBuilder(ServerUri)
.Build();
_server.Start();
}
public static IHostBuilder CreateHostBuilder(Uri serverUri) =>
Host.CreateDefaultBuilder()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls(serverUri.ToString());
webBuilder
.UseStartup<TestStartup>();
});
public void Dispose()
{
_server.Dispose();
}
}
This server does run on the correct port and can be hit from Postman during the timeout but if I don't hit Postman it just stops during the .Verify()
The only error I get is:
Request Failed - error sending request for url (https://localhost:5066/api/v1/searchEndpoint)
and the logs just end with:
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connecting to [::1]:5066
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connected to [::1]:5066
WARN ThreadId(02) pact_matching::metrics:
Share
Improve this question
edited 17 hours ago
jonrsharpe
122k30 gold badges264 silver badges472 bronze badges
asked 17 hours ago
Isaac RichardsonIsaac Richardson
12 bronze badges
1 Answer
Reset to default 0In this case the issue was that the timeout was not long enough on the _pactVerifier
. By default, this time is set to 5 seconds PactNet and this can be overridden using
_pactVerifier.WithRequestTimeout(TimeSpan.FromSeconds(20))
to increase the verifier timeout to 20 seconds in this case.
本文标签: Server stops when running Pact verification on NET ProviderStack Overflow
版权声明:本文标题:Server stops when running Pact verification on .NET Provider - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736572381a1944789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论