Simultaenously build Docker images used in tests

This commit is contained in:
Luke Parker
2023-11-27 01:10:23 -05:00
parent 571195bfda
commit 292263b21e
23 changed files with 639 additions and 526 deletions

View File

@@ -13,10 +13,14 @@ use dockertest::{
PullPolicy, Image, LogAction, LogPolicy, LogSource, LogOptions, TestBodySpecification,
};
pub fn docker_name() -> String {
"serai-dev-message-queue".to_string()
}
pub type MessageQueuePrivateKey = <Ristretto as Ciphersuite>::F;
pub fn instance(
pub async fn instance(
) -> (MessageQueuePrivateKey, HashMap<NetworkId, MessageQueuePrivateKey>, TestBodySpecification) {
serai_docker_tests::build("message-queue".to_string());
serai_docker_tests::build("message-queue".to_string()).await;
let coord_key = <Ristretto as Ciphersuite>::F::random(&mut OsRng);
let priv_keys = HashMap::from([
@@ -26,7 +30,7 @@ pub fn instance(
]);
let composition = TestBodySpecification::with_image(
Image::with_repository("serai-dev-message-queue").pull_policy(PullPolicy::Never),
Image::with_repository(docker_name()).pull_policy(PullPolicy::Never),
)
.set_log_options(Some(LogOptions {
action: LogAction::Forward,
@@ -58,8 +62,8 @@ pub fn instance(
(coord_key, priv_keys, composition)
}
#[test]
fn basic_functionality() {
#[tokio::test]
async fn basic_functionality() {
use zeroize::Zeroizing;
use dockertest::DockerTest;
@@ -67,99 +71,107 @@ fn basic_functionality() {
use serai_message_queue::{Service, Metadata, client::MessageQueue};
let mut test = DockerTest::new().with_network(dockertest::Network::Isolated);
let (coord_key, priv_keys, composition) = instance();
let (coord_key, priv_keys, composition) = instance().await;
test.provide_container(composition);
test.run(|ops| async move {
tokio::time::timeout(core::time::Duration::from_secs(60), async move {
// Sleep for a second for the message-queue to boot
// It isn't an error to start immediately, it just silences an error
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
test
.run_async(|ops| async move {
tokio::time::timeout(core::time::Duration::from_secs(60), async move {
// Sleep for a second for the message-queue to boot
// It isn't an error to start immediately, it just silences an error
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
let rpc = ops.handle("serai-dev-message-queue").host_port(2287).unwrap();
let rpc = rpc.0.to_string() + ":" + &rpc.1.to_string();
let rpc = ops.handle("serai-dev-message-queue").host_port(2287).unwrap();
let rpc = rpc.0.to_string() + ":" + &rpc.1.to_string();
// Queue some messages
let coordinator =
MessageQueue::new(Service::Coordinator, rpc.clone(), Zeroizing::new(coord_key));
coordinator
.queue(
Metadata {
from: Service::Coordinator,
to: Service::Processor(NetworkId::Bitcoin),
intent: b"intent".to_vec(),
},
b"Hello, World!".to_vec(),
)
.await;
// Queue this twice, which message-queue should de-duplicate
for _ in 0 .. 2 {
// Queue some messages
let coordinator =
MessageQueue::new(Service::Coordinator, rpc.clone(), Zeroizing::new(coord_key));
coordinator
.queue(
Metadata {
from: Service::Coordinator,
to: Service::Processor(NetworkId::Bitcoin),
intent: b"intent 2".to_vec(),
intent: b"intent".to_vec(),
},
b"Hello, World, again!".to_vec(),
b"Hello, World!".to_vec(),
)
.await;
}
// Successfully get it
let bitcoin = MessageQueue::new(
Service::Processor(NetworkId::Bitcoin),
rpc.clone(),
Zeroizing::new(priv_keys[&NetworkId::Bitcoin]),
);
let msg = bitcoin.next(Service::Coordinator).await;
assert_eq!(msg.from, Service::Coordinator);
assert_eq!(msg.id, 0);
assert_eq!(&msg.msg, b"Hello, World!");
// Queue this twice, which message-queue should de-duplicate
for _ in 0 .. 2 {
coordinator
.queue(
Metadata {
from: Service::Coordinator,
to: Service::Processor(NetworkId::Bitcoin),
intent: b"intent 2".to_vec(),
},
b"Hello, World, again!".to_vec(),
)
.await;
}
// If we don't ack it, it should continue to be returned
assert_eq!(msg, bitcoin.next(Service::Coordinator).await);
// Successfully get it
let bitcoin = MessageQueue::new(
Service::Processor(NetworkId::Bitcoin),
rpc.clone(),
Zeroizing::new(priv_keys[&NetworkId::Bitcoin]),
);
let msg = bitcoin.next(Service::Coordinator).await;
assert_eq!(msg.from, Service::Coordinator);
assert_eq!(msg.id, 0);
assert_eq!(&msg.msg, b"Hello, World!");
// Acknowledging it should yield the next message
bitcoin.ack(Service::Coordinator, 0).await;
// If we don't ack it, it should continue to be returned
assert_eq!(msg, bitcoin.next(Service::Coordinator).await);
let next_msg = bitcoin.next(Service::Coordinator).await;
assert!(msg != next_msg);
assert_eq!(next_msg.from, Service::Coordinator);
assert_eq!(next_msg.id, 1);
assert_eq!(&next_msg.msg, b"Hello, World, again!");
bitcoin.ack(Service::Coordinator, 1).await;
// Acknowledging it should yield the next message
bitcoin.ack(Service::Coordinator, 0).await;
// No further messages should be available
tokio::time::timeout(core::time::Duration::from_secs(10), bitcoin.next(Service::Coordinator))
.await
.unwrap_err();
let next_msg = bitcoin.next(Service::Coordinator).await;
assert!(msg != next_msg);
assert_eq!(next_msg.from, Service::Coordinator);
assert_eq!(next_msg.id, 1);
assert_eq!(&next_msg.msg, b"Hello, World, again!");
bitcoin.ack(Service::Coordinator, 1).await;
// Queueing to a distinct processor should work, with a unique ID
coordinator
.queue(
Metadata {
from: Service::Coordinator,
to: Service::Processor(NetworkId::Monero),
// Intents should be per-from-to, making this valid
intent: b"intent".to_vec(),
},
b"Hello, World!".to_vec(),
// No further messages should be available
tokio::time::timeout(
core::time::Duration::from_secs(10),
bitcoin.next(Service::Coordinator),
)
.await;
let monero = MessageQueue::new(
Service::Processor(NetworkId::Monero),
rpc,
Zeroizing::new(priv_keys[&NetworkId::Monero]),
);
assert_eq!(monero.next(Service::Coordinator).await.id, 0);
monero.ack(Service::Coordinator, 0).await;
tokio::time::timeout(core::time::Duration::from_secs(10), monero.next(Service::Coordinator))
.await
.unwrap_err();
// Queueing to a distinct processor should work, with a unique ID
coordinator
.queue(
Metadata {
from: Service::Coordinator,
to: Service::Processor(NetworkId::Monero),
// Intents should be per-from-to, making this valid
intent: b"intent".to_vec(),
},
b"Hello, World!".to_vec(),
)
.await;
let monero = MessageQueue::new(
Service::Processor(NetworkId::Monero),
rpc,
Zeroizing::new(priv_keys[&NetworkId::Monero]),
);
assert_eq!(monero.next(Service::Coordinator).await.id, 0);
monero.ack(Service::Coordinator, 0).await;
tokio::time::timeout(
core::time::Duration::from_secs(10),
monero.next(Service::Coordinator),
)
.await
.unwrap_err();
})
.await
.unwrap();
})
.await
.unwrap();
});
.await;
}