Skip to content

Commit 5b5c59e

Browse files
committed
Extract a factory for limited-concurrency Coordinators
1 parent 2ea904b commit 5b5c59e

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

compiler/base/orchestrator/src/coordinator.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,34 +2777,48 @@ mod tests {
27772777
.unwrap_or(5)
27782778
});
27792779

2780-
static CONCURRENT_TEST_SEMAPHORE: Lazy<Arc<Semaphore>> =
2781-
Lazy::new(|| Arc::new(Semaphore::new(*MAX_CONCURRENT_TESTS)));
2782-
2783-
struct LimitedCoordinator<T> {
2784-
_permit: OwnedSemaphorePermit,
2785-
coordinator: Coordinator<T>,
2780+
struct CoordinatorFactory {
2781+
semaphore: Arc<Semaphore>,
27862782
}
27872783

2788-
impl<T> LimitedCoordinator<T>
2789-
where
2790-
T: Backend,
2791-
{
2792-
async fn with<F>(f: F) -> Self
2784+
impl CoordinatorFactory {
2785+
pub fn new(maximum: usize) -> Self {
2786+
Self {
2787+
semaphore: Arc::new(Semaphore::new(maximum)),
2788+
}
2789+
}
2790+
2791+
pub async fn build<B>(&self, backend: B) -> LimitedCoordinator<B>
27932792
where
2794-
F: FnOnce() -> Coordinator<T>,
2793+
B: Backend,
27952794
{
2796-
let semaphore = CONCURRENT_TEST_SEMAPHORE.clone();
2795+
let semaphore = self.semaphore.clone();
27972796
let permit = semaphore
27982797
.acquire_owned()
27992798
.await
28002799
.expect("Unable to acquire permit");
2801-
let coordinator = f();
2802-
Self {
2800+
2801+
let coordinator = Coordinator::new(backend);
2802+
2803+
LimitedCoordinator {
28032804
_permit: permit,
28042805
coordinator,
28052806
}
28062807
}
2808+
}
2809+
2810+
static TEST_COORDINATOR_FACTORY: Lazy<CoordinatorFactory> =
2811+
Lazy::new(|| CoordinatorFactory::new(*MAX_CONCURRENT_TESTS));
28072812

2813+
struct LimitedCoordinator<T> {
2814+
_permit: OwnedSemaphorePermit,
2815+
coordinator: Coordinator<T>,
2816+
}
2817+
2818+
impl<T> LimitedCoordinator<T>
2819+
where
2820+
T: Backend,
2821+
{
28082822
async fn shutdown(self) -> super::Result<T, super::Error> {
28092823
self.coordinator.shutdown().await
28102824
}
@@ -2825,11 +2839,11 @@ mod tests {
28252839
}
28262840

28272841
async fn new_coordinator_test() -> LimitedCoordinator<impl Backend> {
2828-
LimitedCoordinator::with(|| Coordinator::new(TestBackend::new())).await
2842+
TEST_COORDINATOR_FACTORY.build(TestBackend::new()).await
28292843
}
28302844

28312845
async fn new_coordinator_docker() -> LimitedCoordinator<impl Backend> {
2832-
LimitedCoordinator::with(|| Coordinator::new_docker()).await
2846+
TEST_COORDINATOR_FACTORY.build(DockerBackend(())).await
28332847
}
28342848

28352849
async fn new_coordinator() -> LimitedCoordinator<impl Backend> {

0 commit comments

Comments
 (0)