Skip to content

Commit

Permalink
fix config failures tests notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
elrodro83 committed Dec 13, 2024
1 parent 9776f25 commit cee2c50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/
package org.mule.runtime.core.internal.context.notification;

import static java.util.Objects.requireNonNull;
import static org.mule.runtime.core.api.config.i18n.CoreMessages.serverNotificationManagerNotEnabled;

import org.mule.runtime.core.api.MuleContext;
import static java.util.Objects.requireNonNull;

import org.mule.runtime.api.notification.Notification;
import org.mule.runtime.api.notification.NotificationListener;
import org.mule.runtime.api.notification.NotificationListenerRegistry;
import org.mule.runtime.core.api.context.notification.ServerNotificationManager;

import java.util.function.Predicate;

Expand All @@ -25,27 +26,30 @@
*/
public class DefaultNotificationListenerRegistry implements NotificationListenerRegistry {

@Inject
private MuleContext context;
private ServerNotificationManager notificationManager;

@Override
public <N extends Notification> void registerListener(NotificationListener<N> listener) {
requireNonNull(context.getNotificationManager(), serverNotificationManagerNotEnabled().getMessage());
context.getNotificationManager().addListener(listener);
requireNonNull(notificationManager, serverNotificationManagerNotEnabled().getMessage());
notificationManager.addListener(listener);
}

@Override
public <N extends Notification> void registerListener(NotificationListener<N> listener, Predicate<N> selector) {
requireNonNull(context.getNotificationManager(), serverNotificationManagerNotEnabled().getMessage());
requireNonNull(notificationManager, serverNotificationManagerNotEnabled().getMessage());
requireNonNull(selector);
context.getNotificationManager().addListenerSubscription(listener, selector);
notificationManager.addListenerSubscription(listener, selector);
}

@Override
public <N extends Notification> void unregisterListener(NotificationListener<N> listener) {
if (context.getNotificationManager() != null) {
context.getNotificationManager().removeListener(listener);
if (notificationManager != null) {
notificationManager.removeListener(listener);
}
}

@Inject
public void setNotificationManager(ServerNotificationManager notificationManager) {
this.notificationManager = notificationManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
*/
package org.mule.functional.junit4;

import static org.mule.runtime.manifest.api.MuleManifest.getMuleManifest;
import static org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP;
import static org.mule.runtime.core.api.context.notification.MuleContextNotification.CONTEXT_STARTED;
import static org.mule.runtime.core.api.extension.provider.MuleExtensionModelProvider.getExtensionModel;
import static org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded;
import static org.mule.runtime.manifest.api.MuleManifest.getMuleManifest;
import static org.mule.runtime.module.extension.internal.loader.java.AbstractJavaExtensionModelLoader.TYPE_PROPERTY_NAME;
import static org.mule.runtime.module.extension.internal.loader.java.AbstractJavaExtensionModelLoader.VERSION;

Expand All @@ -19,16 +18,14 @@
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

import org.mule.runtime.api.dsl.DslResolvingContext;
import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.meta.model.ExtensionModel;
import org.mule.runtime.api.metadata.ExpressionLanguageMetadataService;
import org.mule.runtime.api.notification.IntegerAction;
import org.mule.runtime.api.notification.NotificationListenerRegistry;
import org.mule.runtime.api.util.concurrent.Latch;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.ConfigurationBuilder;
Expand All @@ -37,8 +34,6 @@
import org.mule.runtime.core.api.context.DefaultMuleContextFactory;
import org.mule.runtime.core.api.context.MuleContextBuilder;
import org.mule.runtime.core.api.context.MuleContextFactory;
import org.mule.runtime.core.api.context.notification.MuleContextNotification;
import org.mule.runtime.core.api.context.notification.MuleContextNotificationListener;
import org.mule.runtime.core.internal.context.MuleContextWithRegistry;
import org.mule.runtime.extension.api.loader.ExtensionModelLoader;
import org.mule.runtime.module.extension.internal.loader.java.CraftedExtensionModelLoader;
Expand All @@ -55,7 +50,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Rule;

Expand Down Expand Up @@ -94,7 +88,8 @@ protected void doConfigure(MuleContext muleContext) throws Exception {
builders.add(configurationBuilder);
builders.add(testServicesConfigurationBuilder);
builders.add(new TestPolicyProviderConfigurationBuilder());
builders.add(new TestNotificationListenerRegistryConfigurationBuilder());
final Latch contextStartedLatch = new Latch();
builders.add(new TestNotificationListenerRegistryConfigurationBuilder(contextStartedLatch));
MuleContextBuilder contextBuilder = MuleContextBuilder.builder(APP);
final DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration();
muleConfiguration.setId(AbstractConfigurationFailuresTestCase.class.getSimpleName());
Expand All @@ -104,27 +99,9 @@ protected void doConfigure(MuleContext muleContext) throws Exception {
contextBuilder.setExecutionClassLoader(new ClassLoader(currentThread().getContextClassLoader()) {});
MuleContextWithRegistry muleContext =
(MuleContextWithRegistry) muleContextFactory.createMuleContext(builders, contextBuilder);
final AtomicReference<Latch> contextStartedLatch = new AtomicReference<>();
contextStartedLatch.set(new Latch());
NotificationListenerRegistry notificationListenerRegistry =
muleContext.getRegistry().get(NotificationListenerRegistry.REGISTRY_KEY);
notificationListenerRegistry.registerListener(new MuleContextNotificationListener<MuleContextNotification>() {

@Override
public boolean isBlocking() {
return false;
}

@Override
public void onNotification(MuleContextNotification notification) {
if (new IntegerAction(CONTEXT_STARTED).equals(notification.getAction())) {
contextStartedLatch.get().countDown();
}
}
});
muleContext.start();
try {
assertThat(contextStartedLatch.get().await(20, SECONDS), is(true));
assertThat(contextStartedLatch.await(20, SECONDS), is(true));
} finally {
muleContext.stop();
muleContext.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,47 @@
*/
package org.mule.tck.config;

import static org.mule.runtime.core.api.context.notification.MuleContextNotification.CONTEXT_STARTED;

import org.mule.runtime.api.notification.IntegerAction;
import org.mule.runtime.api.notification.NotificationListenerRegistry;
import org.mule.runtime.api.util.concurrent.Latch;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.config.builders.AbstractConfigurationBuilder;
import org.mule.runtime.core.api.context.notification.MuleContextNotification;
import org.mule.runtime.core.api.context.notification.MuleContextNotificationListener;
import org.mule.runtime.core.internal.context.MuleContextWithRegistry;
import org.mule.runtime.core.internal.context.notification.DefaultNotificationListenerRegistry;
import org.mule.runtime.core.privileged.registry.RegistrationException;

public class TestNotificationListenerRegistryConfigurationBuilder extends AbstractConfigurationBuilder {

private Latch contextStartedLatch;

public TestNotificationListenerRegistryConfigurationBuilder(Latch contextStartedLatch) {
this.contextStartedLatch = contextStartedLatch;
}

@Override
protected void doConfigure(MuleContext muleContext) throws RegistrationException {
final DefaultNotificationListenerRegistry notificationListenerRegistry = new DefaultNotificationListenerRegistry();
notificationListenerRegistry.setNotificationManager(muleContext.getNotificationManager());
notificationListenerRegistry.registerListener(new MuleContextNotificationListener<>() {

@Override
public boolean isBlocking() {
return false;
}

@Override
public void onNotification(MuleContextNotification notification) {
if (new IntegerAction(CONTEXT_STARTED).equals(notification.getAction())) {
contextStartedLatch.countDown();
}
}
});

((MuleContextWithRegistry) muleContext).getRegistry().registerObject(NotificationListenerRegistry.REGISTRY_KEY,
new DefaultNotificationListenerRegistry());
notificationListenerRegistry);
}
}

0 comments on commit cee2c50

Please sign in to comment.