/** * Start the database. */ public void startDb() { Map<String, String> databaseOptions = new HashMap<>(); databaseOptions.put(MysqldResourceI.PORT, Integer.toString(port)); mysqldResource = new MysqldResource(new File(databaseDir, databaseName)); mysqldResource.start("embedded-mysqld-db-thread-" + System.currentTimeMillis(), databaseOptions); if (!mysqldResource.isRunning()) { throw new RuntimeException("Failed to start embedded MySQL DB!"); } LOG.debug("MySQL started successfully"); }
public void testDestroyShell() { /** TODO: improve this test */ startMysql("DestroyMe"); assertTrue(mysqldResource.isRunning()); mysqldResource.destroyShell(); if (mysqldResource.isRunning()) { new MysqldResource(fileUtil.nullFile()).shutdown(); } }
@Override protected String[] constructArgs(@SuppressWarnings("rawtypes") Map mysqldArgs) { String[] args = super.constructArgs(mysqldArgs); String[] launcherArgs = new String[args.length + 1]; launcherArgs[0] = args[0].replaceAll("mysqld", "launcher"); System.arraycopy(args, 0, launcherArgs, 1, args.length); return launcherArgs; }
String getResourceName() { String dir = osName + "-" + osArch; String name = executableName(); return getVersionDir() + Streams.RESOURCE_SEPARATOR + dir + Streams.RESOURCE_SEPARATOR + name; }
public synchronized static boolean shutdown(File baseDir, File dataDir, int killDelay) { MysqldResource mysqld = new MysqldResource(baseDir, dataDir); if (killDelay > 0) { mysqld.setKillDelay(killDelay); } mysqld.shutdown(); return mysqld.isRunning(); } }
String getResourceName() { String dir = os_arch(); String name = executableName(); return getVersionDir() + Streams.RESOURCE_SEPARATOR + dir + Streams.RESOURCE_SEPARATOR + name; }
boolean haveShell = (getShell() != null); if (!pidFile().exists() && !haveShell) { printMessage("Mysqld not running. No file: " + pidFile()); return; printMessage("stopping mysqld (process: " + pid() + ")"); issueNormalKill(); if (processRunning()) { issueForceKill(); if (shellRunning()) { destroyShell(); setShell(null); if (processRunning()) { printWarning("Process " + pid + "still running; not deleting " + pidFile()); } else { threads.pause(150); System.gc(); threads.pause(150); pidFile().deleteOnExit(); pidFile().delete(); pidFile = null; pid = null;
@Override public synchronized void start(String threadName, Map mysqldArgs, boolean populateAllOptions) { if ((getShell() != null) || processRunning()) { printMessage("mysqld already running (process: " + pid() + ")"); return; options = optionParser.getOptionsFromHelp(getHelp(mysqldArgs)); } else { options = new HashMap(); addCompletionListenser(new Runnable() { @Override public void run() { setShell(exec(threadName, mysqldArgs, out, err, true)); reportPid(); utils.files().writeString(portFile, portStr); boolean ready = canConnectToServer(port, killDelay); setReadyForConnection(ready);
String url = "jdbc:mysql://localhost:" + port + "/test"; MysqldResource mysqldResource = new MysqldResource(baseDir); mysqldResource.start("test-mysqld-thread", options); if (!mysqldResource.isRunning()) { String msg = "MySQL did not start."; throw new RuntimeException(msg); e.printStackTrace(); mysqldResource.shutdown();
public static void main(String[] args) { CommandLineOptionsParser clop = new CommandLineOptionsParser(args); MysqldResource mysqld = new MysqldResource(clop.getBaseDir()); if (args.length == 0) { mysqld.printUsage(); } else if (clop.isShutdown()) { mysqld.shutdown(); } else { mysqld.start(new Threads().newName(), clop.asMap()); } } }
public void testLaunch() throws Exception { Map args = new HashMap(); String port = "" + testUtil.testPort(); args.put(MysqldResourceI.PORT, port); String url = "jdbc:mysql://localhost:" + port + "/test"; String threadName = "testLaunch"; assertFalse("mysqld should not be running", mysqldResource.isRunning()); mysqldResource.start(threadName, args); Shell s1 = mysqldResource.getShell(); /* this asserts the thread starts */ assertRunning(); /* pause for mysqld to bind to port */ for (int i = 0; i < 100 && !mysqldResource.isReadyForConnections(); i++) { threads.pause(25); } testUtil.assertConnectViaJDBC(url, true); mysqldResource.start(threadName, args); Shell s2 = mysqldResource.getShell(); assertEquals(s1, s2); assertTrue(s1.isDaemon()); assertRunningThenShutdown(); }
public void testUseDatabase() throws Exception { String url = "jdbc:mysql://localhost:" + testUtil.testPort() + "/test"; String threadName = "testLaunch"; assertFalse("mysqld should not be running", mysqldResource.isRunning()); startMysql(threadName); Shell s1 = mysqldResource.getShell(); /* this asserts the thread starts */ assertRunning(); /* pause for mysqld to bind to port */ for (int i = 0; i < 100 && !mysqldResource.isReadyForConnections(); i++) { utils.threads().pause(25); } testUtil.assertConnectViaJDBC(url); if (!mysqldResource.isWindows()) { File sockFile = new File(mysqldResource.getDataDir(), "mysql.sock"); assertTrue(sockFile.exists()); } mysqldResource.start(threadName, new HashMap()); Shell s2 = mysqldResource.getShell(); assertEquals(s1, s2); assertTrue(s1.isDaemon()); assertRunningThenShutdown(); }
protected void tearDown() { threads.pause(50); File baseDir = mysqldResource.getBaseDir(); try { if (mysqldResource.isRunning()) { mysqldResource.shutdown(); } } catch (Exception e) { e.printStackTrace(); } fileUtil.deleteTree(baseDir); super.tearDown(); }
private synchronized void setVersion(boolean checkRunning, String mysqlVersionString) { if (checkRunning && isRunning()) { throw new IllegalStateException("Already running"); } if (mysqlVersionString == null || mysqlVersionString.equals("")) { versionString = System.getProperty(MYSQLD_VERSION, connectorMxjProperties.getProperty(MYSQLD_VERSION)) + ""; } else { versionString = mysqlVersionString; } versionString = versionString.trim(); }
protected void tearDown() { utils.threads().pause(50); try { if (mysqldResource.isRunning()) { mysqldResource.setKillDelay(testUtil.testKillDelay()); mysqldResource.shutdown(); } } catch (Exception e) { e.printStackTrace(); } fileUtil.deleteTree(baseDir); super.tearDown(); }
/** * Stop the database. */ public void stopDb() { mysqldResource.shutdown(); LOG.debug("MySQL stoppped succcessfully"); }
/** called from option parser as well */ synchronized Shell exec(String threadName, Map mysqldArgs, PrintStream outStream, PrintStream errStream, boolean withListeners) { mysqldArgs.put(MysqldResourceI.BASEDIR, baseDir.getPath()); mysqldArgs.put(MysqldResourceI.PID_FILE, pidFile().getPath()); socket(mysqldArgs); File dataDir = dataDir(mysqldArgs); makeMysqld(); ensureEssentialFilesExist(dataDir); String[] args = constructArgs(mysqldArgs); outStream.println(str.toString(args)); Shell launch = shellFactory.newShell(args, threadName, outStream, errStream); if (withListeners) { for (int i = 0; i < completionListensers.size(); i++) { Runnable listener = (Runnable) completionListensers.get(i); launch.addCompletionListener(listener); } } launch.setDaemon(true); printMessage("launching mysqld (" + threadName + ")"); launch.start(); return launch; }
/** * Directs output generated by MySQL to Standard Out and Standard Error. * This is the constructor included in the MBeanInfo. */ public SimpleMysqldDynamicMBean() { this(new MysqldResource()); }
public void testTestFinalize() throws Throwable { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream captured = new PrintStream(baos); mysqldResource = new MysqldResource(baseDir, dataDir, "", captured, captured, utils); mysqldResource.finalize(); captured.flush(); String output = new String(baos.toByteArray()); assertEquals("", output); PrintStream devNull = new NullPrintStream(); String[] none = new String[0]; mysqldResource.setShell(new Shell.Default(none, "bogus", devNull, devNull)); mysqldResource.finalize(); captured.flush(); output = new String(baos.toByteArray()); testUtil.assertContainsIgnoreCase(output, "MysqldResource.initTrace"); }