1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.store.BootLockTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.store;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import junit.framework.Test;
import org.apache.derby.iapi.store.raw.data.DataFactory;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Testing file locks that prevent Derby "double boot" a.k.a "dual boot",
* i.e. two VMs booting a database concurrently, a disaster scenario.
* <p/>
* For phoneME, test that the property {@code
* derby.database.forceDatabaseLock} protects us.
* <p/>
* FIXME: If DERBY-4646 is fixed, the special handling for phoneME
* should be removed.
*/
public class BootLockTest extends BaseJDBCTestCase {
private final static String dbName = "BootLockTestDB";
private final static String dbDir = DEFAULT_DB_DIR + File.separator + dbName;
public static String minionCompleteFileName = BootLockTest.dbDir +
File.separator + "minionComplete";
private final static String dbLockFile = dbDir + File.separator +
DataFactory.DB_LOCKFILE_NAME;
private final static String dbExLockFile = dbDir + File.separator +
DataFactory.DB_EX_LOCKFILE_NAME;
private final static String servicePropertiesFileName = dbDir + File.separator + "service.properties";
private static String[] cmd = new String[]{
"org.apache.derbyTesting.functionTests.tests.store.BootLockMinion",
dbName,
};
private final static String DATA_MULTIPLE_JBMS_ON_DB = "XSDB6";
private final static String DATA_MULTIPLE_JBMS_FORCE_LOCK = "XSDB8";
// Ten minutes should hopefully be enough!
public static final int MINION_WAIT_MAX_MILLIS = 600000;
/**
* Constructor
*
* @param name
*/
public BootLockTest(String name) {
super(name);
}
/**
* Creates a suite.
*
* @return The test suite
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("BootLockTest");
suite.addTest(decorateTest());
return suite;
}
/**
* Decorate test with singleUseDatabaseDecorator and noSecurityManager.
*
* @return the decorated test
*/
private static Test decorateTest() {
Test test = new BaseTestSuite(BootLockTest.class);
if (JDBC.vmSupportsJSR169() && !isJ9Platform()) {
// PhoneME requires forceDatabaseLock
Properties props = new Properties();
props.setProperty("derby.database.forceDatabaseLock", "true");
test = new SystemPropertyTestSetup(test, props, true);
}
test = TestConfiguration.singleUseDatabaseDecorator(test, dbName);
test = SecurityManagerSetup.noSecurityManager(test);
return test;
}
public void testBootLock() throws Exception {
Process p = null;
p = execJavaCmd(cmd);
waitForMinionBoot(p,MINION_WAIT_MAX_MILLIS);
// We now know minion has booted
try {
Connection c = getConnection();
fail("Dual boot not detected: check BootLockMinion.log");
} catch (SQLException e) {
if (JDBC.vmSupportsJSR169() && !isJ9Platform()) {
// For PhoneME force database lock required
assertSQLState(
"Dual boot not detected: check BootLockMinion.log",
DATA_MULTIPLE_JBMS_FORCE_LOCK,
e);
} else {
assertSQLState(
"Dual boot not detected: check BootLockMinion.log",
DATA_MULTIPLE_JBMS_ON_DB,
e);
}
}
finally {
if (p!= null) {
p.destroy();
p.waitFor();
}
}
// Since all went OK, no need to keep the minion log file.
File minionLog = new File(DEFAULT_DB_DIR, "BootLockMinion.log");
assertTrue(minionLog.delete());
if (JDBC.vmSupportsJSR169()) {
// Delete lock files so JUnit machinery can clean up the
// one-off database without further warnings on System.err
// (phoneMe).
File db_lockfile_name = new File(dbLockFile);
File db_ex_lockfile_name = new File(dbExLockFile);
db_lockfile_name.delete();
db_ex_lockfile_name.delete();
}
}
private void waitForMinionBoot(Process p, int waitmillis) throws InterruptedException {
boolean minionComplete;
int minionExitValue;
StringBuffer failmsg = new StringBuffer();
// boolean set to true once we find the lock file
File lockFile = new File(dbLockFile);
File servicePropertiesFile = new File(servicePropertiesFileName);
// Attempt to catch any errors happening in minion for better test
// diagnosis.
BufferedReader minionSysErr = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
String minionErrLine= null ;
File checkFile = new File(minionCompleteFileName);
do {
if (checkFile.exists()) {
//The checkFile was created by BootLockMinion when we were
//sure it was finished with creating the database and making
//the connection. It will get cleaned up with the database.
return;
}
// otherwise sleep for a second and try again
waitmillis -= 1000;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
fail(e.getMessage());
}
} while (waitmillis > 0);
// If we got here, the database did not boot. Try to print the error.
failmsg.append(
"Minion did not start or boot db in " +
(MINION_WAIT_MAX_MILLIS/1000) +
" seconds.\n");
try {
minionExitValue = p.exitValue();
minionComplete =true;
failmsg.append("exitValue = " + minionExitValue);
}catch (IllegalThreadStateException e )
{
// got exception on exitValue.
// still running ..
minionComplete=false;
}
// If the process exited try to print why.
if (minionComplete) {
failmsg.append("----Process exited. Minion's stderr:\n");
do {
try {
minionErrLine = minionSysErr.readLine();
} catch (Exception ioe) {
// may not always work, so just bail out.
failmsg.append("could not read minion's stderr");
}
if (minionErrLine != null) {
failmsg.append(minionErrLine);
}
} while (minionErrLine != null);
failmsg.append("\n----Minion's stderr ended");
}
p.destroy();
p.waitFor();
fail(failmsg.toString());
}
}
|