Skip to content

Commit

Permalink
Merge pull request #83 from Cysharp/feature/FixKeepAliveNotWorking
Browse files Browse the repository at this point in the history
Fix a problem the handler not working correctly when keep-alive is enabled
  • Loading branch information
mayuki authored Jul 24, 2024
2 parents 70a2e5a + 6dadf92 commit 42340e9
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 7 deletions.
8 changes: 4 additions & 4 deletions native/yaha_native/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use hyper::{
};

use hyper_util::{
client::{self, legacy::connect::HttpConnector, legacy::Client},
rt::TokioExecutor,
client::{self, legacy::{connect::HttpConnector, Client}},
rt::{TokioExecutor, TokioTimer},
};

use hyper_rustls::ConfigBuilderExt;
Expand Down Expand Up @@ -88,9 +88,9 @@ impl YahaNativeContextInternal<'_> {
}

pub fn build_client(&mut self, skip_verify_certificates: bool) {
let builder = self.client_builder.take().unwrap();
let mut builder = self.client_builder.take().unwrap();
let https = self.new_connector(skip_verify_certificates);
self.client = Some(builder.build(https));
self.client = Some(builder.timer(TokioTimer::new()).build(https));
}

#[cfg(feature = "rustls")]
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion test/YetAnotherHttpHandler.Test/Http2ClearTextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public Http2ClearTextTest(ITestOutputHelper testOutputHelper) : base(testOutputH
{
}

protected override HttpMessageHandler CreateHandler()
protected override YetAnotherHttpHandler CreateHandler()
{
return new YetAnotherHttpHandler() { Http2Only = true };
}
Expand Down
2 changes: 1 addition & 1 deletion test/YetAnotherHttpHandler.Test/Http2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Http2Test(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override HttpMessageHandler CreateHandler()
protected override YetAnotherHttpHandler CreateHandler()
{
// Use self-signed certificate for testing purpose.
return new YetAnotherHttpHandler()
Expand Down
30 changes: 29 additions & 1 deletion test/YetAnotherHttpHandler.Test/Http2TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO.Pipelines;
using System.Net;
using System.Net.Http.Headers;
using Cysharp.Net.Http;
using Grpc.Core;
using Grpc.Net.Client;
using TestWebApp;
Expand All @@ -15,7 +16,7 @@ protected Http2TestBase(ITestOutputHelper testOutputHelper) : base(testOutputHel
{
}

protected abstract HttpMessageHandler CreateHandler();
protected abstract YetAnotherHttpHandler CreateHandler();
protected abstract Task<TestWebAppServer> LaunchServerAsyncCore<T>(Action<WebApplicationBuilder>? configure = null) where T : ITestServerBuilder;

protected Task<TestWebAppServer> LaunchServerAsync<T>(Action<WebApplicationBuilder>? configure = null) where T : ITestServerBuilder
Expand Down Expand Up @@ -630,6 +631,33 @@ public async Task Grpc_Error_TimedOut_With_CancellationToken()
Assert.Equal(StatusCode.Cancelled, ((RpcException)ex).StatusCode);
}

[ConditionalFact]
public async Task Enable_Http2KeepAlive()
{
// Arrange
using var httpHandler = CreateHandler();
httpHandler.Http2KeepAliveInterval = TimeSpan.FromSeconds(5);
httpHandler.Http2KeepAliveTimeout = TimeSpan.FromSeconds(5);
httpHandler.Http2KeepAliveWhileIdle = true;

var httpClient = new HttpClient(httpHandler);
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();

// Act
var request = new HttpRequestMessage(HttpMethod.Get, $"{server.BaseUri}/")
{
Version = HttpVersion.Version20,
};
var response = await httpClient.SendAsync(request).WaitAsync(TimeoutToken);
var responseBody = await response.Content.ReadAsStringAsync().WaitAsync(TimeoutToken);

// Assert
Assert.Equal("__OK__", responseBody);
Assert.Equal(HttpVersion.Version20, response.Version);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}


// Content with default value of true for AllowDuplex because AllowDuplex is internal.
class DuplexStreamContent : HttpContent
{
Expand Down

0 comments on commit 42340e9

Please sign in to comment.