Beispiel #1
0
 @SuppressWarnings("unchecked")
 @Test
 public void nullMultiBulkReply() {
   InputStream is = new ByteArrayInputStream("*-1\r\n".getBytes());
   List<String> response = (List<String>) Protocol.read(new RedisInputStream(is));
   assertNull(response);
 }
Beispiel #2
0
 @Test
 public void fragmentedBulkReply() {
   FragmentedByteArrayInputStream fis =
       new FragmentedByteArrayInputStream("$30\r\n012345678901234567890123456789\r\n".getBytes());
   byte[] response = (byte[]) Protocol.read(new RedisInputStream(fis));
   assertArrayEquals(SafeEncoder.encode("012345678901234567890123456789"), response);
 }
Beispiel #3
0
 public void zinterstore(final byte[] dstkey, final byte[]... sets) {
   final byte[][] params = new byte[sets.length + 2][];
   params[0] = dstkey;
   params[1] = Protocol.toByteArray(sets.length);
   System.arraycopy(sets, 0, params, 2, sets.length);
   sendCommand(ZINTERSTORE, params);
 }
Beispiel #4
0
 public List<Object> getAll(int except) {
   List<Object> all = new ArrayList<Object>();
   flush();
   while (pipelinedCommands > except) {
     all.add(protocol.read(inputStream));
     pipelinedCommands--;
   }
   return all;
 }
Beispiel #5
0
 public void zinterstore(final byte[] dstkey, final ZParams params, final byte[]... sets) {
   final List<byte[]> args = new ArrayList<byte[]>();
   args.add(dstkey);
   args.add(Protocol.toByteArray(sets.length));
   for (final byte[] set : sets) {
     args.add(set);
   }
   args.addAll(params.getParams());
   sendCommand(ZINTERSTORE, args.toArray(new byte[args.size()][]));
 }
Beispiel #6
0
 protected String getStatusCodeReply() {
   flush();
   pipelinedCommands--;
   final byte[] resp = (byte[]) protocol.read(inputStream);
   if (null == resp) {
     return null;
   } else {
     return SafeEncoder.encode(resp);
   }
 }
Beispiel #7
0
 @Test
 public void busyReply() {
   final String busyMessage = "BUSY Redis is busy running a script.";
   final InputStream is = new ByteArrayInputStream(('-' + busyMessage + "\r\n").getBytes());
   try {
     Protocol.read(new RedisInputStream(is));
   } catch (final JedisBusyException e) {
     assertEquals(busyMessage, e.getMessage());
     return;
   }
   fail("Expected a JedisBusyException to be thrown.");
 }
Beispiel #8
0
 public List<Object> getAll(int except) {
   List<Object> all = new ArrayList<Object>();
   flush();
   while (pipelinedCommands > except) {
     try {
       all.add(Protocol.read(inputStream));
     } catch (JedisDataException e) {
       all.add(e);
     }
     pipelinedCommands--;
   }
   return all;
 }
Beispiel #9
0
  @SuppressWarnings("unchecked")
  @Test
  public void multiBulkReply() {
    InputStream is =
        new ByteArrayInputStream(
            "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes());
    List<byte[]> response = (List<byte[]>) Protocol.read(new RedisInputStream(is));
    List<byte[]> expected = new ArrayList<byte[]>();
    expected.add(SafeEncoder.encode("foo"));
    expected.add(SafeEncoder.encode("bar"));
    expected.add(SafeEncoder.encode("Hello"));
    expected.add(SafeEncoder.encode("World"));

    assertEquals(expected, response);
  }
Beispiel #10
0
  @Test
  public void buildACommand() throws IOException {
    PipedInputStream pis = new PipedInputStream();
    BufferedInputStream bis = new BufferedInputStream(pis);
    PipedOutputStream pos = new PipedOutputStream(pis);
    RedisOutputStream ros = new RedisOutputStream(pos);

    Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
    ros.flush();
    pos.close();
    String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";

    int b;
    StringBuilder sb = new StringBuilder();
    while ((b = bis.read()) != -1) {
      sb.append((char) b);
    }

    assertEquals(expectedCommand, sb.toString());
  }
Beispiel #11
0
    @Override
    public void run() {
      JedisDataException exception = null;
      try {
        while (isConnected()) {
          Response<?> response = asyncResponses.take();
          Object data = Protocol.read(inputStream);
          response.set(data);
          wakeNextWriter();
        }

      } catch (Exception ioe) {
        exception = new JedisDataException(ioe);
      }
      if (exception == null) {
        exception = new JedisDataException("Disconnected");
      }
      // if something went wrong
      Response<?> response;
      while ((response = asyncResponses.poll()) != null) {
        response.set(exception);
      }
    }
Beispiel #12
0
 @Test
 public void bulkReply() {
   InputStream is = new ByteArrayInputStream("$6\r\nfoobar\r\n".getBytes());
   byte[] response = (byte[]) Protocol.read(new RedisInputStream(is));
   assertArrayEquals(SafeEncoder.encode("foobar"), response);
 }
Beispiel #13
0
 protected Connection sendCommand(final Command cmd) {
   connect();
   protocol.sendCommand(outputStream, cmd, new byte[0][]);
   pipelinedCommands++;
   return this;
 }
Beispiel #14
0
 @Test
 public void singleLineReply() {
   InputStream is = new ByteArrayInputStream("+OK\r\n".getBytes());
   byte[] response = (byte[]) Protocol.read(new RedisInputStream(is));
   assertArrayEquals(SafeEncoder.encode("OK"), response);
 }
Beispiel #15
0
 @SuppressWarnings("unchecked")
 public List<Object> getObjectMultiBulkReply() {
   flush();
   pipelinedCommands--;
   return (List<Object>) protocol.read(inputStream);
 }
Beispiel #16
0
 public Long getIntegerReply() {
   flush();
   pipelinedCommands--;
   return (Long) protocol.read(inputStream);
 }
Beispiel #17
0
 public byte[] getBinaryBulkReply() {
   flush();
   pipelinedCommands--;
   return (byte[]) protocol.read(inputStream);
 }
Beispiel #18
0
 @SuppressWarnings("deprecation")
 public Double getDoubleReply() {
   flush();
   pipelinedCommands--;
   return Double.valueOf(new String((byte[]) Protocol.read(inputStream)));
 }
Beispiel #19
0
 protected Connection sendCommand(final Command cmd, final byte[]... args) {
   connect();
   protocol.sendCommand(outputStream, cmd, args);
   pipelinedCommands++;
   return this;
 }
Beispiel #20
0
 @Test
 public void nullBulkReply() {
   InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());
   String response = (String) Protocol.read(new RedisInputStream(is));
   assertEquals(null, response);
 }
Beispiel #21
0
 public Object getOne() {
   waitForAsyncReadsToComplete();
   flush();
   pipelinedCommands--;
   return Protocol.read(inputStream);
 }
Beispiel #22
0
 @Test
 public void integerReply() {
   InputStream is = new ByteArrayInputStream(":123\r\n".getBytes());
   long response = (Long) Protocol.read(new RedisInputStream(is));
   assertEquals(123, response);
 }
Beispiel #23
0
 public Object getOne() {
   flush();
   pipelinedCommands--;
   return protocol.read(inputStream);
 }