!56 【HTTP】ylong_runtim替换ylong_http和ylong_http_client下测试用例异步运行时

Merge pull request !56 from Ziheng Liu/ylong_runtime
This commit is contained in:
openharmony_ci
2023-09-14 12:28:26 +00:00
committed by Gitee
24 changed files with 243 additions and 130 deletions
+14 -1
View File
@@ -12,6 +12,7 @@
# limitations under the License.
import("//build/ohos.gni")
import("//build/test.gni")
ohos_rust_static_library("ylong_http") {
subsystem_name = "commonlibrary"
@@ -21,7 +22,6 @@ ohos_rust_static_library("ylong_http") {
edition = "2021"
features = [
"default",
"http1_1",
"ylong_base",
]
@@ -29,3 +29,16 @@ ohos_rust_static_library("ylong_http") {
sources = [ "src/lib.rs" ]
external_deps = [ "ylong_runtime:ylong_runtime" ]
}
ohos_rust_unittest("rust_ylong_http_test_ut") {
module_out_path = "ylong_http/ylong_http"
rustflags = [
"--cfg=feature=\"http1_1\"",
"--cfg=feature=\"ylong_base\"",
]
sources = [ "src/lib.rs" ]
deps = []
external_deps = [ "ylong_runtime:ylong_runtime" ]
}
+9 -3
View File
@@ -134,9 +134,15 @@ mod ut_empty {
/// 1. Creates an `EmptyBody`.
/// 2. Calls its `async_impl::Body::data` method and then checks the
/// results.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_empty_body_async_impl_data() {
#[test]
fn ut_empty_body_async_impl_data() {
let handle = ylong_runtime::spawn(async move {
empty_body_async_impl_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn empty_body_async_impl_data() {
use crate::body::async_impl::Body;
let mut body = EmptyBody::new();
+38 -14
View File
@@ -420,9 +420,15 @@ mod ut_mix {
/// `MixFrom::set_async_reader`.
/// 2. Encodes by asynchronous encoding.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mix_set_async_reader() {
#[test]
fn ut_mix_set_async_reader() {
let handle = ylong_runtime::spawn(async move {
mix_set_async_reader().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mix_set_async_reader() {
mix_encode_compare!(
MixFrom: {
BodyAsyncReader: "12345678".as_bytes(),
@@ -439,9 +445,15 @@ mod ut_mix {
/// `MixFrom::set_reader`.
/// 2. Encodes by asynchronous encoding.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mix_set_reader_then_async_data() {
#[test]
fn ut_mix_set_reader_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mix_set_reader_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mix_set_reader_then_async_data() {
mix_encode_compare!(
MixFrom: {
BodyReader: "12345678".as_bytes(),
@@ -458,14 +470,20 @@ mod ut_mix {
/// `MixFrom::set_bytes`.
/// 2. Encodes by asynchronous encoding.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mix_set_bytes_then_async_data() {
#[test]
fn ut_mix_set_bytes_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mix_set_bytes_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mix_set_bytes_then_async_data() {
mix_encode_compare!(
MixFrom: {
BodySlice: "12345678".as_bytes(),
BodyReader: "12345678".as_bytes(),
},
ResultData: b"12345678",
ResultData: b"",
Async,
);
}
@@ -477,9 +495,15 @@ mod ut_mix {
/// `MixFrom::set_owned`.
/// 2. Encodes by asynchronous encoding.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mix_set_owned_then_async_data() {
#[test]
fn ut_mix_set_owned_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mix_set_owned_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mix_set_owned_then_async_data() {
mix_encode_compare!(
MixFrom: {
BodyOwned: b"12345678".to_vec(),
+10 -3
View File
@@ -542,9 +542,16 @@ mod ut_mime_multi_encoder {
/// 4. Builds a `MimeMultiEncoder` by `from_multi` and encodes
/// asynchronously.
/// 5. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mime_multi_encoder_data_many_parts_nesting_then_async_data() {
#[test]
fn ut_mime_multi_encoder_data_many_parts_nesting_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mime_multi_encoder_data_many_parts_nesting_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mime_multi_encoder_data_many_parts_nesting_then_async_data() {
let multi1 = MimeMulti::builder()
.set_content_type(b"multipart/mixed", b"abc".to_vec())
.add_part(
+27 -9
View File
@@ -381,9 +381,15 @@ mod ut_mime_part_encoder {
/// 2. Sets body by `body_from_owned`.
/// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously.
/// 4. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mime_part_encoder_body_from_owned_then_async_data() {
#[test]
fn ut_mime_part_encoder_body_from_owned_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mime_part_encoder_body_from_owned_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mime_part_encoder_body_from_owned_then_async_data() {
part_encode_compare! (
MimePart: {
BodyOwned: b"123456".to_vec(),
@@ -402,9 +408,15 @@ mod ut_mime_part_encoder {
/// 2. Sets body by `body_from_bytes`.
/// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously.
/// 4. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mime_part_encoder_body_from_bytes_then_async_data() {
#[test]
fn ut_mime_part_encoder_body_from_bytes_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mime_part_encoder_body_from_bytes_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mime_part_encoder_body_from_bytes_then_async_data() {
part_encode_compare! (
MimePart: {
BodySlice: b"123456",
@@ -423,9 +435,15 @@ mod ut_mime_part_encoder {
/// 2. Sets headers and sets body.
/// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously.
/// 4. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_mime_part_encoder_common_then_async_data() {
#[test]
fn ut_mime_part_encoder_common_then_async_data() {
let handle = ylong_runtime::spawn(async move {
mime_part_encoder_common_then_async_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn mime_part_encoder_common_then_async_data() {
part_encode_compare! (
MimePart: {
Header: "accept", "text/html",
+18 -6
View File
@@ -688,9 +688,15 @@ mod ut_mime {
/// 1. Creates a `MultiPart` and sets values.
/// 2. Encodes `MultiPart` by `async_impl::Body::data`.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_multipart_poll_data() {
#[test]
fn ut_multipart_poll_data() {
let handle = ylong_runtime::spawn(async move {
multipart_poll_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn multipart_poll_data() {
let mut mp = MultiPart::new().part(
Part::new()
.name("name")
@@ -722,9 +728,15 @@ mod ut_mime {
/// 1. Creates a `MultiPart` and sets values.
/// 2. Encodes `MultiPart` by `async_impl::Body::data`.
/// 3. Checks whether the result is correct.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_multipart_poll_data_stream() {
#[test]
fn ut_multipart_poll_data_stream() {
let handle = ylong_runtime::spawn(async move {
multipart_poll_data_stream().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn multipart_poll_data_stream() {
let mut mp = MultiPart::new().part(
Part::new()
.name("name")
+9 -3
View File
@@ -519,9 +519,15 @@ mod ut_mod {
/// 2. Gets its mutable reference.
/// 3. Calls its `async_impl::Body::data` method and then checks the
/// results.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_asyn_body_mut_asyn_body_data() {
#[test]
fn ut_asyn_body_mut_asyn_body_data() {
let handle = ylong_runtime::spawn(async move {
asyn_body_mut_asyn_body_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn asyn_body_mut_asyn_body_data() {
use crate::body::async_impl::Body;
let mut body = EmptyBody::new();
+18 -6
View File
@@ -546,9 +546,15 @@ mod ut_text {
/// 1. Creates a `TextBody<FromBytes<'_>>`.
/// 2. Calls its `async_impl::Body::data` method and then checks the
/// results.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_text_body_from_bytes_asyn_data() {
#[test]
fn ut_text_body_from_bytes_asyn_data() {
let handle = ylong_runtime::spawn(async move {
text_body_from_bytes_asyn_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn text_body_from_bytes_asyn_data() {
use crate::body::async_impl::Body;
let bytes = b"Hello World!";
@@ -601,9 +607,15 @@ mod ut_text {
/// 1. Creates a `TextBody<FromAsyncReader<T>>`.
/// 2. Calls its `async_impl::Body::data` method and then checks the
/// results.
#[cfg(feature = "tokio_base")]
#[tokio::test]
async fn ut_text_body_from_async_reader_asyn_data() {
#[test]
fn ut_text_body_from_async_reader_asyn_data() {
let handle = ylong_runtime::spawn(async move {
text_body_from_async_reader_asyn_data().await;
});
ylong_runtime::block_on(handle).unwrap();
}
async fn text_body_from_async_reader_asyn_data() {
use crate::body::async_impl::Body;
let reader = "Hello World!".as_bytes();
+9 -1
View File
@@ -40,7 +40,7 @@ ohos_rust_shared_library("ylong_http_client_inner") {
external_deps = [ "ylong_runtime:ylong_runtime" ]
}
ohos_rust_unittest("unittest") {
ohos_rust_unittest("rust_ylong_http_client_test_ut") {
module_out_path = "ylong_http/ylong_http_client"
rustflags = [
@@ -61,3 +61,11 @@ ohos_rust_unittest("unittest") {
]
external_deps = [ "ylong_runtime:ylong_runtime" ]
}
group("unittest") {
testonly = true
deps = [
":rust_ylong_http_client_test_ut",
"../ylong_http:rust_ylong_http_test_ut",
]
}
+7 -32
View File
@@ -48,7 +48,12 @@ c_openssl_3_0 = ["__c_openssl"] # Uses TLS by FFI of C-openssl 3.0.
[[example]]
name = "async_http"
path = "examples/async_http.rs"
required-features = ["async", "http1_1", "tokio_base"]
required-features = ["async", "http1_1", "ylong_base"]
[[example]]
name = "async_http_multi"
path = "examples/async_http_multi.rs"
required-features = ["async", "http1_1", "ylong_base"]
[[example]]
name = "async_http2"
@@ -78,7 +83,7 @@ required-features = ["async", "http1_1", "tokio_base"]
[[example]]
name = "async_redirect_http"
path = "examples/async_redirect_http.rs"
required-features = ["async", "http1_1"]
required-features = ["async", "http1_1", "tokio_base"]
[[example]]
name = "sync_http"
@@ -94,33 +99,3 @@ required-features = ["sync", "http1_1"]
name = "sync_proxy_http"
path = "examples/sync_proxy_http.rs"
required-features = ["sync", "http1_1"]
[[test]]
name = "sdv_async_http"
path = "./tests/sdv_async_http.rs"
required-features = ["async", "http1_1", "tokio_base"]
[[test]]
name = "sdv_sync_http"
path = "./tests/sdv_sync_http.rs"
required-features = ["sync", "http1_1", "tokio_base"]
[[test]]
name = "sdv_async_https_c_ssl"
path = "./tests/sdv_async_https_c_ssl.rs"
required-features = ["async", "http1_1", "__tls", "tokio_base"]
[[test]]
name = "sdv_sync_https_c_ssl"
path = "./tests/sdv_sync_https_c_ssl.rs"
required-features = ["sync", "http1_1", "__tls", "tokio_base"]
[[test]]
name = "sdv_async_http_on_tcp"
path = "./tests/sdv_async_http_on_tcp.rs"
required-features = ["async", "http1_1", "ylong_base"]
[[test]]
name = "sdv_sync_http_on_tcp"
path = "./tests/sdv_sync_http_on_tcp.rs"
required-features = ["sync", "http1_1"]
+10 -2
View File
@@ -18,8 +18,16 @@
use ylong_http_client::async_impl::{Client, Downloader};
use ylong_http_client::{HttpClientError, Request};
#[tokio::main]
async fn main() -> Result<(), HttpClientError> {
fn main() -> Result<(), HttpClientError> {
let handle = ylong_runtime::spawn(async move {
let _ = client_send().await.unwrap();
});
let _ = ylong_runtime::block_on(handle);
Ok(())
}
async fn client_send() -> Result<(), HttpClientError> {
// Creates a `async_impl::Client`
let client = Client::new();
+3 -1
View File
@@ -14,10 +14,12 @@
//! This is a simple asynchronous HTTP2 client example using the
//! ylong_http_client crate. It demonstrates creating a client, making a
//! request, and reading the response asynchronously.
#[cfg(feature = "tokio_base")]
use ylong_http_client::async_impl::{Body, ClientBuilder};
#[cfg(feature = "tokio_base")]
use ylong_http_client::{HttpClientError, RequestBuilder, StatusCode, TextBody, Version};
#[cfg(feature = "tokio_base")]
fn main() -> Result<(), HttpClientError> {
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
@@ -14,12 +14,14 @@
//! This is a simple asynchronous HTTP client example in concurrent scenarios
//! using the ylong_http_client crate. It demonstrates creating a client, making
//! a request, and reading the response asynchronously.
#[cfg(feature = "tokio_base")]
use std::sync::Arc;
#[cfg(feature = "tokio_base")]
use ylong_http_client::async_impl::{Body, ClientBuilder};
#[cfg(feature = "tokio_base")]
use ylong_http_client::{HttpClientError, RequestBuilder, StatusCode, TextBody, Version};
#[cfg(feature = "tokio_base")]
fn main() -> Result<(), HttpClientError> {
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
@@ -0,0 +1,41 @@
// Copyright (c) 2023 Huawei Device Co., Ltd.
// 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.
//! This is a simple asynchronous HTTP client example using the
//! ylong_http_client crate. It demonstrates creating a client, making a
//! request, and reading the response asynchronously.
use ylong_http_client::async_impl::{Client, Downloader};
use ylong_http_client::{HttpClientError, Request};
fn main() -> Result<(), HttpClientError> {
let mut handles = Vec::new();
for _ in 0..4 {
handles.push(ylong_runtime::spawn(async move {
let client = Client::new();
let request = Request::get("127.0.0.1:3000")
.body("".as_bytes())
.map_err(|e| HttpClientError::other(Some(e)))
.unwrap();
// Sends request and receives a `Response`.
let response = client.request(request).await.unwrap();
// Reads the body of `Response` by using `BodyReader`.
let _ = Downloader::console(response).download().await;
}))
}
for handle in handles {
let _ = ylong_runtime::block_on(handle);
}
Ok(())
}
@@ -10,11 +10,13 @@
// 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.
#[cfg(feature = "tokio_base")]
use ylong_http_client::async_impl::{Client, Downloader};
#[cfg(feature = "tokio_base")]
use ylong_http_client::util::Redirect;
#[cfg(feature = "tokio_base")]
use ylong_http_client::{Certificate, HttpClientError, Request, TlsVersion};
#[cfg(feature = "tokio_base")]
fn main() {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
@@ -14,9 +14,12 @@
//! This is a simple asynchronous HTTP client example using the
//! ylong_http_client crate. It demonstrates creating a client, making a
//! request, and reading the response asynchronously.
#[cfg(feature = "tokio_base")]
use ylong_http_client::async_impl::{ClientBuilder, Downloader};
#[cfg(feature = "tokio_base")]
use ylong_http_client::{EmptyBody, HttpClientError, Proxy, Request};
#[cfg(feature = "tokio_base")]
#[tokio::main]
async fn main() -> Result<(), HttpClientError> {
// Creates a `async_impl::Client`
@@ -14,10 +14,11 @@
//! This is a simple asynchronous HTTP client redirect example using the
//! ylong_http_client crate. It demonstrates creating a client, making a
//! request, and reading the response asynchronously.
#[cfg(feature = "tokio_base")]
use ylong_http_client::async_impl::{ClientBuilder, Downloader};
#[cfg(feature = "tokio_base")]
use ylong_http_client::{HttpClientError, Redirect, Request};
#[cfg(feature = "tokio_base")]
#[tokio::main]
async fn main() -> Result<(), HttpClientError> {
// Creates a `async_impl::Client`
@@ -455,41 +455,4 @@ mod ut_http2 {
Some(H2Error::StreamError(1, ErrorCode::ProtocolError).into())
);
}
#[cfg(feature = "tokio_base")]
#[test]
fn ut_http2_build_data_frame() {
let mut request = build_request!(
Request: {
Method: "GET",
Uri: "http://127.0.0.1:0/data",
Version: "HTTP/2.0",
Header: "te", "trailers",
Header: "host", "127.0.0.1:0",
Body: "Hi",
}
);
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.expect("Build runtime failed.");
rt.block_on(async move {
if let Some(frame) = build_data_frame(1, request.body_mut())
.await
.expect("build data frame failed")
{
assert_eq!(frame.flags().bits(), 0x1);
assert_eq!(frame.stream_id(), 1);
if let Payload::Data(data) = frame.payload() {
assert_eq!(data.data().as_slice(), "Hi".as_bytes())
} else {
panic!("err frame type");
}
} else {
panic!("unexpected data frame build result");
}
});
}
}
+1 -1
View File
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(feature = "async")]
#![cfg(all(feature = "async", feature = "http1_1", feature = "tokio_base"))]
#[macro_use]
mod common;
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(feature = "async")]
#![cfg(all(feature = "async", feature = "http1_1", feature = "ylong_base"))]
#[macro_use]
pub mod tcp_server;
@@ -11,7 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(all(feature = "async", feature = "__tls"))]
#![cfg(all(
feature = "async",
feature = "http1_1",
feature = "__tls",
feature = "tokio_base"
))]
#[macro_use]
mod common;
+1 -1
View File
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(feature = "sync")]
#![cfg(all(feature = "sync", feature = "http1_1", feature = "tokio_base"))]
#[macro_use]
mod common;
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(feature = "sync")]
#![cfg(all(feature = "sync", feature = "http1_1", feature = "ylong_base"))]
#[macro_use]
mod tcp_server;
@@ -11,7 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg(all(feature = "sync", feature = "__tls"))]
#![cfg(all(
feature = "sync",
feature = "http1_1",
feature = "__tls",
feature = "tokio_base"
))]
use std::path::PathBuf;