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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.conf
;
20 import static org
.junit
.Assert
.assertFalse
;
21 import static org
.junit
.Assert
.assertTrue
;
23 import org
.apache
.hadoop
.conf
.Configuration
;
24 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
25 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
26 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
27 import org
.junit
.ClassRule
;
28 import org
.junit
.Test
;
29 import org
.junit
.experimental
.categories
.Category
;
30 import org
.slf4j
.Logger
;
31 import org
.slf4j
.LoggerFactory
;
33 @Category({SmallTests
.class, ClientTests
.class})
34 public class TestConfigurationManager
{
37 public static final HBaseClassTestRule CLASS_RULE
=
38 HBaseClassTestRule
.forClass(TestConfigurationManager
.class);
40 private static final Logger LOG
= LoggerFactory
.getLogger(TestConfigurationManager
.class);
42 class DummyConfigurationObserver
implements ConfigurationObserver
{
43 private boolean notifiedOnChange
= false;
44 private ConfigurationManager cm
;
46 public DummyConfigurationObserver(ConfigurationManager cm
) {
52 public void onConfigurationChange(Configuration conf
) {
53 notifiedOnChange
= true;
56 // Was the observer notified on Configuration change?
57 public boolean wasNotifiedOnChange() {
58 return notifiedOnChange
;
61 public void resetNotifiedOnChange() {
62 notifiedOnChange
= false;
65 public void register() {
66 this.cm
.registerObserver(this);
69 public void deregister() {
70 this.cm
.deregisterObserver(this);
75 * Test if observers get notified by the <code>ConfigurationManager</code>
76 * when the Configuration is reloaded.
79 public void testCheckIfObserversNotified() {
80 Configuration conf
= new Configuration();
81 ConfigurationManager cm
= new ConfigurationManager();
82 DummyConfigurationObserver d1
= new DummyConfigurationObserver(cm
);
84 // Check if we get notified.
85 cm
.notifyAllObservers(conf
);
86 assertTrue(d1
.wasNotifiedOnChange());
87 d1
.resetNotifiedOnChange();
89 // Now check if we get notified on change with more than one observers.
90 DummyConfigurationObserver d2
= new DummyConfigurationObserver(cm
);
91 cm
.notifyAllObservers(conf
);
92 assertTrue(d1
.wasNotifiedOnChange());
93 d1
.resetNotifiedOnChange();
94 assertTrue(d2
.wasNotifiedOnChange());
95 d2
.resetNotifiedOnChange();
97 // Now try deregistering an observer and verify that it was not notified
99 cm
.notifyAllObservers(conf
);
100 assertTrue(d1
.wasNotifiedOnChange());
101 d1
.resetNotifiedOnChange();
102 assertFalse(d2
.wasNotifiedOnChange());
105 // Register an observer that will go out of scope immediately, allowing
106 // us to test that out of scope observers are deregistered.
107 private void registerLocalObserver(ConfigurationManager cm
) {
108 new DummyConfigurationObserver(cm
);
112 * Test if out-of-scope observers are deregistered on GC.
115 public void testDeregisterOnOutOfScope() {
116 Configuration conf
= new Configuration();
117 ConfigurationManager cm
= new ConfigurationManager();
119 boolean outOfScopeObserversDeregistered
= false;
121 // On my machine, I was able to cause a GC after around 5 iterations.
122 // If we do not cause a GC in 100k iterations, which is very unlikely,
123 // there might be something wrong with the GC.
124 for (int i
= 0; i
< 100000; i
++) {
125 registerLocalObserver(cm
);
126 cm
.notifyAllObservers(conf
);
128 // 'Suggest' the system to do a GC. We should be able to cause GC
129 // atleast once in the 2000 iterations.
132 // If GC indeed happened, all the observers (which are all out of scope),
133 // should have been deregistered.
134 if (cm
.getNumObservers() <= i
) {
135 outOfScopeObserversDeregistered
= true;
139 if (!outOfScopeObserversDeregistered
) {
140 LOG
.warn("Observers were not GC-ed! Something seems to be wrong.");
142 assertTrue(outOfScopeObserversDeregistered
);