Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copiedBuffer.

Prototype

private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) 

Source Link

Usage

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldReplaceWithMatchingCAS() throws Exception {
    final String key = "replace-key-cas-match";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8),
            bucket());/* w  w w. j  ava2 s .c o m*/
    ReplaceResponse response = cluster().<ReplaceResponse>send(insert).toBlocking().single();
    assertEquals(ResponseStatus.NOT_EXISTS, response.status());
    ReferenceCountUtil.releaseLater(response.content());
    assertNull(response.mutationToken());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket());
    response = cluster().<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    ReferenceCountUtil.releaseLater(response.content());
                    return cluster().send(new ReplaceRequest(key,
                            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), response.cas(), bucket()));
                }
            }).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertValidMetadata(response.mutationToken());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldRemoveDocumentWithoutCAS() throws Exception {
    String key = "remove-key";
    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, bucket());
    RemoveResponse response = cluster().<RemoveResponse>send(remove).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertTrue(response.cas() != 0);/*  w w  w .j av  a2 s . c  o m*/
    ReferenceCountUtil.releaseLater(response.content());
    assertValidMetadata(response.mutationToken());
    assertMetadataSequence(upsertResponse.mutationToken(), response.mutationToken());

    GetRequest get = new GetRequest(key, bucket());
    GetResponse getResponse = cluster().<GetResponse>send(get).toBlocking().single();
    assertEquals(ResponseStatus.NOT_EXISTS, getResponse.status());
    ReferenceCountUtil.releaseLater(getResponse.content());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@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() != 0);/*from w ww. j  a  v  a2  s  . c o m*/
    ReferenceCountUtil.releaseLater(response.content());
    assertValidMetadata(response.mutationToken());
    assertMetadataSequence(upsertResponse.mutationToken(), response.mutationToken());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldGetAndTouch() throws Exception {
    String key = "get-and-touch";

    UpsertRequest request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), 3, 0,
            bucket());/*from  w  w  w  .j  a  va 2  s. co  m*/
    UpsertResponse response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    Thread.sleep(2000);

    GetResponse getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), false, true, 3))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("content", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());

    Thread.sleep(2000);

    getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), false, true, 3)).toBlocking()
            .single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("content", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldGetAndLock() throws Exception {
    String key = "get-and-lock";

    UpsertRequest request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8),
            bucket());/*from  w ww . j  a  va 2s .c o  m*/
    UpsertResponse response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    GetResponse getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), true, false, 2))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("content", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());

    request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), bucket());
    response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.EXISTS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    GetResponse secondLockResponse = (GetResponse) cluster().send(new GetRequest(key, bucket(), true, false, 2))
            .toBlocking().single();
    assertEquals(ResponseStatus.TEMPORARY_FAILURE, secondLockResponse.status());
    ReferenceCountUtil.releaseLater(secondLockResponse.content());

    Thread.sleep(3000);

    request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), bucket());
    response = cluster().<UpsertResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    assertEquals(ResponseStatus.SUCCESS, response.status());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

/**
 * Verificiation for MB-15727./*from   w  w  w.ja va2s. c  om*/
 *
 * This test is ignored in versions lower than 4.5 since thats the version where it has been fixed.
 */
@Test
public void shouldGetAndLockWithAppend() throws Exception {
    assumeMinimumVersionCompatible(4, 5);

    String key = "get-and-lock-append";

    UpsertRequest request = new UpsertRequest(key, Unpooled.copiedBuffer("foo", CharsetUtil.UTF_8), bucket());
    UpsertResponse response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    GetResponse getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), true, false, 2))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("foo", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());

    AppendResponse appendResponse = cluster().<AppendResponse>send(new AppendRequest(key, getResponse.cas(),
            Unpooled.copiedBuffer("bar", CharsetUtil.UTF_8), bucket())).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertTrue(getResponse.cas() != appendResponse.cas());
    ReferenceCountUtil.releaseLater(appendResponse.content());

    getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), false, false, 0)).toBlocking()
            .single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("foobar", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldTouch() throws Exception {
    String key = "touch";

    UpsertRequest request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), 3, 0,
            bucket());//  w w  w .j  a v  a 2 s  . co  m
    UpsertResponse response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    Thread.sleep(2000);

    TouchResponse touchResponse = cluster().<TouchResponse>send(new TouchRequest(key, 3, bucket())).toBlocking()
            .single();
    assertEquals(ResponseStatus.SUCCESS, touchResponse.status());
    ReferenceCountUtil.releaseLater(touchResponse.content());

    Thread.sleep(2000);

    GetResponse getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket())).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("content", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldUnlock() throws Exception {
    String key = "unlock";

    UpsertRequest request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8),
            bucket());/*  w w  w  .  j av a2  s.  c om*/
    UpsertResponse response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    GetResponse getResponse = cluster().<GetResponse>send(new GetRequest(key, bucket(), true, false, 15))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, getResponse.status());
    assertEquals("content", getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());

    request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), bucket());
    response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.EXISTS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    UnlockRequest unlockRequest = new UnlockRequest(key, getResponse.cas(), bucket());
    UnlockResponse unlockResponse = cluster().<UnlockResponse>send(unlockRequest).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, unlockResponse.status());
    ReferenceCountUtil.releaseLater(unlockResponse.content());

    request = new UpsertRequest(key, Unpooled.copiedBuffer("content", CharsetUtil.UTF_8), bucket());
    response = cluster().<UpsertResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());
}

From source file:com.couchbase.client.core.cluster.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldHandleSpecialKeyChars() {
    String key = "AVERY READY INDEX";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket());
    UpsertResponse response = cluster().<UpsertResponse>send(upsert).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());

    GetRequest request = new GetRequest(key, bucket());
    GetResponse getResponse = cluster().<GetResponse>send(request).toBlocking().single();
    assertEquals(content, getResponse.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(getResponse.content());
}

From source file:com.couchbase.client.core.cluster.NetworkMetricsTest.java

License:Apache License

@Test
public void shouldCapturePerformedOperations() throws Exception {
    Observable<CouchbaseEvent> eventBus = env().eventBus().get();
    TestSubscriber<CouchbaseEvent> eventSubscriber = new TestSubscriber<CouchbaseEvent>();
    eventBus.filter(new Func1<CouchbaseEvent, Boolean>() {
        @Override//from   w  w w  . jav a2 s.c  o  m
        public Boolean call(CouchbaseEvent event) {
            return event instanceof NetworkLatencyMetricsEvent;
        }
    }).subscribe(eventSubscriber);

    assertTrue(eventSubscriber.getOnErrorEvents().isEmpty());
    assertTrue(eventSubscriber.getOnNextEvents().isEmpty());

    InsertResponse insertResponse = cluster()
            .<InsertResponse>send(
                    new InsertRequest("perfIns", Unpooled.copiedBuffer("ins", CharsetUtil.UTF_8), bucket()))
            .toBlocking().single();
    ReferenceCountUtil.release(insertResponse);

    UpsertResponse upsertResponse = cluster()
            .<UpsertResponse>send(
                    new UpsertRequest("perfUps", Unpooled.copiedBuffer("ups", CharsetUtil.UTF_8), bucket()))
            .toBlocking().single();
    ReferenceCountUtil.release(upsertResponse);

    for (int i = 0; i < 5; i++) {
        GetResponse getResponse = cluster().<GetResponse>send(new GetRequest("perfIns", bucket())).toBlocking()
                .single();
        ReferenceCountUtil.release(getResponse);

    }

    env().networkLatencyMetricsCollector().triggerEmit();

    Thread.sleep(100);

    List<CouchbaseEvent> events = eventSubscriber.getOnNextEvents();
    assertEquals(1, events.size());
    NetworkLatencyMetricsEvent event = (NetworkLatencyMetricsEvent) events.get(0);

    boolean hasInsert = false;
    boolean hasUpsert = false;
    boolean hasGet = false;
    for (Map.Entry<NetworkLatencyMetricsIdentifier, LatencyMetric> metric : event.latencies().entrySet()) {
        assertFalse(metric.getKey().host().contains(":"));

        if (metric.getKey().request().equals("InsertRequest")) {
            hasInsert = true;
            assertTrue(metric.getValue().count() >= 1);
        } else if (metric.getKey().request().equals("UpsertRequest")) {
            hasUpsert = true;
            assertTrue(metric.getValue().count() >= 1);
        } else if (metric.getKey().request().equals("GetRequest")) {
            hasGet = true;
            assertTrue(metric.getValue().count() >= 1);
        }
        assertTrue(metric.getValue().max() > 0);
        assertTrue(metric.getValue().min() > 0);
        assertTrue(metric.getValue().count() > 0);

        long lastPercentile = 0;
        for (Map.Entry<Double, Long> percentile : metric.getValue().percentiles().entrySet()) {
            assertTrue(percentile.getValue() >= lastPercentile);
            lastPercentile = percentile.getValue();
        }
    }

    assertTrue(hasInsert);
    assertTrue(hasUpsert);
    assertTrue(hasGet);
}