Skip to content

Commit

Permalink
Add flushEnabled option for couchbase buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerschtli committed Jun 23, 2021
1 parent 3379f62 commit 351d521
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@
public class BucketDefinition {

private final String name;
private boolean flushEnabled = false;
private boolean queryPrimaryIndex = true;
private int quota = 100;

public BucketDefinition(final String name) {
this.name = name;
}

/**
* Enables flush for this bucket (disabled by default).
*
* @param flushEnabled if true, the bucket can be flushed.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withFlushEnabled(final boolean flushEnabled) {
this.flushEnabled = flushEnabled;
return this;
}

/**
* Sets a custom bucket quota (100MB by default).
*
Expand Down Expand Up @@ -58,6 +70,10 @@ public String getName() {
return name;
}

public boolean hasFlushEnabled() {
return flushEnabled;
}

public boolean hasPrimaryIndex() {
return queryPrimaryIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private void createBuckets() {
@Cleanup Response response = doHttpRequest(MGMT_PORT, "/pools/default/buckets", "POST", new FormBody.Builder()
.add("name", bucket.getName())
.add("ramQuotaMB", Integer.toString(bucket.getQuota()))
.add("flushEnabled", bucket.hasFlushEnabled() ? "1" : "0")
.build(), true);

checkSuccessfulResponse(response, "Could not create bucket " + bucket.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import java.util.function.Consumer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class CouchbaseContainerTest {

Expand All @@ -41,17 +44,7 @@ public void testBasicContainerUsage() {
.withBucket(bucketDefinition)
// }
) {
container.start();

// cluster_creation {
Cluster cluster = Cluster.connect(
container.getConnectionString(),
container.getUsername(),
container.getPassword()
);
// }

try {
setUpClient(container, cluster -> {
Bucket bucket = cluster.bucket(bucketDefinition.getName());
Collection collection = bucket.defaultCollection();

Expand All @@ -60,10 +53,47 @@ public void testBasicContainerUsage() {
JsonObject fooObject = collection.get("foo").contentAsObject();

assertEquals("value", fooObject.getString("key"));
} finally {
cluster.disconnect();
}
});
}
}

@Test
public void testBucketIsFlushableIfEnabled() {
BucketDefinition bucketDefinition = new BucketDefinition("mybucket")
.withFlushEnabled(true);

try (
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE)
.withBucket(bucketDefinition)
) {
setUpClient(container, cluster -> {
Bucket bucket = cluster.bucket(bucketDefinition.getName());
Collection collection = bucket.defaultCollection();

collection.upsert("foo", JsonObject.create().put("key", "value"));

cluster.buckets().flushBucket(bucketDefinition.getName());

assertFalse(collection.exists("foo").exists());
});
}
}

private void setUpClient(CouchbaseContainer container, Consumer<Cluster> consumer) {
container.start();

// cluster_creation {
Cluster cluster = Cluster.connect(
container.getConnectionString(),
container.getUsername(),
container.getPassword()
);
// }

try {
consumer.accept(cluster);
} finally {
cluster.disconnect();
}
}
}

0 comments on commit 351d521

Please sign in to comment.