From fde1e2d18a4dd4748621792d3d2c0fcf573145d6 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 9 Jul 2024 14:43:10 +1000 Subject: [PATCH 1/2] chore: Fix race condition in tests in CI --- rust/pact_ffi/src/mock_server/mod.rs | 5 +++-- rust/pact_ffi/tests/tests.rs | 16 +++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rust/pact_ffi/src/mock_server/mod.rs b/rust/pact_ffi/src/mock_server/mod.rs index f9ea7ae4d..5377bb1be 100644 --- a/rust/pact_ffi/src/mock_server/mod.rs +++ b/rust/pact_ffi/src/mock_server/mod.rs @@ -199,6 +199,7 @@ pub extern fn pactffi_get_tls_ca_certificate() -> *mut c_char { #[no_mangle] #[tracing::instrument(level = "trace")] #[allow(clippy::not_unsafe_ptr_arg_deref)] +#[deprecated(note = "This function is deprecated and replaced with `pactffi_create_mock_server_for_transport`")] pub extern fn pactffi_create_mock_server_for_pact(pact: PactHandle, addr_str: *const c_char, tls: bool) -> i32 { let result = catch_unwind(|| { let addr_c_str = unsafe { @@ -312,7 +313,7 @@ ffi_fn! { transport_config: *const c_char ) -> i32 { let addr = safe_str!(addr); - let transport = safe_str!(transport); + let transport = optional_str(transport).unwrap_or_else(|| "http".to_string()); let transport_config = match optional_str(transport_config).map(|config| str::parse::(config.as_str())) { None => Ok(None), @@ -338,7 +339,7 @@ ffi_fn! { }; match pact_mock_server::start_mock_server_for_transport(Uuid::new_v4().to_string(), - inner.pact.boxed(), socket_addr, transport, config) { + inner.pact.boxed(), socket_addr, transport.as_str(), config) { Ok(ms_port) => { inner.mock_server_started = true; ms_port diff --git a/rust/pact_ffi/tests/tests.rs b/rust/pact_ffi/tests/tests.rs index 3805ff35f..c673a06de 100644 --- a/rust/pact_ffi/tests/tests.rs +++ b/rust/pact_ffi/tests/tests.rs @@ -1,9 +1,10 @@ -use std::env; +use std::{env, thread}; use std::ffi::{CStr, CString}; use std::fs::File; use std::io::Read; use std::path::PathBuf; use std::ptr::null; +use std::time::Duration; use bytes::Bytes; use expectest::prelude::*; @@ -30,6 +31,7 @@ use pact_ffi::mock_server::{ pactffi_mock_server_mismatches, pactffi_write_pact_file, pactffi_mock_server_logs, + pactffi_create_mock_server_for_transport }; #[allow(deprecated)] use pact_ffi::mock_server::handles::{ @@ -773,7 +775,6 @@ fn test_missing_plugin() { // Issue #299 #[test_log::test] -#[allow(deprecated)] fn each_value_matcher() { let consumer_name = CString::new("each_value_matcher-consumer").unwrap(); let provider_name = CString::new("each_value_matcher-provider").unwrap(); @@ -796,7 +797,7 @@ fn each_value_matcher() { ] }); let body = CString::new(json.to_string()).unwrap(); - let address = CString::new("127.0.0.1:0").unwrap(); + let address = CString::new("127.0.0.1").unwrap(); let method = CString::new("PUT").unwrap(); pactffi_upon_receiving(interaction.clone(), description.as_ptr()); @@ -804,7 +805,7 @@ fn each_value_matcher() { pactffi_with_body(interaction.clone(), InteractionPart::Request, content_type.as_ptr(), body.as_ptr()); pactffi_response_status(interaction.clone(), 200); - let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false); + let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); @@ -823,6 +824,7 @@ fn each_value_matcher() { } }; + thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events let mismatches = unsafe { CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned() }; @@ -838,7 +840,6 @@ fn each_value_matcher() { // Issue #301 #[test_log::test] -#[allow(deprecated)] fn each_key_matcher() { let consumer_name = CString::new("each_key_matcher-consumer").unwrap(); let provider_name = CString::new("each_key_matcher-provider").unwrap(); @@ -862,7 +863,7 @@ fn each_key_matcher() { ] }); let body = CString::new(json.to_string()).unwrap(); - let address = CString::new("127.0.0.1:0").unwrap(); + let address = CString::new("127.0.0.1").unwrap(); let method = CString::new("PUT").unwrap(); pactffi_upon_receiving(interaction.clone(), description.as_ptr()); @@ -870,7 +871,7 @@ fn each_key_matcher() { pactffi_with_body(interaction.clone(), InteractionPart::Request, content_type.as_ptr(), body.as_ptr()); pactffi_response_status(interaction.clone(), 200); - let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false); + let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); @@ -880,6 +881,7 @@ fn each_key_matcher() { .body(r#"{"1": "foo","not valid": 1,"key": "value","key2": "value"}"#) .send(); + thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events let mismatches = unsafe { CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned() }; From b52007e224385314ac27b20daf13b536ebce301e Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 9 Jul 2024 14:46:57 +1000 Subject: [PATCH 2/2] chore: Fix race condition in tests in CI --- rust/pact_ffi/tests/tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rust/pact_ffi/tests/tests.rs b/rust/pact_ffi/tests/tests.rs index c673a06de..517f82c7a 100644 --- a/rust/pact_ffi/tests/tests.rs +++ b/rust/pact_ffi/tests/tests.rs @@ -400,7 +400,6 @@ fn add_text_comment() { } #[test_log::test] -#[allow(deprecated)] fn http_consumer_feature_test() { let consumer_name = CString::new("http-consumer").unwrap(); let provider_name = CString::new("http-provider").unwrap(); @@ -416,7 +415,7 @@ fn http_consumer_feature_test() { let query_param_matcher = CString::new("{\"value\":\"bar\",\"pact:matcher:type\":\"regex\", \"regex\":\"(bar|baz|bat)\"}").unwrap(); let request_body_with_matchers = CString::new("{\"id\": {\"value\":1,\"pact:matcher:type\":\"type\"}}").unwrap(); let response_body_with_matchers = CString::new("{\"created\": {\"value\":\"maybe\",\"pact:matcher:type\":\"regex\", \"regex\":\"(yes|no|maybe)\"}}").unwrap(); - let address = CString::new("127.0.0.1:0").unwrap(); + let address = CString::new("127.0.0.1").unwrap(); let description = CString::new("a request to test the FFI interface").unwrap(); let method = CString::new("POST").unwrap(); let query = CString::new("foo").unwrap(); @@ -437,8 +436,8 @@ fn http_consumer_feature_test() { pactffi_with_header(interaction.clone(), InteractionPart::Response, special_header.as_ptr(), 0, value_header_with_matcher.as_ptr()); pactffi_with_body(interaction.clone(), InteractionPart::Response, header.as_ptr(), response_body_with_matchers.as_ptr()); pactffi_response_status(interaction.clone(), 200); - let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false); + let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); // Mock server has started, we can't now modify the pact @@ -463,6 +462,7 @@ fn http_consumer_feature_test() { } }; + thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events let mismatches = unsafe { CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned() }; @@ -667,7 +667,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec let content_type = CString::new("image/gif").unwrap(); let path = CString::new("/upload").unwrap(); - let address = CString::new("127.0.0.1:0").unwrap(); + let address = CString::new("127.0.0.1").unwrap(); let description = CString::new("a request to test the FFI interface").unwrap(); let method = CString::new("POST").unwrap(); @@ -686,8 +686,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec // will respond with... pactffi_response_status(interaction.clone(), 201); - let port = pactffi_create_mock_server_for_pact(pact_handle.clone(), address.as_ptr(), false); - + let port = pactffi_create_mock_server_for_transport(pact_handle.clone(), address.as_ptr(), 0, null(), null()); expect!(port).to(be_greater_than(0)); let client = Client::default(); @@ -696,6 +695,7 @@ fn pactffi_with_binary_file_feature_test(specification: PactSpecification, expec .body(buffer) .send(); + thread::sleep(Duration::from_millis(100)); // Give mock server some time to update events let mismatches = unsafe { CStr::from_ptr(pactffi_mock_server_mismatches(port)).to_string_lossy().into_owned() };