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.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldRemoveDocumentWithoutCAS() {
    String key = "remove-key";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<UpsertResponse>send(upsert).toBlockingObservable().single().status());

    RemoveRequest remove = new RemoveRequest(key, bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<RemoveResponse>send(remove).toBlockingObservable().single().status());
    GetRequest get = new GetRequest(key, bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<GetResponse>send(get).toBlockingObservable().single().status());
}

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

License:Open Source License

@Test
public void shouldRemoveDocumentWithCAS() {
    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).toBlockingObservable().single();
    assertEquals(ResponseStatus.SUCCESS, upsertResponse.status());

    RemoveRequest remove = new RemoveRequest(key, 1233443, bucket);
    assertEquals(ResponseStatus.EXISTS,/*  w w  w.  j  av  a2s  .c o  m*/
            cluster.<RemoveResponse>send(remove).toBlockingObservable().single().status());
    remove = new RemoveRequest(key, upsertResponse.cas(), bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<RemoveResponse>send(remove).toBlockingObservable().single().status());
}

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

License:Apache License

private void upsertKey(String key, String value) {
    UpsertResponse resp = cluster()/*from w  ww .  ja  va  2  s.  co m*/
            .<UpsertResponse>send(
                    new UpsertRequest(key, Unpooled.copiedBuffer(value, CharsetUtil.UTF_8), 1, 0, bucket()))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, resp.status());
    ReferenceCountUtil.releaseLater(resp.content());
}

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

License:Open Source License

@Test
public void shouldRequestStream() throws Exception {
    List<OpenConnectionResponse> open = cluster()
            .<OpenConnectionResponse>send(new OpenConnectionRequest("hello", bucket())).toList().toBlocking()
            .single();//from www . j a  v a2s.c o m
    assertEquals(1, open.size());
    for (OpenConnectionResponse response : open) {
        assertEquals(ResponseStatus.SUCCESS, response.status());
    }

    StreamRequestResponse addStream = cluster()
            .<StreamRequestResponse>send(new StreamRequestRequest(calculateVBucketForKey("foo"), bucket()))
            .toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, addStream.status());

    TestSubscriber<DCPRequest> subscriber = new TestSubscriber<DCPRequest>();
    open.get(0).connection().subject().takeUntil(Observable.timer(2, TimeUnit.SECONDS))
            .subscribe((Subscriber) subscriber);

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

    subscriber.awaitTerminalEvent();
    List<DCPRequest> items = subscriber.getOnNextEvents();

    boolean seenMutation = false;
    boolean seenSnapshot = false;
    for (DCPRequest found : items) {
        if (found instanceof SnapshotMarkerMessage) {
            seenSnapshot = true;
        } else if (found instanceof MutationMessage) {
            seenMutation = true;
            assertEquals("foo", ((MutationMessage) found).key());
            ReferenceCountUtil.releaseLater(((MutationMessage) found).content());
        }
    }

    assertTrue(seenMutation);
    assertTrue(seenSnapshot);
}

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

License:Apache License

@Test
public void shouldFlush() {
    List<String> keys = Arrays.asList("key1", "key2", "key3");

    Observable.from(keys).flatMap(new Func1<String, Observable<UpsertResponse>>() {
        @Override/*from   www.  j av  a2  s.co m*/
        public Observable<UpsertResponse> call(String key) {
            return cluster().send(
                    new UpsertRequest(key, Unpooled.copiedBuffer("Content", CharsetUtil.UTF_8), bucket()));
        }
    }).doOnNext(new Action1<UpsertResponse>() {
        @Override
        public void call(UpsertResponse upsertResponse) {
            ReferenceCountUtil.releaseLater(upsertResponse.content());
        }
    }).toBlocking().last();

    FlushResponse response = cluster().<FlushResponse>send(new FlushRequest(bucket(), password())).toBlocking()
            .single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    ReferenceCountUtil.releaseLater(response.content());

    List<GetResponse> responses = Observable.from(keys).flatMap(new Func1<String, Observable<GetResponse>>() {
        @Override
        public Observable<GetResponse> call(String key) {
            return cluster().send(new GetRequest(key, bucket()));
        }
    }).toList().toBlocking().single();

    assertEquals(keys.size(), responses.size());
    for (GetResponse get : responses) {
        assertEquals(ResponseStatus.NOT_EXISTS, get.status());
        ReferenceCountUtil.releaseLater(get.content());
    }
}

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

License:Apache License

@Test
public void shouldUpsertAndGetDocument() throws Exception {
    String key = "upsert-key";
    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());
    assertValidMetadata(response.mutationToken());

    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.KeyValueMessageTest.java

License:Apache License

@Test
public void shouldUpsertWithExpiration() throws Exception {
    String key = "upsert-key-vanish";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), 1, 0,
            bucket());/*w w w. j ava  2 s .c o  m*/
    UpsertResponse response = cluster().<UpsertResponse>send(upsert).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    assertValidMetadata(response.mutationToken());

    Thread.sleep(2000);

    GetRequest request = new GetRequest(key, bucket());
    GetResponse getResponse = cluster().<GetResponse>send(request).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 shouldHandleDoubleInsert() throws Exception {
    String key = "insert-key";
    String content = "Hello World!";
    InsertRequest insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket());
    InsertResponse insertResponse = cluster().<InsertResponse>send(insert).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, insertResponse.status());
    ReferenceCountUtil.releaseLater(insertResponse.content());
    assertValidMetadata(insertResponse.mutationToken());

    insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket());
    insertResponse = cluster().<InsertResponse>send(insert).toBlocking().single();
    assertEquals(ResponseStatus.EXISTS, insertResponse.status());
    ReferenceCountUtil.releaseLater(insertResponse.content());
    assertNull(insertResponse.mutationToken());
}

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

License:Apache License

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

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8),
            bucket());// ww  w .jav  a2  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), bucket()));
                }
            }).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    assertValidMetadata(response.mutationToken());

    assertEquals(ResponseStatus.SUCCESS, response.status());
}

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

License:Apache License

@Test
public void shouldReplaceWithFailingCAS() {
    final String key = "replace-key-cas-fail";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8),
            bucket());/*from   w w  w.ja  v  a2s . 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), 24234234L, bucket()));
                }
            }).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    assertEquals(ResponseStatus.EXISTS, response.status());
    assertNull(response.mutationToken());
}