Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,50 @@
*/
package org.apache.activemq.store.jdbc;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.HashMap;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.Locker;
import org.apache.activemq.broker.SuppressReplyException;
import org.apache.activemq.util.LeaseLockerIOExceptionHandler;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.util.Wait;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.States;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@Ignore // AMQ-9239 FIXME: mock / byte-buddy opens
public class JDBCIOExceptionHandlerMockeryTest {

private static final Logger LOG = LoggerFactory.getLogger(JDBCIOExceptionHandlerMockeryTest.class);
private HashMap<Thread, Throwable> exceptions = new HashMap<Thread, Throwable>();
private final HashMap<Thread, Throwable> exceptions = new HashMap<Thread, Throwable>();

@Test
public void testShutdownWithoutTransportRestart() throws Exception {

Mockery context = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error("unexpected exception {} on thread {}", e, t);
exceptions.put(t, e);
}
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
LOG.error("unexpected exception {} on thread {}", e, t);
exceptions.put(t, e);
});

final BrokerService brokerService = context.mock(BrokerService.class);
final JDBCPersistenceAdapter jdbcPersistenceAdapter = context.mock(JDBCPersistenceAdapter.class);
final Locker locker = context.mock(Locker.class);

final States jdbcConn = context.states("jdbc").startsAs("down");
final States broker = context.states("broker").startsAs("started");
// Create mocks
BrokerService brokerService = mock(BrokerService.class);
JDBCPersistenceAdapter jdbcPersistenceAdapter = mock(JDBCPersistenceAdapter.class);
Locker locker = mock(Locker.class);

// simulate jdbc up between hasLock and checkpoint, so hasLock fails to verify
context.checking(new Expectations() {{
allowing(brokerService).isStarted();
will(returnValue(true));
allowing(brokerService).isRestartAllowed();
will(returnValue(false));
allowing(brokerService).setSystemExitOnShutdown(with(false));
allowing(brokerService).stopAllConnectors(with(any(ServiceStopper.class)));
allowing(brokerService).getPersistenceAdapter();
will(returnValue(jdbcPersistenceAdapter));
allowing(jdbcPersistenceAdapter).allowIOResumption();
allowing(jdbcPersistenceAdapter).getLocker();
will(returnValue(locker));
allowing(locker).keepAlive();
when(jdbcConn.is("down"));
will(returnValue(true));
allowing(locker).keepAlive();
when(jdbcConn.is("up"));
will(returnValue(false));

allowing(jdbcPersistenceAdapter).checkpoint(with(true));
then(jdbcConn.is("up"));
allowing(brokerService).stop();
then(broker.is("stopped"));

}});
// Setup mock behaviors
when(brokerService.isStarted()).thenReturn(true);
when(brokerService.isRestartAllowed()).thenReturn(false);
when(brokerService.getPersistenceAdapter()).thenReturn(jdbcPersistenceAdapter);
doNothing().when(brokerService).setSystemExitOnShutdown(false);
doNothing().when(brokerService).stopAllConnectors(any(ServiceStopper.class));
when(jdbcPersistenceAdapter.getLocker()).thenReturn(locker);
when(locker.keepAlive()).thenReturn(true);

LeaseLockerIOExceptionHandler underTest = new LeaseLockerIOExceptionHandler();
underTest.setBrokerService(brokerService);
Expand All @@ -101,15 +70,7 @@ public void uncaughtException(Thread t, Throwable e) {
} catch (SuppressReplyException expected) {
}

assertTrue("broker stopped state triggered", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("broker state {}", broker);
return broker.is("stopped").isActive();
}
}));
context.assertIsSatisfied();

verify(brokerService, timeout(5000).times(1)).stop();
assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
}