1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.eviction; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.Region; |
12 |
| import org.jboss.cache.RegionManager; |
13 |
| |
14 |
| import java.util.Iterator; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| public class ElementSizeAlgorithmTest extends TestCase |
21 |
| { |
22 |
| RegionManager regionManager; |
23 |
| ElementSizeAlgorithm algo; |
24 |
| |
25 |
2
| public ElementSizeAlgorithmTest(String s)
|
26 |
| { |
27 |
2
| super(s);
|
28 |
| } |
29 |
| |
30 |
2
| public void setUp() throws Exception
|
31 |
| { |
32 |
2
| super.setUp();
|
33 |
| |
34 |
2
| algo = new ElementSizeAlgorithm();
|
35 |
2
| regionManager = new RegionManager();
|
36 |
2
| ElementSizeConfiguration config = new ElementSizeConfiguration();
|
37 |
| |
38 |
2
| config.setMaxElementsPerNode(0);
|
39 |
2
| config.setEvictionPolicyClass(DummyEvictionPolicy.class.getName());
|
40 |
2
| regionManager.getRegion("/a/b", true).setEvictionPolicy(config);
|
41 |
| } |
42 |
| |
43 |
2
| public void tearDown() throws Exception
|
44 |
| { |
45 |
2
| super.tearDown();
|
46 |
| } |
47 |
| |
48 |
1
| public void testMaxElements() throws Exception
|
49 |
| { |
50 |
1
| Region region = regionManager.getRegion("/a/b", true);
|
51 |
1
| ElementSizeConfiguration config = (ElementSizeConfiguration) region.getEvictionPolicyConfig();
|
52 |
1
| config.setMaxNodes(10);
|
53 |
1
| config.setMaxElementsPerNode(6);
|
54 |
| |
55 |
1
| for (int i = 0; i < 10; i++)
|
56 |
| { |
57 |
10
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
58 |
10
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
|
59 |
10
| if (i % 2 == 0)
|
60 |
| { |
61 |
5
| for (int k = 0; k < i; k++)
|
62 |
| { |
63 |
20
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT));
|
64 |
| } |
65 |
| } |
66 |
| } |
67 |
| |
68 |
1
| algo.process(region);
|
69 |
| |
70 |
1
| ElementSizeQueue queue = (ElementSizeQueue) algo.evictionQueue;
|
71 |
1
| assertEquals(9, algo.getEvictionQueue().getNumberOfNodes());
|
72 |
1
| assertEquals(12, algo.getEvictionQueue().getNumberOfElements());
|
73 |
| |
74 |
1
| Iterator it = queue.iterate();
|
75 |
1
| int count = 6;
|
76 |
1
| while (it.hasNext())
|
77 |
| { |
78 |
9
| NodeEntry ne = (NodeEntry) it.next();
|
79 |
9
| System.out.println(ne);
|
80 |
| |
81 |
9
| if (count > 0)
|
82 |
| { |
83 |
3
| assertEquals(count, ne.getNumberOfElements());
|
84 |
| } |
85 |
| else |
86 |
| { |
87 |
6
| assertEquals(0, ne.getNumberOfElements());
|
88 |
| } |
89 |
9
| count -= 2;
|
90 |
| } |
91 |
| |
92 |
1
| for (int i = 0; i < 7; i++)
|
93 |
| { |
94 |
7
| region.putNodeEvent(new EvictedEventNode(Fqn.fromString("/a/b/9"), NodeEventType.ADD_ELEMENT_EVENT));
|
95 |
7
| region.putNodeEvent(new EvictedEventNode(Fqn.fromString("/a/b/7"), NodeEventType.ADD_ELEMENT_EVENT));
|
96 |
| } |
97 |
| |
98 |
1
| algo.process(region);
|
99 |
| |
100 |
1
| assertEquals(7, queue.getNumberOfNodes());
|
101 |
| } |
102 |
| |
103 |
1
| public void testMaxNodesAndMaxElements() throws Exception
|
104 |
| { |
105 |
1
| Region region = regionManager.getRegion("/a/b", true);
|
106 |
1
| ElementSizeConfiguration config = (ElementSizeConfiguration) region.getEvictionPolicyConfig();
|
107 |
1
| config.setMaxNodes(10);
|
108 |
1
| config.setMaxElementsPerNode(100);
|
109 |
| |
110 |
1
| for (int i = 0; i < 20; i++)
|
111 |
| { |
112 |
20
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
113 |
20
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
|
114 |
20
| for (int k = 0; k < i; k++)
|
115 |
| { |
116 |
190
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT));
|
117 |
| |
118 |
| } |
119 |
| } |
120 |
| |
121 |
1
| algo.process(region);
|
122 |
| |
123 |
1
| ElementSizeQueue queue = (ElementSizeQueue) algo.evictionQueue;
|
124 |
1
| assertEquals(10, algo.getEvictionQueue().getNumberOfNodes());
|
125 |
1
| assertEquals(45, algo.getEvictionQueue().getNumberOfElements());
|
126 |
| |
127 |
| |
128 |
1
| Iterator it = queue.iterate();
|
129 |
1
| int num = 9;
|
130 |
1
| while (it.hasNext())
|
131 |
| { |
132 |
10
| NodeEntry ne = (NodeEntry) it.next();
|
133 |
10
| assertEquals(num, ne.getNumberOfElements());
|
134 |
10
| num--;
|
135 |
| } |
136 |
| |
137 |
| } |
138 |
| |
139 |
| } |