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 |
| |
21 |
| |
22 |
| public class FIFOAlgorithmTest extends TestCase |
23 |
| { |
24 |
| |
25 |
| RegionManager regionManager; |
26 |
| FIFOAlgorithm algo; |
27 |
| |
28 |
2
| public FIFOAlgorithmTest(String s)
|
29 |
| { |
30 |
2
| super(s);
|
31 |
| } |
32 |
| |
33 |
2
| public void setUp() throws Exception
|
34 |
| { |
35 |
2
| super.setUp();
|
36 |
| |
37 |
2
| algo = new FIFOAlgorithm();
|
38 |
2
| FIFOConfiguration config = new FIFOConfiguration();
|
39 |
| |
40 |
2
| config.setMaxNodes(0);
|
41 |
2
| regionManager = new RegionManager();
|
42 |
2
| config.setEvictionPolicyClass(DummyEvictionPolicy.class.getName());
|
43 |
2
| regionManager.getRegion("/a/b", true).setEvictionPolicy(config);
|
44 |
| } |
45 |
| |
46 |
2
| public void tearDown() throws Exception
|
47 |
| { |
48 |
2
| super.tearDown();
|
49 |
| } |
50 |
| |
51 |
1
| public void testMaxNodes1() throws Exception
|
52 |
| { |
53 |
1
| Region region = regionManager.getRegion("/a/b", true);
|
54 |
1
| FIFOConfiguration config = (FIFOConfiguration) region.getEvictionPolicyConfig();
|
55 |
1
| config.setMaxNodes(5);
|
56 |
| |
57 |
1
| for (int i = 0; i < 8; i++)
|
58 |
| { |
59 |
8
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
60 |
8
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
|
61 |
| } |
62 |
| |
63 |
1
| algo.process(region);
|
64 |
1
| FIFOQueue queue = (FIFOQueue) algo.evictionQueue;
|
65 |
1
| assertEquals(5, algo.getEvictionQueue().getNumberOfNodes());
|
66 |
| |
67 |
| |
68 |
1
| Iterator it = queue.iterate();
|
69 |
1
| int index = 3;
|
70 |
1
| while (it.hasNext())
|
71 |
| { |
72 |
5
| NodeEntry ne = (NodeEntry) it.next();
|
73 |
5
| String fqn = ne.getFqn().toString();
|
74 |
5
| assertTrue(fqn.endsWith("/" + Integer.toString(index)));
|
75 |
5
| index++;
|
76 |
| } |
77 |
| |
78 |
| |
79 |
1
| for (int i = 3; i < 8; i++)
|
80 |
| { |
81 |
5
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
82 |
5
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
|
83 |
| } |
84 |
1
| for (int i = 3; i < 5; i++)
|
85 |
| { |
86 |
2
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
87 |
2
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT));
|
88 |
| } |
89 |
| |
90 |
1
| algo.process(region);
|
91 |
| |
92 |
1
| assertEquals(5, algo.getEvictionQueue().getNumberOfNodes());
|
93 |
| |
94 |
1
| it = queue.iterate();
|
95 |
1
| index = 3;
|
96 |
1
| while (it.hasNext())
|
97 |
| { |
98 |
5
| NodeEntry ne = (NodeEntry) it.next();
|
99 |
5
| String fqn = ne.getFqn().toString();
|
100 |
5
| assertTrue(fqn.endsWith("/" + Integer.toString(index)));
|
101 |
5
| index++;
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
1
| public void testMaxNodes2() throws Exception
|
106 |
| { |
107 |
1
| Region region = regionManager.getRegion("/a/b", true);
|
108 |
1
| FIFOConfiguration config = (FIFOConfiguration) region.getEvictionPolicyConfig();
|
109 |
1
| config.setMaxNodes(50000);
|
110 |
| |
111 |
1
| for (int i = 0; i < 50000; i++)
|
112 |
| { |
113 |
50000
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
114 |
50000
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
|
115 |
| } |
116 |
| |
117 |
1
| algo.process(region);
|
118 |
1
| FIFOQueue queue = (FIFOQueue) algo.evictionQueue;
|
119 |
1
| assertEquals(50000, algo.getEvictionQueue().getNumberOfNodes());
|
120 |
| |
121 |
1
| Iterator it = queue.iterate();
|
122 |
1
| int index = 0;
|
123 |
1
| while (it.hasNext())
|
124 |
| { |
125 |
50000
| NodeEntry ne = (NodeEntry) it.next();
|
126 |
50000
| assertTrue(ne.getFqn().toString().endsWith("/" + Integer.toString(index)));
|
127 |
50000
| index++;
|
128 |
| } |
129 |
| |
130 |
1
| for (int i = 50000; i < 60000; i++)
|
131 |
| { |
132 |
10000
| Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
|
133 |
10000
| region.putNodeEvent(new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT));
|
134 |
| } |
135 |
| |
136 |
1
| algo.process(region);
|
137 |
| |
138 |
1
| it = queue.iterate();
|
139 |
1
| index = 10000;
|
140 |
1
| while (it.hasNext())
|
141 |
| { |
142 |
50000
| NodeEntry ne = (NodeEntry) it.next();
|
143 |
50000
| assertTrue(ne.getFqn().toString().endsWith("/" + Integer.toString(index)));
|
144 |
50000
| index++;
|
145 |
| } |
146 |
| } |
147 |
| } |