Skip to content

Commit d737ba4

Browse files
committed
Ref #29447: add more tests for pre-initalization
1 parent f2c9e73 commit d737ba4

6 files changed

Lines changed: 250 additions & 60 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/backend/dispatcher/ProxyDispatcherService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void init() {
6060
ISeatStore seatStore = storeFactory.createSeatStore(proxySpec.getId());
6161
IDelegateProxyStore delegateProxyStore = storeFactory.createDelegateProxyStore(proxySpec.getId());
6262

63-
ProxySharingScaler proxySharingScaler = new ProxySharingScaler(seatStore, proxySpec, delegateProxyStore);
63+
ProxySharingScaler proxySharingScaler = createProxySharingScaler(seatStore, proxySpec, delegateProxyStore);
6464
createBean(proxySharingScaler, "proxySharingScaler_" + proxySpec.getId());
6565

6666
ProxySharingDispatcher proxySharingDispatcher = new ProxySharingDispatcher(proxySpec, delegateProxyStore, seatStore);
@@ -73,6 +73,10 @@ public void init() {
7373
}
7474
}
7575

76+
protected ProxySharingScaler createProxySharingScaler(ISeatStore seatStore, ProxySpec proxySpec, IDelegateProxyStore delegateProxyStore) {
77+
return new ProxySharingScaler(seatStore, proxySpec, delegateProxyStore);
78+
}
79+
7680
public IProxyDispatcher getDispatcher(String specId) {
7781
return dispatchers.get(specId);
7882
}

src/main/java/eu/openanalytics/containerproxy/backend/dispatcher/proxysharing/ProxySharingScaler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484

8585
public class ProxySharingScaler {
8686

87-
private static String publicPathPrefix = "/api/route/";
88-
private final ExecutorService executor = ExecutorServiceFactory.create("ProxySharingScaler");
89-
private final IDelegateProxyStore delegateProxyStore;
90-
private final ISeatStore seatStore;
91-
private final ProxySharingSpecExtension specExtension;
92-
private final List<String> pendingDelegatingProxies = Collections.synchronizedList(new ArrayList<>());
87+
protected static String publicPathPrefix = "/api/route/";
88+
protected final ExecutorService executor = ExecutorServiceFactory.create("ProxySharingScaler");
89+
protected final IDelegateProxyStore delegateProxyStore;
90+
protected final ISeatStore seatStore;
91+
protected final ProxySharingSpecExtension specExtension;
92+
protected final List<String> pendingDelegatingProxies = Collections.synchronizedList(new ArrayList<>());
9393
private final Logger logger = LoggerFactory.getLogger(getClass());
9494
private final ProxySpec proxySpec;
9595
private final String proxySpecHash;
@@ -476,7 +476,7 @@ private void scaleDown(long numToScaleDown) {
476476
removeDelegateProxies(delegateProxiesToRemove);
477477
}
478478

479-
private void cleanup() {
479+
protected void cleanup() {
480480
if (!lastReconcileStatus.equals(ReconcileStatus.Stable) && !lastReconcileStatus.equals(ReconcileStatus.ScaleDown)) {
481481
return;
482482
}

src/test/java/eu/openanalytics/containerproxy/test/helpers/ShinyProxyInstance.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@
2121
package eu.openanalytics.containerproxy.test.helpers;
2222

2323
import eu.openanalytics.containerproxy.ContainerProxyApplication;
24+
import eu.openanalytics.containerproxy.backend.dispatcher.DefaultProxyDispatcher;
25+
import eu.openanalytics.containerproxy.backend.dispatcher.ProxyDispatcherService;
26+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.IDelegateProxyStore;
27+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.ProxySharingScaler;
28+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.store.IProxySharingStoreFactory;
29+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.store.ISeatStore;
2430
import eu.openanalytics.containerproxy.model.runtime.Proxy;
31+
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
2532
import eu.openanalytics.containerproxy.service.ProxyService;
2633
import eu.openanalytics.containerproxy.spec.IProxySpecProvider;
2734
import eu.openanalytics.containerproxy.util.Retrying;
@@ -30,6 +37,8 @@
3037
import org.slf4j.Logger;
3138
import org.slf4j.LoggerFactory;
3239
import org.springframework.beans.BeansException;
40+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
41+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3342
import org.springframework.boot.SpringApplication;
3443
import org.springframework.context.ConfigurableApplicationContext;
3544
import org.springframework.context.annotation.Bean;
@@ -54,6 +63,7 @@ public class ShinyProxyInstance implements AutoCloseable {
5463
private final Logger logger = LoggerFactory.getLogger(getClass());
5564
private final Thread thread;
5665
private ConfigurableApplicationContext app;
66+
private boolean cleanup;
5767

5868
public ShinyProxyInstance(String configFileName, Map<String, String> properties) {
5969
this(configFileName, 7583, "demo", properties, false);
@@ -77,7 +87,7 @@ public ShinyProxyInstance(String configFileName, int port, String usernameAndPas
7787
// only works if networking is NOT internal!
7888
application.addPrimarySources(Collections.singletonList(NotInternalOnlyTestStrategyConfiguration.class));
7989
}
80-
application.addPrimarySources(Collections.singletonList(JavaMailSenderConfiguration.class));
90+
application.addPrimarySources(Collections.singletonList(TestConfiguration.class));
8191
Properties allProperties = ContainerProxyApplication.getDefaultProperties();
8292
allProperties.put("spring.config.location", "src/test/resources/" + configFileName);
8393
allProperties.put("server.port", port);
@@ -116,6 +126,18 @@ public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
116126

117127
@Override
118128
public void close() {
129+
if (cleanup) {
130+
stopAllApps();
131+
IProxySpecProvider proxySpecProvider = getBean("defaultSpecProvider", IProxySpecProvider.class);
132+
for (ProxySpec proxySpec : proxySpecProvider.getSpecs()) {
133+
try {
134+
ProxySharingScaler proxySharingScaler = getBean("proxySharingScaler_" + proxySpec.getId(), ProxySharingScaler.class);
135+
proxySharingScaler.stopAll();
136+
} catch (NoSuchBeanDefinitionException ex) {
137+
// no container sharing, ignore
138+
}
139+
}
140+
}
119141
app.stop();
120142
app.close();
121143
try {
@@ -125,6 +147,10 @@ public void close() {
125147
}
126148
}
127149

150+
public void enableCleanup() {
151+
cleanup = true;
152+
}
153+
128154
public void stopAllApps() {
129155
for (Proxy proxy: proxyService.getAllProxies()) {
130156
proxyService.stopProxy(null, proxy, true).run();
@@ -141,7 +167,7 @@ public NotInternalOnlyTestStrategy notInternalOnlyTestStrategy() {
141167

142168
}
143169

144-
public static class JavaMailSenderConfiguration {
170+
public static class TestConfiguration {
145171

146172
@Primary
147173
@Bean
@@ -169,6 +195,19 @@ public void send(MimeMessage... mimeMessages) throws MailException {
169195
}
170196
};
171197
}
198+
199+
@Primary
200+
@Bean
201+
public ProxyDispatcherService proxyDispatcherService(IProxySpecProvider proxySpecProvider,
202+
IProxySharingStoreFactory storeFactory,
203+
ConfigurableListableBeanFactory beanFactory,
204+
DefaultProxyDispatcher defaultProxyDispatcher) {
205+
return new ProxyDispatcherService(proxySpecProvider, storeFactory, beanFactory, defaultProxyDispatcher) {
206+
protected TestProxySharingScaler createProxySharingScaler(ISeatStore seatStore, ProxySpec proxySpec, IDelegateProxyStore delegateProxyStore) {
207+
return new TestProxySharingScaler(seatStore, proxySpec, delegateProxyStore);
208+
}
209+
};
210+
}
172211
}
173212

174213
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2024 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.test.helpers;
22+
23+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.IDelegateProxyStore;
24+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.ProxySharingScaler;
25+
import eu.openanalytics.containerproxy.backend.dispatcher.proxysharing.store.ISeatStore;
26+
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
27+
28+
public class TestProxySharingScaler extends ProxySharingScaler {
29+
30+
private boolean cleanup = true;
31+
32+
public TestProxySharingScaler(ISeatStore seatStore, ProxySpec proxySpec, IDelegateProxyStore delegateProxyStore) {
33+
super(seatStore, proxySpec, delegateProxyStore);
34+
}
35+
36+
public void disableCleanup() {
37+
cleanup = false;
38+
}
39+
40+
public void enableCleanup() {
41+
cleanup = true;
42+
}
43+
44+
@Override
45+
protected void cleanup() {
46+
if (cleanup) {
47+
System.out.println("Doing cleanup");
48+
super.cleanup();
49+
} else {
50+
System.out.println("Skipping cleanup");
51+
}
52+
}
53+
54+
public IDelegateProxyStore getDelegateProxyStore() {
55+
return delegateProxyStore;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)