Search in sources :

Example 6 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project jnosql-diana-driver by eclipse.

the class DefaultCouchbaseBucketManagerFactory method getBucket.

private Bucket getBucket(String bucketName) {
    requireNonNull(bucketName, "bucket is required");
    CouchbaseCluster couchbaseCluster = CouchbaseClusterUtil.getCouchbaseCluster(bucketName, this.couchbaseCluster, user, password);
    return couchbaseCluster.openBucket(bucketName);
}
Also used : CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster)

Example 7 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project samza by apache.

the class TestCouchbaseBucketRegistry method testCloseBucket.

/**
 * This unit test simulates 10 tasks using the same bucket. Each task will call registry.getBucket once. Then
 * each task will also call registry.closeBucket once. After that, registry.closeBucket should return false if we
 * close the bucket one more time. And the bucket should have already been closed.
 */
@Test
public void testCloseBucket() {
    String bucketName = "bucket";
    List<String> clusterNodes = Arrays.asList("cluster");
    CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
    CouchbaseCluster cluster = mock(CouchbaseCluster.class);
    Bucket bucket = mock(Bucket.class);
    when(bucket.close()).thenReturn(true).thenReturn(false);
    when(cluster.openBucket(bucketName)).thenReturn(bucket);
    when(cluster.disconnect()).thenReturn(true).thenReturn(false);
    mockStatic(CouchbaseCluster.class);
    when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes))).thenReturn(cluster);
    CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
    int numOfThreads = 10;
    for (int i = 0; i < numOfThreads; i++) {
        registry.getBucket(bucketName, clusterNodes, configs);
    }
    for (int i = 0; i < numOfThreads; i++) {
        assertTrue(registry.closeBucket(bucketName, clusterNodes));
    }
    // Close one more time. Should return false.
    assertFalse(registry.closeBucket(bucketName, clusterNodes));
    // Bucket should has been closed
    assertFalse(bucket.close());
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project samza by apache.

the class TestCouchbaseBucketRegistry method testOpenSameBucketNameFromDifferentClusters.

/**
 * This unit test uses CouchbaseBucketRegistry to register two mocked buckets with same name but in different clusters.
 * Calling registry.getBucket with same bucketName but different clusterNodes should return different Bucket instances.
 */
@Test
public void testOpenSameBucketNameFromDifferentClusters() {
    String bucketName = "bucket";
    List<String> clusterNodes1 = Arrays.asList("cluster1");
    List<String> clusterNodes2 = Arrays.asList("cluster2");
    CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
    CouchbaseCluster cluster1 = mock(CouchbaseCluster.class);
    CouchbaseCluster cluster2 = mock(CouchbaseCluster.class);
    when(cluster1.openBucket(bucketName)).thenReturn(mock(Bucket.class));
    when(cluster2.openBucket(bucketName)).thenReturn(mock(Bucket.class));
    mockStatic(CouchbaseCluster.class);
    when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes1))).thenReturn(cluster1);
    when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes2))).thenReturn(cluster2);
    CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
    Bucket bucketInCluster1 = registry.getBucket(bucketName, clusterNodes1, configs);
    Bucket bucketInCluster2 = registry.getBucket(bucketName, clusterNodes2, configs);
    assertNotEquals(bucketInCluster1, bucketInCluster2);
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project samza by apache.

the class TestCouchbaseBucketRegistry method testOpenBuckets.

/**
 * This unit test uses CouchbaseBucketRegistry to register two mocked buckets. It tests:
 * 1. Calling registry.getBucket with same bucketName and clusterNodes should return same Bucket instance
 * 2. Calling registry.getBucket with different bucketNames should return different Bucket instances
 */
@Test
public void testOpenBuckets() {
    String bucketName1 = "bucket1";
    String bucketName2 = "bucket2";
    List<String> clusterNodes = Arrays.asList("cluster");
    CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
    CouchbaseCluster cluster = mock(CouchbaseCluster.class);
    when(cluster.openBucket(bucketName1)).thenReturn(mock(Bucket.class));
    when(cluster.openBucket(bucketName2)).thenReturn(mock(Bucket.class));
    mockStatic(CouchbaseCluster.class);
    when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), anyListOf(String.class))).thenReturn(cluster);
    CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
    Bucket bucket1 = registry.getBucket(bucketName1, clusterNodes, configs);
    Bucket bucket1Copy = registry.getBucket(bucketName1, clusterNodes, configs);
    Bucket bucket2 = registry.getBucket(bucketName2, clusterNodes, configs);
    assertEquals(bucket1, bucket1Copy);
    assertNotEquals(bucket1, bucket2);
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CouchbaseCluster (com.couchbase.client.java.CouchbaseCluster)9 Bucket (com.couchbase.client.java.Bucket)6 CouchbaseEnvironment (com.couchbase.client.java.env.CouchbaseEnvironment)6 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JsonDocument (com.couchbase.client.java.document.JsonDocument)2 DefaultCouchbaseEnvironment (com.couchbase.client.java.env.DefaultCouchbaseEnvironment)2 JsonObject (com.couchbase.client.java.document.json.JsonObject)1 IOException (java.io.IOException)1 Test (org.testng.annotations.Test)1