public void testClose() {
    Client client = new ClientImpl(TEST_HOST, TEST_PORT);
    String s = client.listTubeUsed();
    assertNotNull(s);

    client.close();
    try {
      client.listTubeUsed();
      fail("didn't throw expected exception");
    } catch (BeanstalkException be) {
      String message = be.getMessage();
      assertEquals("Socket is closed", message);
    } catch (Exception e) {
      fail("caught exception: " + e.getMessage());
    }

    // close again
    client.close();
    try {
      client.listTubeUsed();
      fail("didn't throw expected exception");
    } catch (BeanstalkException be) {
      String message = be.getMessage();
      assertEquals("Socket is closed", message);
    } catch (Exception e) {
      fail("caught exception: " + e.getMessage());
    }
  }
  public void testUseTube() {
    Client client = new ClientImpl(TEST_HOST, TEST_PORT);
    client.useTube("foobar");

    // hashes are not valid in tube names
    try {
      client.useTube("foobar#");
      fail("no BAD_FORMAT thrown");
    } catch (BeanstalkException be) {
      assertEquals("BAD_FORMAT", be.getMessage());
    } catch (Exception e) {
      fail(e.getMessage());
    }

    // underscores are valid in tube names >= beanstalk 1.4.4.
    if (serverSupportsUnderscoreInTubeName(client)) {
      try {
        client.useTube("foobar_");
      } catch (Exception e) {
        fail(e.getMessage());
      }
    }

    // per pashields http://github.com/pashields/JavaBeanstalkClient.git
    // Names cannot start with hyphen
    try {
      client.useTube("-foobar");
      fail("no BAD_FORMAT thrown");
    } catch (BeanstalkException be) {
      assertEquals("BAD_FORMAT", be.getMessage());
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
  public void testTouch() {

    Client client = new ClientImpl(TEST_HOST, TEST_PORT);

    Object[] tubeNames = pushWatchedTubes(client);

    String srcString = "testTouch";
    int timeoutSeconds = 2;

    // nothing to touch
    boolean ok = false;
    ok = client.touch(0);
    assertFalse(ok);

    // producer
    client.useTube((String) tubeNames[1]);
    long jobId = client.put(65536, 0, timeoutSeconds, srcString.getBytes());
    assertTrue(jobId > 0);

    // we haven't reserved, so we can't touch
    ok = client.touch(jobId);
    assertFalse(ok);

    // reserve the job
    Job job = client.reserve(null);
    assertNotNull(job);

    // try to reserve another job
    try {
      job = client.reserve(2 * timeoutSeconds);
      fail("expected DEADLINE_SOON");
    } catch (BeanstalkException be) {
      String message = be.getMessage();
      assertEquals("DEADLINE_SOON", message);
      ok = client.touch(jobId);
      assertTrue(ok);
    } catch (Exception e) {
      fail("caught exception: " + e.getMessage());
    }

    client.delete(jobId);

    popWatchedTubes(client, tubeNames);
  }
  public void testNullArgs() {
    Client client = new ClientImpl(TEST_HOST, TEST_PORT);

    try {
      client.ignore(null);
      fail("didn't throw");
    } catch (BeanstalkException be) {
      assertEquals("null tubeName", be.getMessage());
    } catch (Exception e) {
      fail(
          "caught unexpected exception: "
              + e.getClass().getCanonicalName()
              + ", "
              + e.getMessage());
    }

    try {
      client.useTube(null);
      fail("didn't throw");
    } catch (BeanstalkException be) {
      assertEquals("null tubeName", be.getMessage());
    } catch (Exception e) {
      fail(
          "caught unexpected exception: "
              + e.getClass().getCanonicalName()
              + ", "
              + e.getMessage());
    }

    try {
      client.watch(null);
      fail("didn't throw");
    } catch (BeanstalkException be) {
      assertEquals("null tubeName", be.getMessage());
    } catch (Exception e) {
      fail(
          "caught unexpected exception: "
              + e.getClass().getCanonicalName()
              + ", "
              + e.getMessage());
    }
  }
  public void testPut() {

    Client client = new ClientImpl(TEST_HOST, TEST_PORT);
    long jobId = client.put(65536, 0, 120, "testPut".getBytes());
    assertTrue(jobId > 0);
    client.delete(jobId);

    // invalid priority
    try {
      jobId = client.put(-1, 0, 120, "testPutNegativePriority".getBytes());
      client.delete(jobId);
      fail("no BAD_FORMAT thrown");
    } catch (BeanstalkException be) {
      assertEquals("BAD_FORMAT", be.getMessage());
    } catch (Exception e) {
      fail(e.getMessage());
    }

    // invalid priority
    try {
      jobId = client.put(Long.MAX_VALUE, 0, 120, "testPutHugePriority".getBytes());
      client.delete(jobId);
      fail("no UNKNOWN_COMMAND thrown");
    } catch (BeanstalkException be) {
      assertEquals("invalid priority", be.getMessage());
    } catch (Exception e) {
      fail(e.getMessage());
    }

    // null data
    try {
      jobId = client.put(65536, 0, 120, null);
      client.delete(jobId);
      fail("no exception");
    } catch (BeanstalkException be) {
      assertEquals("null data", be.getMessage());
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }