Skip to content

Commit

Permalink
chore: Fix race condition in tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 9, 2024
1 parent 42e2ba6 commit fde1e2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 3 additions & 2 deletions rust/pact_ffi/src/mock_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<Value>(config.as_str())) {
None => Ok(None),
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions rust/pact_ffi/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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::{
Expand Down Expand Up @@ -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();
Expand All @@ -796,15 +797,15 @@ 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());
pactffi_with_request(interaction.clone(), method.as_ptr(), path.as_ptr());
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));

Expand All @@ -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()
};
Expand All @@ -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();
Expand All @@ -862,15 +863,15 @@ 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());
pactffi_with_request(interaction.clone(), method.as_ptr(), path.as_ptr());
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));

Expand All @@ -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()
};
Expand Down

0 comments on commit fde1e2d

Please sign in to comment.