Add unit test of caching, which fails now
@@ -3,13 +3,17 @@ package com.yahoo.vespa.hosted.provision.persistence;
import com.yahoo.path.Path;
import com.yahoo.transaction.NestedTransaction;
+import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
+import com.yahoo.vespa.curator.transaction.CuratorOperation;
import com.yahoo.vespa.curator.transaction.CuratorOperations;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;
+import com.yahoo.vespa.curator.transaction.TransactionChanges;
import org.junit.Test;
import java.util.List;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
@@ -47,7 +51,19 @@ public class CuratorDatabaseTest {
assertEquals(2, database.getChildren(Path.fromString("/2")).size());
assertFalse("We do not reuse cached data in different parts of the tree when there are commits",
children1Call3 == children1Call2);
+ }
+
+ @Test
+ public void testCacheInvalidation() throws Exception {
+ MockCurator curator = new MockCurator();
+ CuratorDatabase database = new CuratorDatabase(curator, Path.fromString("/"), true);
+ assertEquals(0L, (long)curator.counter("/changeCounter").get().get().postValue());
+ commitCreate("/1", database);
+ assertArrayEquals(new byte[0], database.getData(Path.fromString("/1")).get());
+ commitReadingWrite("/1", "hello".getBytes(), database);
+ // Data cached during commit of write transaction. Should be invalid now, and re-read.
+ assertArrayEquals("hello".getBytes(), database.getData(Path.fromString("/1")).get());
}
@Test
@@ -118,4 +134,33 @@ public class CuratorDatabaseTest {
t.commit();
}
+ private void commitReadingWrite(String path, byte[] data, CuratorDatabase database) {
+ NestedTransaction transaction = new NestedTransaction();
+ byte[] oldData = database.getData(Path.fromString(path)).get();
+ CuratorTransaction curatorTransaction = database.newCuratorTransactionIn(transaction);
+ // Add a dummy operation which reads the data and populates the cache during commit of the write.
+ curatorTransaction.add(new DummyOperation(() -> assertArrayEquals(oldData, database.getData(Path.fromString(path)).get())));
+ curatorTransaction.add(CuratorOperations.setData(path, data));
+ transaction.commit();
+ }
+
+ static class DummyOperation implements CuratorOperation {
+
+ private final Runnable task;
+
+ public DummyOperation(Runnable task) {
+ this.task = task;
+ }
+
+ @Override
+ public org.apache.curator.framework.api.transaction.CuratorTransaction and(org.apache.curator.framework.api.transaction.CuratorTransaction transaction) {
+ task.run();
+ return transaction;
+ }
+
+ @Override
+ public void check(Curator curator, TransactionChanges changes) { }
+
+ }
+
}
