Search in sources :

Example 1 with Snapshot

use of com.coreos.jetcd.Maintenance.Snapshot in project jetcd by coreos.

the class MaintenanceTest method Testsnapshot.

// 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();
    }
}
Also used : Snapshot(com.coreos.jetcd.Maintenance.Snapshot) FileOutputStream(java.io.FileOutputStream) ByteString(com.google.protobuf.ByteString) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) AuthFailedException(com.coreos.jetcd.exception.AuthFailedException) ConnectException(com.coreos.jetcd.exception.ConnectException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 2 with Snapshot

use of com.coreos.jetcd.Maintenance.Snapshot in project jetcd by coreos.

the class MaintenanceUnitTest method testWrite.

@Test(timeOut = 1000)
void testWrite() throws IOException {
    Snapshot snapshot = maintenanceCli.snapshot();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteString blob = ByteString.copyFromUtf8("blob");
    responseObserverRef.get().onNext(SnapshotResponse.newBuilder().setBlob(blob).setRemainingBytes(0).build());
    responseObserverRef.get().onCompleted();
    snapshot.write(out);
    assertThat(out.toByteArray()).isEqualTo(blob.toByteArray());
}
Also used : Snapshot(com.coreos.jetcd.Maintenance.Snapshot) ByteString(com.google.protobuf.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 3 with Snapshot

use of com.coreos.jetcd.Maintenance.Snapshot in project jetcd by coreos.

the class MaintenanceUnitTest method testWriteTwice.

@Test(timeOut = 1000)
public void testWriteTwice() throws IOException {
    Snapshot snapshot = maintenanceCli.snapshot();
    responseObserverRef.get().onCompleted();
    OutputStream out = new ByteArrayOutputStream();
    snapshot.write(out);
    assertThatThrownBy(() -> snapshot.write(out)).isInstanceOf(IOException.class).hasMessageContaining("write is called more than once");
}
Also used : Snapshot(com.coreos.jetcd.Maintenance.Snapshot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 4 with Snapshot

use of com.coreos.jetcd.Maintenance.Snapshot in project jetcd by coreos.

the class MaintenanceUnitTest method testConnectionError.

@Test(timeOut = 1000)
public void testConnectionError() throws IOException {
    Snapshot snapshot = maintenanceCli.snapshot();
    OutputStream out = new ByteArrayOutputStream();
    executor.execute(() -> {
        try {
            Thread.sleep(50);
            responseObserverRef.get().onError(Status.ABORTED.asRuntimeException());
        } catch (InterruptedException e) {
            Assert.fail("expect no exception, but got InterruptedException", e);
        }
    });
    assertThatThrownBy(() -> snapshot.write(out)).isInstanceOf(IOException.class).hasMessageContaining("connection error");
}
Also used : Snapshot(com.coreos.jetcd.Maintenance.Snapshot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Example 5 with Snapshot

use of com.coreos.jetcd.Maintenance.Snapshot in project jetcd by coreos.

the class MaintenanceUnitTest method testCloseWhenWrite.

@Test(timeOut = 1000)
public void testCloseWhenWrite() throws IOException {
    Snapshot snapshot = maintenanceCli.snapshot();
    OutputStream out = new ByteArrayOutputStream();
    executor.execute(() -> {
        try {
            Thread.sleep(50);
            snapshot.close();
        } catch (Exception e) {
            Assert.fail("don't expect any exception, but got", e);
        }
    });
    assertThatThrownBy(() -> snapshot.write(out)).isInstanceOf(IOException.class);
}
Also used : Snapshot(com.coreos.jetcd.Maintenance.Snapshot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuthFailedException(com.coreos.jetcd.exception.AuthFailedException) ConnectException(com.coreos.jetcd.exception.ConnectException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) AfterTest(org.testng.annotations.AfterTest)

Aggregations

Snapshot (com.coreos.jetcd.Maintenance.Snapshot)7 Test (org.testng.annotations.Test)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 AfterTest (org.testng.annotations.AfterTest)6 BeforeTest (org.testng.annotations.BeforeTest)6 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)5 AuthFailedException (com.coreos.jetcd.exception.AuthFailedException)2 ConnectException (com.coreos.jetcd.exception.ConnectException)2 ByteString (com.google.protobuf.ByteString)2 ExecutionException (java.util.concurrent.ExecutionException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1