1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.persistence;
import com.yahoo.path.Path;
import com.yahoo.transaction.NestedTransaction;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.curator.transaction.CuratorOperations;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;
import org.junit.Test;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/**
* Tests the curator db directly.
* This verifies the details of the current implementation of the database, not just its API;
* breaking this does not necessarily mean that a change is wrong.
*
* @author bratseth
*/
public class CuratorDatabaseTest {
@Test
public void testTransactionsIncreaseTimer() 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);
commitCreate("/2", database);
commitCreate("/1/1", database);
commitCreate("/2/1", database);
assertEquals(4L, (long)curator.counter("/changeCounter").get().get().postValue());
List<String> children1Call0 = database.getChildren(Path.fromString("/1")); // prime the db; this call returns a different instance
List<String> children1Call1 = database.getChildren(Path.fromString("/1"));
List<String> children1Call2 = database.getChildren(Path.fromString("/1"));
assertTrue("We reuse cached data when there are no commits", children1Call1 == children1Call2);
assertEquals(1, database.getChildren(Path.fromString("/2")).size());
commitCreate("/2/2", database);
List<String> children1Call3 = database.getChildren(Path.fromString("/1"));
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 testTransactionsWithDeactivatedCache() throws Exception {
MockCurator curator = new MockCurator();
CuratorDatabase database = new CuratorDatabase(curator, Path.fromString("/"), false);
assertEquals(0L, (long)curator.counter("/changeCounter").get().get().postValue());
commitCreate("/1", database);
commitCreate("/2", database);
commitCreate("/1/1", database);
commitCreate("/2/1", database);
assertEquals(4L, (long)curator.counter("/changeCounter").get().get().postValue());
List<String> children1Call0 = database.getChildren(Path.fromString("/1")); // prime the db; this call returns a different instance
List<String> children1Call1 = database.getChildren(Path.fromString("/1"));
List<String> children1Call2 = database.getChildren(Path.fromString("/1"));
assertTrue("No cache, no reused data", children1Call1 != children1Call2);
}
@Test
public void testThatCounterIncreasesAlsoOnCommitFailure() throws Exception {
MockCurator curator = new MockCurator();
CuratorDatabase database = new CuratorDatabase(curator, Path.fromString("/"), true);
assertEquals(0L, (long)curator.counter("/changeCounter").get().get().postValue());
try {
commitCreate("/1/2", database); // fail as parent does not exist
fail("Expected exception");
}
catch (Exception expected) {
// expected because the parent does not exist
}
assertEquals(1L, (long)curator.counter("/changeCounter").get().get().postValue());
}
@Test
public void testThatCounterIncreasesAlsoOnCommitFailureFromExistingTransaction() throws Exception {
MockCurator curator = new MockCurator();
CuratorDatabase database = new CuratorDatabase(curator, Path.fromString("/"), true);
assertEquals(0L, (long)curator.counter("/changeCounter").get().get().postValue());
try {
NestedTransaction t = new NestedTransaction();
CuratorTransaction separateC = new CuratorTransaction(curator);
separateC.add(CuratorOperations.create("/1/2")); // fail as parent does not exist
t.add(separateC);
CuratorTransaction c = database.newCuratorTransactionIn(t);
c.add(CuratorOperations.create("/1")); // does not fail
t.commit();
fail("Expected exception");
}
catch (Exception expected) {
// expected because the parent does not exist
}
assertEquals(1L, (long)curator.counter("/changeCounter").get().get().postValue());
}
private void commitCreate(String path, CuratorDatabase database) {
NestedTransaction t = new NestedTransaction();
CuratorTransaction c = database.newCuratorTransactionIn(t);
c.add(CuratorOperations.create(path));
t.commit();
}
}
|