I believe I figure this out. The test class is XATest.java. It looks like
that is run without any specific policy file. So borrowing from another
class, I added:
//create own policy file
private static final String POLICY_FILE_NAME =
"org/apache/derbyTesting/functionTests/tests/jdbcapi/XATest.policy";
and change
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("XATest");
// no XA for JSR169
if (JDBC.vmSupportsJSR169())
return suite;
suite.addTest(baseSuite("XATest:embedded"));
suite.addTest(TestConfiguration
.clientServerDecorator(baseSuite("XATest:client")));
Test test = DatabasePropertyTestSetup.setLockTimeouts(suite, 3, 5);
test = decorateWithPolicy(test);
return test;
}
And added
private static Test decorateWithPolicy(Test test) {
//
// Install a security manager using the initial policy file.
//
return new SecurityManagerSetup(test, POLICY_FILE_NAME);
}
I copied "ServerPropertiesTest.policy" to "XATest.policy" and added one line:
to the
grant codeBase "${derbyTesting.testjar}derbyTesting.jar" {
..
permission java.lang.management.ManagementPermission "monitor";
}
I don't know if I can remove the other "grant" sections or not. But at least
my test that I added to XATest.java causes the deadlock and the watchdog
detects the deadlock and then does a System.exit(1) to kill the test for now.
From: Bergquist, Brett [mailto:[email protected]]
Sent: Friday, June 24, 2016 5:06 PM
To: [email protected]
Subject: RE: How should one handle a test case that when it fails will cause a
deadlock in Derby Netwok Server
It looks like the test will be run in embed mode and not be run using the
network server.
I have coded up a watchdog that looks like:
class DeadlockWatchdog implements Runnable {
private boolean stopped = false;
private long timeout;
public DeadlockWatchdog(long timeout) {
this.timeout = timeout;
}
public synchronized void start() {
stopped = false;
Thread t = new Thread(this, "WATCHDOG");
t.setDaemon(true);
t.start();
}
public synchronized void stop() {
stopped = true;
notifyAll();
}
public synchronized void run() {
final long until = System.currentTimeMillis() + timeout;
long now;
while (!stopped && until > (now = System.currentTimeMillis())) {
try {
wait(until - now);
} catch (InterruptedException e) {
}
}
if (!stopped) {
try {
boolean res = AccessController.doPrivileged(
new PrivilegedExceptionAction<Boolean>() {
public Boolean run() throws IOException,
MalformedObjectNameException, InstanceNotFoundException, MBeanException,
ReflectionException {
return checkForDeadlock();
}
});
if (res) {
System.err.println("Deadlock detected");
System.exit(1);
}
} catch (Exception x) {
System.err.println("Watchdog failed: " + x.toString());
System.exit(1);
}
}
}
boolean checkForDeadlock() throws MalformedObjectNameException,
InstanceNotFoundException, MBeanException, ReflectionException, IOException {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long[] findDeadlockedThreads = bean.findDeadlockedThreads();
if (null != findDeadlockedThreads && 0 != findDeadlockedThreads.length)
{
return true;
}
return false;
}
}
But this is failing with an "java.security.AccessControlException: access
denied ("java.lang.management.ManagementPermission" "monitor")
Any ideas? There is probably a security policy that I need to update but have
no idea where that would be?
From: Bergquist, Brett [mailto:[email protected]]
Sent: Friday, June 24, 2016 4:08 PM
To: [email protected]<mailto:[email protected]>
Subject: How should one handle a test case that when it fails will cause a
deadlock in Derby Netwok Server
I am writing a test case for
https://issues.apache.org/jira/browse/DERBY-6879
Which if written correctly will cause a deadlock in the derby network server.
How should this test case recover from this? System.exit(1)?
The test case will never continue because of the deadlock and the derby network
server that the test case is running against will not be able to be shutdown
either. Probably Derby should have a piece of self-monitoring code that looks
periodically for a deadlock and does a "panic" type of shutdown.
________________________________
Canoga Perkins
20600 Prairie Street
Chatsworth, CA 91311
(818) 718-6300
This e-mail and any attached document(s) is confidential and is intended only
for the review of the party to whom it is addressed. If you have received this
transmission in error, please notify the sender immediately and discard the
original message and any attachment(s).
________________________________
Canoga Perkins
20600 Prairie Street
Chatsworth, CA 91311
(818) 718-6300
This e-mail and any attached document(s) is confidential and is intended only
for the review of the party to whom it is addressed. If you have received this
transmission in error, please notify the sender immediately and discard the
original message and any attachment(s).
________________________________
Canoga Perkins
20600 Prairie Street
Chatsworth, CA 91311
(818) 718-6300
This e-mail and any attached document(s) is confidential and is intended only
for the review of the party to whom it is addressed. If you have received this
transmission in error, please notify the sender immediately and discard the
original message and any attachment(s).