1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.filter;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27
28 import junit.framework.TestCase;
29
30 import org.apache.hadoop.hbase.SmallTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.experimental.categories.Category;
33
34 @Category(SmallTests.class)
35 public class TestRandomRowFilter extends TestCase {
36 protected RandomRowFilter quarterChanceFilter;
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 quarterChanceFilter = new RandomRowFilter(0.25f);
42 }
43
44
45
46
47
48
49 public void testBasics() throws Exception {
50 int included = 0;
51 int max = 1000000;
52 for (int i = 0; i < max; i++) {
53 if (!quarterChanceFilter.filterRowKey(Bytes.toBytes("row"), 0, Bytes
54 .toBytes("row").length)) {
55 included++;
56 }
57 }
58
59
60
61 int epsilon = max / 100;
62 assertTrue("Roughly 25% should pass the filter", Math.abs(included - max
63 / 4) < epsilon);
64 }
65
66
67
68
69
70
71 public void testSerialization() throws Exception {
72 RandomRowFilter newFilter = serializationTest(quarterChanceFilter);
73
74 assertTrue("float should be equal", Math.abs(newFilter.getChance()
75 - quarterChanceFilter.getChance()) < 0.000001f);
76 }
77
78 private RandomRowFilter serializationTest(RandomRowFilter filter)
79 throws Exception {
80
81 ByteArrayOutputStream stream = new ByteArrayOutputStream();
82 DataOutputStream out = new DataOutputStream(stream);
83 filter.write(out);
84 out.close();
85 byte[] buffer = stream.toByteArray();
86
87
88 DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
89 RandomRowFilter newFilter = new RandomRowFilter();
90 newFilter.readFields(in);
91
92 return newFilter;
93 }
94
95 @org.junit.Rule
96 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
97 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
98 }
99