maintenance: add a simple snapshot integration test
package com.coreos.jetcd;
-import com.coreos.jetcd.api.SnapshotResponse;
+import com.coreos.jetcd.Maintenance.Snapshot;
import com.coreos.jetcd.api.StatusResponse;
import com.coreos.jetcd.exception.AuthFailedException;
import com.coreos.jetcd.exception.ConnectException;
import com.google.protobuf.ByteString;
-import java.util.concurrent.CountDownLatch;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
@@ -18,14 +21,14 @@ public class MaintenanceTest {
private final Client client;
private final Maintenance maintenance;
private final Assertion test = new Assertion();
- private volatile ByteString snapshotBlob;
- private CountDownLatch finishLatch = new CountDownLatch(1);
+ private volatile ByteString snapshotBlob = ByteString.copyFromUtf8("");
public MaintenanceTest() throws AuthFailedException, ConnectException {
this.client = ClientBuilder.newBuilder().endpoints(TestConstants.endpoints).build();
- maintenance = client.getMaintenanceClient();
+ this.maintenance = client.getMaintenanceClient();
}
+
/**
* test status member function
*/
@@ -35,6 +38,22 @@ public class MaintenanceTest {
test.assertTrue(statusResponse.getDbSize() > 0);
}
+ // TODO: find a better way to test snapshot.
+ @Test
+ public void Testsnapshot() throws IOException {
+ // create snapshot.db file current folder.
+ String dir = Paths.get("").toAbsolutePath().toString();
+ File snapfile = new File(dir, "snapshot.db");
+
+ // leverage try-with-resources
+ try (Snapshot snapshot = maintenance.snapshot();
+ FileOutputStream fop = new FileOutputStream(snapfile)) {
+ snapshot.write(fop);
+ } catch (Exception e) {
+ snapfile.delete();
+ }
+ }
+
/**
* test alarm list function
* TODO trigger alarm, valid whether listAlarms will work.
@@ -45,36 +64,6 @@ public class MaintenanceTest {
maintenance.listAlarms().get();
}
- /**
- * test setSnapshotCallback function
- * TODO trigger snapshot, valid whether setSnapshotCallback will work.
- * TODO test removeSnapShotCallback
- */
- @Test
- void testAddSnapshotCallback() {
- maintenance.setSnapshotCallback(new Maintenance.SnapshotCallback() {
- @Override
- public synchronized void onSnapShot(SnapshotResponse snapshotResponse) {
- // blob contains the next chunk of the snapshot in the snapshot stream, blob is the bytes snapshot.
- // remaining_bytes is the number of blob bytes to be sent after this message
- if (snapshotBlob == null) {
- snapshotBlob = snapshotResponse.getBlob();
- } else {
- snapshotBlob = snapshotBlob.concat(snapshotResponse.getBlob());
- }
- if (snapshotResponse.getRemainingBytes() == 0) {
- // TODO finishLatch will be replaced by ListenableFuture instance
- finishLatch.countDown();
- }
- }
-
- @Override
- public void onError(Throwable throwable) {
-
- }
- });
- }
-
/**
* test defragmentMember function
*/
