/**
  * Stops the server.
  *
  * @param port server port
  * @param eport event port
  * @throws IOException I/O exception
  */
 public static void stop(final int port, final int eport) throws IOException {
   final File stop = stopFile(port);
   try {
     stop.createNewFile();
     new Socket(LOCALHOST, eport).close();
     new Socket(LOCALHOST, port).close();
     // check if server was really stopped
     while (ping(LOCALHOST, port)) Performance.sleep(50);
     Performance.sleep(50);
   } catch (final IOException ex) {
     stop.delete();
     throw ex;
   }
 }
Beispiel #2
0
  /**
   * 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);
  }
Beispiel #3
0
 /** 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);
 }
Beispiel #4
0
 @Override
 public void run() {
   while (text != null) {
     flashing ^= true;
     panel.repaint();
     Performance.sleep(500);
   }
   flashing = false;
   panel.repaint();
 }
Beispiel #5
0
 /** 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));
   }
 }
 @Override
 public void run() {
   try {
     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);
       new XQuery(qu).execute(context);
     }
   } catch (final BaseXException ex) {
     ex.printStackTrace();
   }
 }
  /**
   * Starts the database server in a separate process.
   *
   * @param port server port
   * @param args command-line arguments
   * @throws BaseXException database exception
   */
  public static void start(final int port, final String... args) throws BaseXException {

    // check if server is already running (needs some time)
    if (ping(LOCALHOST, port)) throw new BaseXException(SRV_RUNNING);

    Util.start(BaseXServer.class, args);

    // try to connect to the new server instance
    for (int c = 0; c < 10; ++c) {
      if (ping(LOCALHOST, port)) return;
      Performance.sleep(100);
    }
    throw new BaseXException(CONNECTION_ERROR);
  }
Beispiel #8
0
  /**
   * 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;
    }
  }
Beispiel #9
0
    @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();
      }
    }
Beispiel #10
0
  /**
   * 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;
    }
  }
Beispiel #11
0
  /** Exits the session. */
  public synchronized void quit() {
    running = false;
    if (log != null) log.write(this, "LOGOUT " + context.user.name, OK);

    // wait until running command was stopped
    if (command != null) {
      command.stop();
      while (command != null) Performance.sleep(50);
    }
    context.delete(this);

    try {
      new Close().execute(context);
      socket.close();
      if (events) {
        esocket.close();
        // remove this session from all events in pool
        for (final Sessions s : context.events.values()) s.remove(this);
      }
    } catch (final Exception ex) {
      if (log != null) log.write(ex.getMessage());
      Util.stack(ex);
    }
  }
Beispiel #12
0
 /** Stop BaseX HTTP. */
 private void stopBaseXHTTP() {
   Util.start(BaseXHTTP.class, "stop");
   Performance.sleep(TIMEOUT); // give the server some time to stop
 }
Beispiel #13
0
 /** 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
 }
  /**
   * Constructor.
   *
   * @param ctx database context
   * @param args command-line arguments
   * @throws IOException I/O exception
   */
  public BaseXServer(final Context ctx, final String... args) throws IOException {

    super(args, ctx);
    final MainProp mprop = context.mprop;
    final int port = mprop.num(MainProp.SERVERPORT);
    final int eport = mprop.num(MainProp.EVENTPORT);
    // check if ports are distinct
    if (port == eport) throw new BaseXException(PORT_TWICE_X, port);

    final String host = mprop.get(MainProp.SERVERHOST);
    final InetAddress addr = host.isEmpty() ? null : InetAddress.getByName(host);

    if (service) {
      start(port, args);
      Util.outln(SRV_STARTED);
      Performance.sleep(1000);
      return;
    }

    if (stopped) {
      stop(port, eport);
      Util.outln(SRV_STOPPED);
      Performance.sleep(1000);
      return;
    }

    try {
      // execute command-line arguments
      for (final String c : commands) execute(c);

      log = new Log(context, quiet);
      log.write(SRV_STARTED);

      socket = new ServerSocket();
      socket.setReuseAddress(true);
      socket.bind(new InetSocketAddress(addr, port));
      esocket = new ServerSocket();
      esocket.setReuseAddress(true);
      esocket.bind(new InetSocketAddress(addr, eport));
      stop = stopFile(port);

      // show info when server is aborted
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {
                @Override
                public void run() {
                  log.write(SRV_STOPPED);
                  log.close();
                  Util.outln(SRV_STOPPED);
                }
              });

      new Thread(this).start();
      while (!running) Performance.sleep(100);

      Util.outln(CONSOLE + (console ? TRY_MORE_X : SRV_STARTED), SERVERMODE);

      if (console) {
        console();
        quit();
      }
    } catch (final IOException ex) {
      if (log != null) log.write(ex.getMessage());
      throw ex;
    }
  }