1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.conf;
20
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 @Category(SmallTests.class)
32 public class TestConfigurationManager {
33 private static final Log LOG = LogFactory.getLog(TestConfigurationManager.class);
34
35 class DummyConfigurationObserver implements ConfigurationObserver {
36 private boolean notifiedOnChange = false;
37 private ConfigurationManager cm;
38
39 public DummyConfigurationObserver(ConfigurationManager cm) {
40 this.cm = cm;
41 register();
42 }
43
44 public void onConfigurationChange(Configuration conf) {
45 notifiedOnChange = true;
46 }
47
48
49 public boolean wasNotifiedOnChange() {
50 return notifiedOnChange;
51 }
52
53 public void resetNotifiedOnChange() {
54 notifiedOnChange = false;
55 }
56
57 public void register() {
58 this.cm.registerObserver(this);
59 }
60
61 public void deregister() {
62 this.cm.deregisterObserver(this);
63 }
64 }
65
66
67
68
69
70 @Test
71 public void testCheckIfObserversNotified() {
72 Configuration conf = new Configuration();
73 ConfigurationManager cm = new ConfigurationManager();
74 DummyConfigurationObserver d1 = new DummyConfigurationObserver(cm);
75
76
77 cm.notifyAllObservers(conf);
78 assertTrue(d1.wasNotifiedOnChange());
79 d1.resetNotifiedOnChange();
80
81
82 DummyConfigurationObserver d2 = new DummyConfigurationObserver(cm);
83 cm.notifyAllObservers(conf);
84 assertTrue(d1.wasNotifiedOnChange());
85 d1.resetNotifiedOnChange();
86 assertTrue(d2.wasNotifiedOnChange());
87 d2.resetNotifiedOnChange();
88
89
90 d2.deregister();
91 cm.notifyAllObservers(conf);
92 assertTrue(d1.wasNotifiedOnChange());
93 d1.resetNotifiedOnChange();
94 assertFalse(d2.wasNotifiedOnChange());
95 }
96
97
98
99 private void registerLocalObserver(ConfigurationManager cm) {
100 new DummyConfigurationObserver(cm);
101 }
102
103
104
105
106 @Test
107 public void testDeregisterOnOutOfScope() {
108 Configuration conf = new Configuration();
109 ConfigurationManager cm = new ConfigurationManager();
110
111 boolean outOfScopeObserversDeregistered = false;
112
113
114
115
116 for (int i = 0; i < 100000; i++) {
117 registerLocalObserver(cm);
118 cm.notifyAllObservers(conf);
119
120
121
122 System.gc();
123
124
125
126 if (cm.getNumObservers() <= i) {
127 outOfScopeObserversDeregistered = true;
128 break;
129 }
130 }
131 if (!outOfScopeObserversDeregistered) {
132 LOG.warn("Observers were not GC-ed! Something seems to be wrong.");
133 }
134 assertTrue(outOfScopeObserversDeregistered);
135 }
136 }