Skip to content

Commit

Permalink
faster test for propertyConfigurator reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Sep 6, 2024
1 parent 4b13296 commit f629363
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import ch.qos.logback.core.status.StatusUtil;

import static ch.qos.logback.core.CoreConstants.PROPERTIES_FILE_EXTENSION;
import static ch.qos.logback.core.spi.ConfigurationEvent.newConfigurationChangeDetectorRunningEvent;
import static ch.qos.logback.core.spi.ConfigurationEvent.newConfigurationEndedSuccessfullyEvent;
import static ch.qos.logback.core.spi.ConfigurationEvent.*;

public class ReconfigureOnChangeTask extends ContextAwareBase implements Runnable {

Expand Down Expand Up @@ -75,7 +74,7 @@ public void run() {
return;
}

// ========
// ======== fuller processing below

cancelFutureInvocationsOfThisTaskInstance();
URL mainConfigurationURL = configurationWatchList.getMainURL();
Expand All @@ -95,6 +94,7 @@ private void runPropertiesConfigurator(File changedFile) {
propertyConfigurator.setContext(context);
try {
propertyConfigurator.doConfigure(changedFile);
context.fireConfigurationEvent(newPartialConfigurationEndedSuccessfullyEvent(this));
} catch (JoranException e) {
addError("Failed to reload "+ changedFile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.joran;

import ch.qos.logback.core.spi.ConfigurationEvent;
import ch.qos.logback.core.spi.ConfigurationEventListener;

import java.util.concurrent.CountDownLatch;

class PartialConfigurationEndedSuccessfullyEventListener implements ConfigurationEventListener {

CountDownLatch countDownLatch;

PartialConfigurationEndedSuccessfullyEventListener(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}

@Override
public void listen(ConfigurationEvent configurationEvent) {
switch (configurationEvent.getEventType()) {
case PARTIAL_CONFIGURATION_ENDED_SUCCESSFULLY:
System.out.println(this.toString() + "#listen PARTIAL_CONFIGURATION_ENDED_SUCCESSFULLY detected " + configurationEvent +" count="+countDownLatch.getCount());

countDownLatch.countDown();
break;
default:
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void scanWithResourceInclusion() throws JoranException, IOException, Inte
assertThatListContainsFile(fileList, innerFile);
}

@Timeout(value = TIMEOUT, unit = TimeUnit.SECONDS)
@Test
public void propertiesConfigurationTest() throws IOException, JoranException, InterruptedException {
String loggerName = "abc";
Expand All @@ -169,14 +170,21 @@ public void propertiesConfigurationTest() throws IOException, JoranException, In
configure(asBAIS(configurationStr));
Logger abcLogger = loggerContext.getLogger(loggerName);
assertEquals(Level.INFO, abcLogger.getLevel());
Thread.sleep(100);

CountDownLatch changeDetectedLatch0 = registerChangeDetectedListener();
CountDownLatch configurationDoneLatch0 = registerPartialConfigurationEndedSuccessfullyEventListener();

writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=WARN");
Thread.sleep(200);
changeDetectedLatch0.await();
configurationDoneLatch0.await();
assertEquals(Level.WARN, abcLogger.getLevel());

Thread.sleep(100);

CountDownLatch changeDetectedLatch1 = registerChangeDetectedListener();
CountDownLatch configurationDoneLatch1 = registerPartialConfigurationEndedSuccessfullyEventListener();
writeToFile(propertiesFile, PropertyConfigurator.LOGBACK_LOGGER_PREFIX + loggerName+"=ERROR");
Thread.sleep(200);
changeDetectedLatch1.await();
configurationDoneLatch1.await();
assertEquals(Level.ERROR, abcLogger.getLevel());


Expand Down Expand Up @@ -318,6 +326,13 @@ CountDownLatch registerNewReconfigurationDoneSuccessfullyListener(ReconfigureOnC
return latch;
}

CountDownLatch registerPartialConfigurationEndedSuccessfullyEventListener() {
CountDownLatch latch = new CountDownLatch(1);
PartialConfigurationEndedSuccessfullyEventListener listener = new PartialConfigurationEndedSuccessfullyEventListener(latch);
loggerContext.addConfigurationEventListener(listener);
return latch;
}

CountDownLatch registerChangeDetectedListener() {
CountDownLatch latch = new CountDownLatch(1);
ChangeDetectedListener changeDetectedListener = new ChangeDetectedListener(latch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum EventType {
CHANGE_DETECTOR_RUNNING,
CHANGE_DETECTED,
CONFIGURATION_STARTED,
PARTIAL_CONFIGURATION_ENDED_SUCCESSFULLY,
CONFIGURATION_ENDED_SUCCESSFULLY,
CONFIGURATION_ENDED_WITH_XML_PARSING_ERRORS;
}
Expand Down Expand Up @@ -62,6 +63,11 @@ static public ConfigurationEvent newConfigurationChangeDetectedEvent(Object data
static public ConfigurationEvent newConfigurationStartedEvent(Object data) {
return new ConfigurationEvent(EventType.CONFIGURATION_STARTED, data);
}
static public ConfigurationEvent newPartialConfigurationEndedSuccessfullyEvent(Object data) {
return new ConfigurationEvent(EventType.PARTIAL_CONFIGURATION_ENDED_SUCCESSFULLY, data);
}


static public ConfigurationEvent newConfigurationEndedSuccessfullyEvent(Object data) {
return new ConfigurationEvent(EventType.CONFIGURATION_ENDED_SUCCESSFULLY, data);
}
Expand Down

0 comments on commit f629363

Please sign in to comment.