Skip to content

Commit

Permalink
ARTEMIS-4391 tests: rework AssertionLoggerHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
amarkevich authored and clebertsuconic committed Aug 17, 2023
1 parent 999789b commit 67f9c9d
Show file tree
Hide file tree
Showing 54 changed files with 793 additions and 1,090 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,22 @@ public void shouldCallOnErrorOnEachCallbackWithExceptions() {
}

@Test
public void shouldLogOnDoneForEachExceptions() {
AssertionLoggerHandler.startCapture();
try {
public void shouldLogOnDoneForEachExceptions() throws Exception {
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
final CountingIOCallback countingIOCallback = new CountingIOCallback(true);
final DelegateCallback callback = new DelegateCallback(Collections.singleton(countingIOCallback));
callback.done();
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ142024"));
} finally {
AssertionLoggerHandler.stopCapture();
Assert.assertTrue(loggerHandler.findText("AMQ142024"));
}
}

@Test
public void shouldLogOnErrorForEachExceptions() {
AssertionLoggerHandler.startCapture();
try {
public void shouldLogOnErrorForEachExceptions() throws Exception {
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
final CountingIOCallback countingIOCallback = new CountingIOCallback(true);
final DelegateCallback callback = new DelegateCallback(Collections.singleton(countingIOCallback));
callback.onError(0, "not a real error");
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ142025"));
} finally {
AssertionLoggerHandler.stopCapture();
Assert.assertTrue(loggerHandler.findText("AMQ142025"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,47 +97,39 @@ public void exceptions() {
}

@Test
public void longList() {
AssertionLoggerHandler.startCapture(false, true);
try {
public void longList() throws Exception {
try (AssertionLoggerHandler logHandler = new AssertionLoggerHandler()) {
SimpleBundle.MESSAGES.longParameters("1", "2", "3", "4", "5");
Assert.assertTrue("parameter not found", AssertionLoggerHandler.findText("p1"));
Assert.assertTrue("parameter not found", AssertionLoggerHandler.findText("p2"));
Assert.assertTrue("parameter not found", AssertionLoggerHandler.findText("p3"));
Assert.assertTrue("parameter not found", AssertionLoggerHandler.findText("p4"));
Assert.assertTrue("parameter not found", AssertionLoggerHandler.findText("p5"));
Assert.assertFalse("{}", AssertionLoggerHandler.findText("{}"));
} finally {
AssertionLoggerHandler.stopCapture();
Assert.assertTrue("parameter not found", logHandler.findText("p1"));
Assert.assertTrue("parameter not found", logHandler.findText("p2"));
Assert.assertTrue("parameter not found", logHandler.findText("p3"));
Assert.assertTrue("parameter not found", logHandler.findText("p4"));
Assert.assertTrue("parameter not found", logHandler.findText("p5"));
Assert.assertFalse("{}", logHandler.findText("{}"));
}
}


@Test
public void onlyException() {
try {
AssertionLoggerHandler.startCapture(false, false);

public void onlyException() throws Exception {
try (AssertionLoggerHandler logHandler = new AssertionLoggerHandler()) {
SimpleBundle.MESSAGES.onlyException(createMyExceptionBreadcrumbMethod("MSG7777"));

Assert.assertTrue(AssertionLoggerHandler.findText("TST14"));
Assert.assertFalse(AssertionLoggerHandler.findText("MSG7777"));

AssertionLoggerHandler.clear();
Assert.assertTrue(logHandler.findText("TST14"));
Assert.assertFalse(logHandler.findText("MSG7777"));
}

AssertionLoggerHandler.startCapture(false, true);
try (AssertionLoggerHandler logHandler = new AssertionLoggerHandler(true)) {
SimpleBundle.MESSAGES.onlyException(createMyExceptionBreadcrumbMethod("MSG7777"));
Assert.assertTrue(AssertionLoggerHandler.findText("TST14"));
Assert.assertTrue(AssertionLoggerHandler.findText("MSG7777"));
Assert.assertTrue(AssertionLoggerHandler.findText("createMyExceptionBreadcrumbMethod"));
} finally {
AssertionLoggerHandler.stopCapture();
Assert.assertTrue(logHandler.findText("TST14"));
Assert.assertTrue(logHandler.findTrace("MSG7777"));
Assert.assertTrue(logHandler.findTrace("createMyExceptionBreadcrumbMethod"));
}
}


// I'm doing it on a method just to assert if this method will appear on the stack trace
private MyException createMyExceptionBreadcrumbMethod(String message) {
private static MyException createMyExceptionBreadcrumbMethod(String message) {
return new MyException(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,8 @@ public void testAddressSettingsPageLimit() throws Throwable {

@Test
public void testAddressSettingsPageLimitInvalidConfiguration1() throws Throwable {
AssertionLoggerHandler.startCapture();
runAfter(AssertionLoggerHandler::stopCapture);
AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler();
runAfter(() -> loggerHandler.close());
ConfigurationImpl configuration = new ConfigurationImpl();

Properties properties = new Properties();
Expand All @@ -1245,14 +1245,14 @@ public void testAddressSettingsPageLimitInvalidConfiguration1() throws Throwable
Assert.assertEquals(null, storeImpl.getPageLimitMessages());
Assert.assertEquals(null, storeImpl.getPageLimitBytes());
Assert.assertEquals(null, storeImpl.getPageFullMessagePolicy());
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224125"));
Assert.assertTrue(loggerHandler.findText("AMQ224125"));
}


@Test
public void testAddressSettingsPageLimitInvalidConfiguration2() throws Throwable {
AssertionLoggerHandler.startCapture();
runAfter(AssertionLoggerHandler::stopCapture);
AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler();
runAfter(() -> loggerHandler.close());
ConfigurationImpl configuration = new ConfigurationImpl();

Properties properties = new Properties();
Expand All @@ -1276,13 +1276,13 @@ public void testAddressSettingsPageLimitInvalidConfiguration2() throws Throwable
Assert.assertEquals(null, storeImpl.getPageLimitMessages());
Assert.assertEquals(null, storeImpl.getPageLimitBytes());
Assert.assertEquals(null, storeImpl.getPageFullMessagePolicy());
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224125"));
Assert.assertTrue(loggerHandler.findText("AMQ224125"));
}

@Test
public void testAddressSettingsPageLimitInvalidConfiguration3() throws Throwable {
AssertionLoggerHandler.startCapture();
runAfter(AssertionLoggerHandler::stopCapture);
AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler();
runAfter(() -> loggerHandler.close());
ConfigurationImpl configuration = new ConfigurationImpl();

Properties properties = new Properties();
Expand All @@ -1306,13 +1306,13 @@ public void testAddressSettingsPageLimitInvalidConfiguration3() throws Throwable
Assert.assertEquals(null, storeImpl.getPageLimitMessages());
Assert.assertEquals(null, storeImpl.getPageLimitBytes());
Assert.assertEquals(null, storeImpl.getPageFullMessagePolicy());
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224125"));
Assert.assertTrue(loggerHandler.findText("AMQ224125"));
}

@Test
public void testAddressSettingsPageLimitInvalidConfiguration4() throws Throwable {
AssertionLoggerHandler.startCapture();
runAfter(AssertionLoggerHandler::stopCapture);
AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler();
runAfter(() -> loggerHandler.close());
ConfigurationImpl configuration = new ConfigurationImpl();

Properties properties = new Properties();
Expand All @@ -1336,7 +1336,7 @@ public void testAddressSettingsPageLimitInvalidConfiguration4() throws Throwable
Assert.assertEquals(null, storeImpl.getPageLimitMessages());
Assert.assertEquals(null, storeImpl.getPageLimitBytes());
Assert.assertEquals(null, storeImpl.getPageFullMessagePolicy());
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224124"));
Assert.assertTrue(loggerHandler.findText("AMQ224124"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,16 +967,13 @@ public MeterRegistry getRegistry() {
@Test
public void testValidateCache() throws Exception {

AssertionLoggerHandler.startCapture(true);
try {
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
FileConfiguration fc = new FileConfiguration();
FileDeploymentManager deploymentManager = new FileDeploymentManager(getConfigurationName());
deploymentManager.addDeployable(fc);
deploymentManager.readConfiguration();
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224117"));
Assert.assertEquals(1, AssertionLoggerHandler.countText("AMQ224117"));
} finally {
AssertionLoggerHandler.stopCapture();
Assert.assertTrue(loggerHandler.findText("AMQ224117"));
Assert.assertEquals(1, loggerHandler.countText("AMQ224117"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.apache.activemq.artemis.core.deployers.impl.FileConfigurationParser;
import org.apache.activemq.artemis.logs.AssertionLoggerHandler;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
Expand All @@ -32,30 +30,17 @@
*/
public class WrongRoleFileConfigurationParserTest extends ActiveMQTestBase {

@BeforeClass
public static void prepareLogger() {
AssertionLoggerHandler.startCapture();
}

@AfterClass
public static void clearLogger() {
AssertionLoggerHandler.stopCapture();
}

/**
*
*
*
*/
@Test
public void testParsingDefaultServerConfig() throws Exception {
FileConfigurationParser parser = new FileConfigurationParser();
ByteArrayInputStream input = new ByteArrayInputStream(configuration.getBytes(StandardCharsets.UTF_8));
parser.parseMainConfig(input);
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
FileConfigurationParser parser = new FileConfigurationParser();
ByteArrayInputStream input = new ByteArrayInputStream(configuration.getBytes(StandardCharsets.UTF_8));
parser.parseMainConfig(input);

// Using the code only because I don't want a test failing just for someone editing Log text
assertTrue(AssertionLoggerHandler.findText("AMQ222177", "create-durable-queue"));
assertTrue(AssertionLoggerHandler.findText("AMQ222177", "delete-durable-queue"));
// Using the code only because I don't want a test failing just for someone editing Log text
assertTrue(loggerHandler.findText("AMQ222177", "create-durable-queue"));
assertTrue(loggerHandler.findText("AMQ222177", "delete-durable-queue"));
}
}

private static final String configuration = "<configuration xmlns=\"urn:activemq\"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,9 @@ public void testMaxZero() throws Exception {

@Test
public void testMoveOverPaging() throws Exception {
AssertionLoggerHandler.startCapture();

ExecutorService threadPool = Executors.newCachedThreadPool();
try {
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
manager.setMaxFolders(3);
for (int i = 1; i <= 10; i++) {
HierarchicalRepository<AddressSettings> addressSettings = new HierarchicalObjectRepository<>();
Expand Down Expand Up @@ -325,9 +324,8 @@ public void testMoveOverPaging() throws Exception {
Assert.assertEquals(Math.min(i, manager.getMaxFolders()), manager.getNumberOfFolders());
}

Assert.assertFalse("The loggers are complaining about address.txt", AssertionLoggerHandler.findText("address.txt"));
Assert.assertFalse("The loggers are complaining about address.txt", loggerHandler.findText("address.txt"));
} finally {
AssertionLoggerHandler.stopCapture();
threadPool.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public void testTooLongToStartLOG() throws Exception {

private void testTooLongToStart(CriticalAnalyzerPolicy policy) throws Exception {

AssertionLoggerHandler.startCapture();
try {
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {

ConfigurationImpl configuration = new ConfigurationImpl();
configuration.setCriticalAnalyzerPolicy(policy);
Expand All @@ -58,11 +57,9 @@ private void testTooLongToStart(CriticalAnalyzerPolicy policy) throws Exception
// this will be faking a condition
server.setState(ActiveMQServer.SERVER_STATE.STARTING);
CriticalAnalyzerAccessor.fireActions(server.getCriticalAnalyzer(), new CriticalComponentImpl(server.getCriticalAnalyzer(), 2));
Assert.assertTrue(AssertionLoggerHandler.findText("224116"));
Assert.assertTrue(loggerHandler.findText("224116"));
Assert.assertEquals(ActiveMQServer.SERVER_STATE.STARTING, server.getState()); // should not be changed
server.stop();
} finally {
AssertionLoggerHandler.stopCapture();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@ private LeaseLock lock(long acquireMillis, long queryTimeoutMillis) {
}
}

private LeaseLock lock(long acquireMillis, long queryTimeoutMillis, long allowedTimeDiff) {
try {
return JdbcSharedStateManager
.createLiveLock(
UUID.randomUUID().toString(),
jdbcSharedStateManager.getJdbcConnectionProvider(),
sqlProvider,
acquireMillis,
queryTimeoutMillis,
allowedTimeDiff);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

@Before
public void createLockTable() throws Exception {
dbConf = createDefaultDatabaseStorageConfiguration();
Expand Down Expand Up @@ -418,9 +403,7 @@ public void shouldNotifyOnceLostLockIfStopped() throws Exception {
}

@Test
public void validateTimeDiffsOnLeaseLock() {
AssertionLoggerHandler.startCapture();
runAfter(AssertionLoggerHandler::stopCapture);
public void validateTimeDiffsOnLeaseLock() throws Exception {

AtomicInteger diff = new AtomicInteger(0);

Expand All @@ -435,18 +418,22 @@ protected long fetchDatabaseTime(Connection connection) throws SQLException {
}
};

diff.set(10_000);
hackLock.dbCurrentTimeMillis();
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224118"));
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
diff.set(10_000);
hackLock.dbCurrentTimeMillis();
Assert.assertTrue(loggerHandler.findText("AMQ224118"));
}

diff.set(-10_000);
AssertionLoggerHandler.clear();
hackLock.dbCurrentTimeMillis();
Assert.assertTrue(AssertionLoggerHandler.findText("AMQ224118"));
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
diff.set(-10_000);
hackLock.dbCurrentTimeMillis();
Assert.assertTrue(loggerHandler.findText("AMQ224118"));
}

diff.set(0);
AssertionLoggerHandler.clear();
hackLock.dbCurrentTimeMillis();
Assert.assertFalse(AssertionLoggerHandler.findText("AMQ224118"));
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
diff.set(0);
hackLock.dbCurrentTimeMillis();
Assert.assertFalse(loggerHandler.findText("AMQ224118"));
}
}
}

0 comments on commit 67f9c9d

Please sign in to comment.