PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/test/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java

http://github.com/apache/zookeeper
Java | 260 lines | 181 code | 54 blank | 25 comment | 3 complexity | 8c849511210f546eb4d3d26f12892cfc MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper.server;
  19. import org.apache.jute.BinaryInputArchive;
  20. import org.apache.jute.BinaryOutputArchive;
  21. import org.apache.zookeeper.ZooDefs;
  22. import org.apache.zookeeper.data.ACL;
  23. import org.apache.zookeeper.data.Id;
  24. import org.junit.Test;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.IOException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import static org.junit.Assert.*;
  31. public class ReferenceCountedACLCacheTest {
  32. @Test
  33. public void testSameACLGivesSameID() {
  34. List<ACL> testACL = createACL("myid");
  35. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  36. Long aclId = cache.convertAcls(testACL);
  37. List<ACL> testACL2 = createACL("myid");
  38. assertEquals(aclId, cache.convertAcls(testACL2));
  39. }
  40. @Test
  41. public void testWhetherOrderingMatters() {
  42. List<ACL> testACL = new ArrayList<ACL>();
  43. testACL.add(new ACL(ZooDefs.Perms.READ, new Id("scheme", "ro")));
  44. testACL.add(new ACL(ZooDefs.Perms.WRITE, new Id("scheme", "rw")));
  45. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  46. Long aclId = cache.convertAcls(testACL);
  47. List<ACL> testACL2 = new ArrayList<ACL>();
  48. testACL2.add(new ACL(ZooDefs.Perms.WRITE, new Id("scheme", "rw")));
  49. testACL2.add(new ACL(ZooDefs.Perms.READ, new Id("scheme", "ro")));
  50. assertFalse(aclId.equals(cache.convertAcls(testACL2)));
  51. }
  52. @Test
  53. public void testBidirectionality() {
  54. List<ACL> testACL = createACL("myid");
  55. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  56. Long aclId = cache.convertAcls(testACL);
  57. assertEquals(testACL, cache.convertLong(aclId));
  58. }
  59. @Test
  60. public void testCacheSize() {
  61. List<ACL> testACL = createACL("myid");
  62. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  63. Long aclId = cache.convertAcls(testACL);
  64. assertEquals(1, cache.size());
  65. List<ACL> testACL2 = createACL("myid");
  66. assertEquals(aclId, cache.convertAcls(testACL2));
  67. assertEquals(1, cache.size());
  68. List<ACL> testACL3 = createACL("differentId");
  69. Long aclId3 = cache.convertAcls(testACL3);
  70. assertFalse(aclId3.equals(aclId));
  71. assertEquals(2, cache.size());
  72. }
  73. @Test
  74. public void testAddThenRemove() {
  75. List<ACL> testACL = createACL("myid");
  76. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  77. Long aclId = cache.convertAcls(testACL);
  78. assertEquals(1, cache.size());
  79. cache.removeUsage(aclId);
  80. assertEquals(0, cache.size());
  81. }
  82. @Test
  83. public void testMultipleAddsAndRemove() {
  84. List<ACL> testACL = createACL("myid");
  85. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  86. Long aclId = cache.convertAcls(testACL);
  87. assertEquals(1, cache.size());
  88. cache.convertAcls(testACL);
  89. assertEquals(1, cache.size());
  90. List<ACL> testACL2 = createACL("anotherId");
  91. cache.convertAcls(testACL2);
  92. cache.removeUsage(aclId);
  93. assertEquals(2, cache.size());
  94. cache.removeUsage(aclId);
  95. assertEquals(1, cache.size());
  96. Long newId = cache.convertAcls(testACL);
  97. assertFalse(aclId.equals(newId));
  98. }
  99. @Test
  100. public void testAddUsage() {
  101. List<ACL> testACL = createACL("myid");
  102. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  103. Long aclId = cache.convertAcls(testACL);
  104. assertEquals(1, cache.size());
  105. cache.addUsage(aclId);
  106. assertEquals(1, cache.size());
  107. cache.removeUsage(aclId);
  108. assertEquals(1, cache.size());
  109. cache.removeUsage(aclId);
  110. assertEquals(0, cache.size());
  111. }
  112. @Test
  113. public void testAddNonExistentUsage() {
  114. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  115. cache.addUsage(1L);
  116. assertEquals(0, cache.size());
  117. /*
  118. On startup, it's possible that we'll try calling addUsage of an ID not in the cache. This is safe to ignore
  119. as it'll be added later when we traverse the tranlog. See discussion here:
  120. http://mail-archives.apache.org/mod_mbox/zookeeper-user/201507.mbox/%3CCAB5oV2_ujhvBA1sEkCG2WRakPjCy%2BNR10620WK2G1GGgmEO44g%40mail.gmail.com%3E
  121. This test makes sure that we don't add the ID to the cache in this case as that would result in dupes later
  122. and consequently incorrect counts and entries that will never be cleaned out.
  123. */
  124. }
  125. @Test
  126. public void testSerializeDeserialize() throws IOException {
  127. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  128. List<ACL> acl1 = createACL("one");
  129. List<ACL> acl2 = createACL("two");
  130. List<ACL> acl3 = createACL("three");
  131. List<ACL> acl4 = createACL("four");
  132. List<ACL> acl5 = createACL("five");
  133. Long aclId1 = convertACLsNTimes(cache, acl1, 1);
  134. Long aclId2 = convertACLsNTimes(cache, acl2, 2);
  135. Long aclId3 = convertACLsNTimes(cache, acl3, 3);
  136. Long aclId4 = convertACLsNTimes(cache, acl4, 4);
  137. Long aclId5 = convertACLsNTimes(cache, acl5, 5);
  138. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  139. BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
  140. cache.serialize(archive);
  141. BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
  142. ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
  143. deserializedCache.deserialize(inArchive);
  144. callAddUsageNTimes(deserializedCache, aclId1, 1);
  145. callAddUsageNTimes(deserializedCache, aclId2, 2);
  146. callAddUsageNTimes(deserializedCache, aclId3, 3);
  147. callAddUsageNTimes(deserializedCache, aclId4, 4);
  148. callAddUsageNTimes(deserializedCache, aclId5, 5);
  149. assertCachesEqual(cache, deserializedCache);
  150. }
  151. private void assertCachesEqual(ReferenceCountedACLCache expected, ReferenceCountedACLCache actual){
  152. assertEquals(expected.aclIndex, actual.aclIndex);
  153. assertEquals(expected.aclKeyMap, actual.aclKeyMap);
  154. assertEquals(expected.longKeyMap, actual.longKeyMap);
  155. assertEquals(expected.referenceCounter, actual.referenceCounter);
  156. }
  157. @Test
  158. public void testPurgeUnused() throws IOException {
  159. ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
  160. List<ACL> acl1 = createACL("one");
  161. List<ACL> acl2 = createACL("two");
  162. List<ACL> acl3 = createACL("three");
  163. List<ACL> acl4 = createACL("four");
  164. List<ACL> acl5 = createACL("five");
  165. Long aclId1 = convertACLsNTimes(cache, acl1, 1);
  166. Long aclId2 = convertACLsNTimes(cache, acl2, 2);
  167. Long aclId3 = convertACLsNTimes(cache, acl3, 3);
  168. Long aclId4 = convertACLsNTimes(cache, acl4, 4);
  169. Long aclId5 = convertACLsNTimes(cache, acl5, 5);
  170. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  171. BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
  172. cache.serialize(archive);
  173. BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
  174. ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
  175. deserializedCache.deserialize(inArchive);
  176. callAddUsageNTimes(deserializedCache, aclId1, 1);
  177. callAddUsageNTimes(deserializedCache, aclId2, 2);
  178. deserializedCache.purgeUnused();
  179. assertEquals(2, deserializedCache.size());
  180. assertEquals(aclId1, deserializedCache.convertAcls(acl1));
  181. assertEquals(aclId2, deserializedCache.convertAcls(acl2));
  182. assertFalse(acl3.equals(deserializedCache.convertAcls(acl3)));
  183. assertFalse(acl4.equals(deserializedCache.convertAcls(acl4)));
  184. assertFalse(acl5.equals(deserializedCache.convertAcls(acl5)));
  185. }
  186. private void callAddUsageNTimes(ReferenceCountedACLCache deserializedCache, Long aclId, int num) {
  187. for (int i = 0; i < num; i++) {
  188. deserializedCache.addUsage(aclId);
  189. }
  190. }
  191. private Long convertACLsNTimes(ReferenceCountedACLCache cache, List<ACL> acl, int num) {
  192. if (num <= 0) {
  193. return -1L;
  194. }
  195. for (int i = 0; i < num -1; i++) {
  196. cache.convertAcls(acl);
  197. }
  198. return cache.convertAcls(acl);
  199. }
  200. private List<ACL> createACL(String id) {
  201. List<ACL> acl1 = new ArrayList<ACL>();
  202. acl1.add(new ACL(ZooDefs.Perms.ADMIN, new Id("scheme", id)));
  203. return acl1;
  204. }
  205. }