2121package eu .openanalytics .containerproxy .test .helpers ;
2222
2323import 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 ;
2430import eu .openanalytics .containerproxy .model .runtime .Proxy ;
31+ import eu .openanalytics .containerproxy .model .spec .ProxySpec ;
2532import eu .openanalytics .containerproxy .service .ProxyService ;
2633import eu .openanalytics .containerproxy .spec .IProxySpecProvider ;
2734import eu .openanalytics .containerproxy .util .Retrying ;
3037import org .slf4j .Logger ;
3138import org .slf4j .LoggerFactory ;
3239import org .springframework .beans .BeansException ;
40+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
41+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
3342import org .springframework .boot .SpringApplication ;
3443import org .springframework .context .ConfigurableApplicationContext ;
3544import 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}
0 commit comments