Java Code Examples for io.netty.util.ReferenceCountUtil#releaseLater()

The following examples show how to use io.netty.util.ReferenceCountUtil#releaseLater() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdyPingFrameLength() throws Exception {
    short type = 6;
    byte flags = 0;
    int length = 8; // invalid length
    int id = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(id);

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 2
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdySettingsFrameNumSettings() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 2;
    int length = 8 * numSettings + 4;
    byte idFlags = 0;
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(0); // invalid num_settings
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 3
Source File: KeyValueMessageTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRemoveDocumentWithCAS() throws Exception {
    String key = "remove-key-cas";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket());
    UpsertResponse upsertResponse = cluster().<UpsertResponse>send(upsert).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, upsertResponse.status());
    ReferenceCountUtil.releaseLater(upsertResponse.content());
    assertValidMetadata(upsertResponse.mutationToken());

    RemoveRequest remove = new RemoveRequest(key, 1233443, bucket());
    RemoveResponse response = cluster().<RemoveResponse>send(remove).toBlocking().single();
    assertEquals(ResponseStatus.EXISTS, response.status());
    ReferenceCountUtil.releaseLater(response.content());
    assertNull(response.mutationToken());

    remove = new RemoveRequest(key, upsertResponse.cas(), bucket());
    response = cluster().<RemoveResponse>send(remove).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertTrue(response.cas() != upsertResponse.cas());
    ReferenceCountUtil.releaseLater(response.content());
    assertValidMetadata(response.mutationToken());
    assertMetadataSequence(upsertResponse.mutationToken(), response.mutationToken());
}
 
Example 4
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testPipelinedSpdyDataFrames() throws Exception {
    int streamId1 = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int streamId2 = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    byte flags = 0;
    int length = 0;

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(2 * (SPDY_HEADER_SIZE + length)));
    encodeDataFrameHeader(buf, streamId1, flags, length);
    encodeDataFrameHeader(buf, streamId2, flags, length);

    delegate.readDataFrame(streamId1, false, Unpooled.EMPTY_BUFFER);
    delegate.readDataFrame(streamId2, false, Unpooled.EMPTY_BUFFER);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 5
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtraData() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(22));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}
 
Example 6
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnidirectionalSpdySynStreamFrame() throws Exception {
    short type = 1;
    byte flags = 0x02; // FLAG_UNIDIRECTIONAL
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);

    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, true);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 7
Source File: KeyValueErrorMapTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void verifyExponentialRetry() throws Exception {
    opFailRequest(Long.parseLong("7FF2", 16), -1);
    startRetryVerifyRequest();
    try {
        String key = "upsert-key";
        UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("", CharsetUtil.UTF_8), bucket());
        UpsertResponse response = cluster().<UpsertResponse>send(upsert).toBlocking().single();
        ReferenceCountUtil.releaseLater(response.content());
    } catch (Exception ex) {
        //ignore exception
    }
    checkRetryVerifyRequest(Long.parseLong("7FF2", 16), OP_UPSERT, 25);
    opFailRequest(Long.parseLong("7FF2", 16),  0);
}
 
Example 8
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdyHeadersFrameStreamId() throws Exception {
    short type = 8;
    byte flags = 0;
    int length = 4;
    int streamId = 0; // invalid stream identifier

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 9
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdyRstStreamFrameFlags() throws Exception {
    short type = 3;
    byte flags = (byte) 0xFF; // invalid flags
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int statusCode = RANDOM.nextInt() | 0x01;

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 10
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testProgressivelyDiscardUnknownEmptyFrame() throws Exception {
    short type = 5;
    byte flags = (byte) 0xFF;
    int segment = 4;
    int length = 2 * segment;

    ByteBuf header = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE));
    ByteBuf segment1 = Unpooled.buffer(segment);
    ByteBuf segment2 = Unpooled.buffer(segment);
    encodeControlFrameHeader(header, type, flags, length);
    segment1.writeInt(RANDOM.nextInt());
    segment2.writeInt(RANDOM.nextInt());

    replay(delegate);
    decoder.decode(header);
    decoder.decode(segment1);
    decoder.decode(segment2);
    verify(delegate);
    assertFalse(header.isReadable());
    assertFalse(segment1.isReadable());
    assertFalse(segment2.isReadable());
}
 
Example 11
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroNameLength() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(8));
    headerBlock.writeInt(1);
    headerBlock.writeInt(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}
 
Example 12
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeValueLength() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(-1);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}
 
Example 13
Source File: SubdocumentMessageTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetMutationTokenWithMutation() throws Exception {
    String subPath = "sub.value";
    ByteBuf fragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubDictUpsertRequest upsertRequest = new SubDictUpsertRequest(testSubKey, subPath, fragment, bucket());
    SimpleSubdocResponse upsertResponse = cluster().<SimpleSubdocResponse>send(upsertRequest).toBlocking().single();
    ReferenceCountUtil.releaseLater(upsertResponse.content());
    assertValidMetadata(upsertResponse.mutationToken());
}
 
Example 14
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpdySettingsPersistedValues() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 1;
    int length = 8 * numSettings + 4;
    byte idFlags = 0x02; // FLAG_SETTINGS_PERSISTED
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(numSettings);
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    delegate.readSettingsFrame(false);
    delegate.readSetting(id, value, false, true);
    expectLastCall().times(numSettings);
    delegate.readSettingsEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 15
Source File: SubdocumentMessageTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSubGetValueInExistingDocumentObject() throws Exception{
    String subValuePath = "sub.value";

    SubGetRequest valueRequest = new SubGetRequest(testSubKey, subValuePath, bucket());
    SimpleSubdocResponse valueResponse = cluster().<SimpleSubdocResponse>send(valueRequest).toBlocking().single();
    String raw = valueResponse.content().toString(CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(valueResponse.content());
    assertNotNull(raw);
    assertEquals("\"subStringValue\"", raw);
}
 
Example 16
Source File: SubdocumentMessageTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnPathNotFoundOnPushFirstOnNonExistingPath() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.PUSH_FIRST;
    String path = "sub.array2";

    ByteBuf fragment = Unpooled.copiedBuffer("\"arrayElement\"", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUBDOC_PATH_NOT_FOUND, response.status());
}
 
Example 17
Source File: SubdocumentMessageTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPushFirstInArray() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.PUSH_FIRST;
    String path = "sub.array";

    ByteBuf fragment = Unpooled.copiedBuffer("\"arrayElement\"", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertMutation(testSubKey, jsonContent.replace("[", "[\"arrayElement\", "));
}
 
Example 18
Source File: ViewHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingOnNullKeys() {
    String keys = null;
    String query = "stale=false&endKey=test";
    ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password");
    channel.writeOutbound(request);
    DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound();
    assertEquals(HttpMethod.GET, outbound.getMethod());
    assertFalse(outbound.getUri().contains("keys="));
    assertTrue(outbound.getUri().endsWith("?stale=false&endKey=test"));
    String content = outbound.content().toString(CharsetUtil.UTF_8);
    assertTrue(content.isEmpty());
    ReferenceCountUtil.releaseLater(outbound); //NO-OP since content is empty but still...
}
 
Example 19
Source File: SubdocumentMessageTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDeleteExistingWholeArrayPath() {
    String path = "sub.array";
    SubDeleteRequest request = new SubDeleteRequest(testSubKey, path, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());

    assertTrue(response.status().isSuccess());
    assertEquals(0, response.content().readableBytes());
    assertEquals(ResponseStatus.SUCCESS, response.status());

    //assert the mutation
    String expected = "{\"value\":\"stringValue\", \"sub\": {\"value\": \"subStringValue\"}}";
    assertMutation(testSubKey, expected);
}
 
Example 20
Source File: QueryHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param inbound
 * @param expectedSuccess
 * @param expectedStatus
 * @param expectedRequestId
 * @param expectedClientId
 * @param expectedFinalStatus
 * @param expectedSignature
 * @param assertRows
 * @param assertErrors
 * @param metricsToCheck null to expect no metrics
 */
protected void assertResponse(GenericQueryResponse inbound,
        boolean expectedSuccess, ResponseStatus expectedStatus,
        String expectedRequestId, String expectedClientId,
        String expectedFinalStatus, String expectedSignature,
        Action1<ByteBuf> assertRows,
        Action1<ByteBuf> assertErrors,
        Map<String, Object> metricsToCheck) {
    assertEquals(expectedSuccess, inbound.status().isSuccess());
    assertEquals(expectedStatus, inbound.status());
    assertEquals(expectedRequestId, inbound.requestId());
    assertEquals(expectedClientId, inbound.clientRequestId());
    assertNotNull(inbound.request());

    assertEquals(expectedFinalStatus, inbound.queryStatus().timeout(1, TimeUnit.SECONDS).toBlocking().single());

    inbound.rows().timeout(5, TimeUnit.SECONDS).toBlocking()
           .forEach(assertRows);

    List<ByteBuf> metricList = inbound.info().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single();
    if (metricsToCheck == null) {
        assertEquals(0, metricList.size());
    } else {
        assertEquals(1, metricList.size());
        String metricsJson = metricList.get(0).toString(CharsetUtil.UTF_8);
        metricList.get(0).release();
        try {
            Map<String, Object> metrics = DefaultObjectMapper.readValueAsMap(metricsJson);
            assertEquals(7, metrics.size());

            for (Map.Entry<String, Object> entry : metricsToCheck.entrySet()) {
                assertNotNull(metrics.get(entry.getKey()));
                assertEquals(entry.getKey(), entry.getValue(), metrics.get(entry.getKey()));
            }
        } catch (IOException e) {
            fail(e.toString());
        }
    }

    inbound.errors().timeout(1, TimeUnit.SECONDS).toBlocking()
           .forEach(assertErrors);

    List<ByteBuf> signatureList = inbound.signature().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single();
    if (expectedSignature != null) {
        assertEquals(1, signatureList.size());
        String signatureJson = signatureList.get(0).toString(CharsetUtil.UTF_8);
        assertNotNull(signatureJson);
        assertEquals(expectedSignature, signatureJson.replaceAll("\\s", ""));
        ReferenceCountUtil.releaseLater(signatureList.get(0));
    } else {
        assertEquals(0, signatureList.size());
    }
    assertEquals(0, inbound.profileInfo().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single().size());
}