示例#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);
 }
示例#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);
 }
示例#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);
 }
示例#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;
 }
示例#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()][]));
 }
示例#6
0
 protected String getStatusCodeReply() {
   flush();
   pipelinedCommands--;
   final byte[] resp = (byte[]) protocol.read(inputStream);
   if (null == resp) {
     return null;
   } else {
     return SafeEncoder.encode(resp);
   }
 }
示例#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.");
 }
示例#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;
 }
示例#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);
  }
示例#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());
  }
示例#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);
      }
    }
示例#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);
 }
示例#13
0
 protected Connection sendCommand(final Command cmd) {
   connect();
   protocol.sendCommand(outputStream, cmd, new byte[0][]);
   pipelinedCommands++;
   return this;
 }
示例#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);
 }
示例#15
0
 @SuppressWarnings("unchecked")
 public List<Object> getObjectMultiBulkReply() {
   flush();
   pipelinedCommands--;
   return (List<Object>) protocol.read(inputStream);
 }
示例#16
0
 public Long getIntegerReply() {
   flush();
   pipelinedCommands--;
   return (Long) protocol.read(inputStream);
 }
示例#17
0
 public byte[] getBinaryBulkReply() {
   flush();
   pipelinedCommands--;
   return (byte[]) protocol.read(inputStream);
 }
示例#18
0
 @SuppressWarnings("deprecation")
 public Double getDoubleReply() {
   flush();
   pipelinedCommands--;
   return Double.valueOf(new String((byte[]) Protocol.read(inputStream)));
 }
示例#19
0
 protected Connection sendCommand(final Command cmd, final byte[]... args) {
   connect();
   protocol.sendCommand(outputStream, cmd, args);
   pipelinedCommands++;
   return this;
 }
示例#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);
 }
示例#21
0
 public Object getOne() {
   waitForAsyncReadsToComplete();
   flush();
   pipelinedCommands--;
   return Protocol.read(inputStream);
 }
示例#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);
 }
示例#23
0
 public Object getOne() {
   flush();
   pipelinedCommands--;
   return protocol.read(inputStream);
 }