@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); }
@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); }
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); }
public List<Object> getAll(int except) { List<Object> all = new ArrayList<Object>(); flush(); while (pipelinedCommands > except) { all.add(protocol.read(inputStream)); pipelinedCommands--; } return all; }
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()][])); }
protected String getStatusCodeReply() { flush(); pipelinedCommands--; final byte[] resp = (byte[]) protocol.read(inputStream); if (null == resp) { return null; } else { return SafeEncoder.encode(resp); } }
@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."); }
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; }
@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); }
@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()); }
@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); } }
@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); }
protected Connection sendCommand(final Command cmd) { connect(); protocol.sendCommand(outputStream, cmd, new byte[0][]); pipelinedCommands++; return this; }
@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); }
@SuppressWarnings("unchecked") public List<Object> getObjectMultiBulkReply() { flush(); pipelinedCommands--; return (List<Object>) protocol.read(inputStream); }
public Long getIntegerReply() { flush(); pipelinedCommands--; return (Long) protocol.read(inputStream); }
public byte[] getBinaryBulkReply() { flush(); pipelinedCommands--; return (byte[]) protocol.read(inputStream); }
@SuppressWarnings("deprecation") public Double getDoubleReply() { flush(); pipelinedCommands--; return Double.valueOf(new String((byte[]) Protocol.read(inputStream))); }
protected Connection sendCommand(final Command cmd, final byte[]... args) { connect(); protocol.sendCommand(outputStream, cmd, args); pipelinedCommands++; return this; }
@Test public void nullBulkReply() { InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes()); String response = (String) Protocol.read(new RedisInputStream(is)); assertEquals(null, response); }
public Object getOne() { waitForAsyncReadsToComplete(); flush(); pipelinedCommands--; return Protocol.read(inputStream); }
@Test public void integerReply() { InputStream is = new ByteArrayInputStream(":123\r\n".getBytes()); long response = (Long) Protocol.read(new RedisInputStream(is)); assertEquals(123, response); }
public Object getOne() { flush(); pipelinedCommands--; return protocol.read(inputStream); }