Created
May 11, 2020 04:53
-
-
Save gterzian/113814dfbd44f5fa3f04b53dae4aedd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A map of keeping count of pending order per customer, | |
// owned by the "basket" service. | |
let mut pending_orders = HashMap::new(); | |
for _ in 0..4 { | |
let customer_id = CustomerId(Uuid::new_v4()); | |
match pending_orders.entry(customer_id) { | |
Entry::Vacant(entry) => { | |
entry.insert(1); | |
} | |
Entry::Occupied(mut entry) => { | |
*entry.get_mut() += 1; | |
} | |
}; | |
let _ = order_request_sender.send(OrderRequest::NewOrder(customer_id)); | |
} | |
// Have the main thread of the test double-up as the "basket" service. | |
loop { | |
match order_result_receiver.recv() { | |
Ok(OrderResult(customer_id, _succeeded)) => { | |
match pending_orders.entry(customer_id) { | |
Entry::Vacant(_) => { | |
panic!("Got an order confirmation for an unknonw customer."); | |
} | |
Entry::Occupied(mut entry) => { | |
if *entry.get() == 1 { | |
entry.remove_entry(); | |
} else { | |
*entry.get_mut() -= 1; | |
} | |
} | |
}; | |
if pending_orders.is_empty() { | |
let _ = order_request_sender.send(OrderRequest::ShutDown); | |
break; | |
} | |
} | |
_ => panic!("Error receiving an order result."), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment