/** * Test concurrent reader and writer (GH-458). * * <p><b>Test case:</b> * * <ol> * <li/>start a long running reader; * <li/>try to start a writer: it should time out; * <li/>stop the reader; * <li/>start the writer again: it should succeed. * </ol> * * @throws Exception error during request execution */ @Test @Ignore("There is no way to stop a query on the server!") public void testReaderWriter() throws Exception { final String readerQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d"; final String writerQuery = "/test.xml"; final byte[] content = Token.token("<a/>"); final Get readerAction = new Get(readerQuery); final Put writerAction = new Put(writerQuery, content); final ExecutorService exec = Executors.newFixedThreadPool(2); // start reader exec.submit(readerAction); Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started // start writer Future<HTTPResponse> writer = exec.submit(writerAction); try { final HTTPResponse result = writer.get(TIMEOUT, TimeUnit.MILLISECONDS); if (result.status.isSuccess()) fail("Database modified while a reader is running"); throw new Exception(result.toString()); } catch (final TimeoutException e) { // writer is blocked by the reader: stop it writerAction.stop = true; } // stop reader readerAction.stop = true; // start the writer again writer = exec.submit(writerAction); assertEquals(HTTPCode.CREATED, writer.get().status); }
/** * Writes a log message. * * @param str strings to be written * @param time add performance info */ public void log(final boolean time, final Object... str) { final Object[] obj = new Object[str.length + (time ? 2 : 1)]; obj[0] = remote(); System.arraycopy(str, 0, obj, 1, str.length); if (time) obj[obj.length - 1] = perf.toString(); context.log.write(obj); }
/** Drops users. */ @Test public void dropUsers() { no(new DropUser(NAME), testSession); no(new DropUser(NAME), adminSession); ok(new Exit(), testSession); // give the server some time to close the client session Performance.sleep(50); ok(new DropUser(NAME), adminSession); }
/** Clean up method. */ @After public void cleanUp() { try { testSession.close(); adminSession.execute(new DropDB(RENAMED)); adminSession.execute(new DropDB(NAME)); adminSession.close(); // give the server some time to clean up the sessions before next test Performance.sleep(100); } catch (final Exception ex) { fail(Util.message(ex)); } }
/** * Authenticate the user and returns a new client {@link Context} instance. * * @return client context * @throws LoginException login exception */ public Context authenticate() throws LoginException { final byte[] address = token(req.getRemoteAddr()); try { if (user == null || user.isEmpty() || pass == null || pass.isEmpty()) throw new LoginException(NOPASSWD); final Context ctx = new Context(context(), null); ctx.user = ctx.users.get(user); if (ctx.user == null || !ctx.user.password.equals(md5(pass))) throw new LoginException(); context.blocker.remove(address); return ctx; } catch (final LoginException ex) { // delay users with wrong passwords for (int d = context.blocker.delay(address); d > 0; d--) Performance.sleep(100); throw ex; } }
@Override public void run() { try { // Perform some queries for (int i = 0; i < runs; ++i) { Performance.sleep((long) (50 * RND.nextDouble())); // Return nth text of the database final int n = RND.nextInt() % MAX + 1; final String qu = Util.info(QUERY, n); session.execute("xquery " + qu); } session.close(); } catch (final Exception ex) { ex.printStackTrace(); } }
/** * Test 2 concurrent readers (GH-458). * * <p><b>Test case:</b> * * <ol> * <li/>start a long running reader; * <li/>start a fast reader: it should succeed. * </ol> * * @throws Exception error during request execution */ @Test public void testMultipleReaders() throws Exception { final String number = "63177"; final String slowQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d"; final String fastQuery = "?query=" + number; final Get slowAction = new Get(slowQuery); final Get fastAction = new Get(fastQuery); final ExecutorService exec = Executors.newFixedThreadPool(2); exec.submit(slowAction); Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started final Future<HTTPResponse> fast = exec.submit(fastAction); try { final HTTPResponse result = fast.get(TIMEOUT, TimeUnit.MILLISECONDS); assertEquals(HTTPCode.OK, result.status); assertEquals(number, result.data); } finally { slowAction.stop = true; } }
/** Stop BaseX HTTP. */ private void stopBaseXHTTP() { Util.start(BaseXHTTP.class, "stop"); Performance.sleep(TIMEOUT); // give the server some time to stop }
/** Start BaseX HTTP. */ private void startBaseXHTTP() { Util.start(BaseXHTTP.class, "-U" + UserText.ADMIN, "-P" + UserText.ADMIN); Performance.sleep(TIMEOUT); // give the server some time to stop }