View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.monitoring;
20  
21  import static org.junit.Assert.*;
22  
23  import java.util.concurrent.atomic.AtomicBoolean;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.junit.Test;
27  import org.junit.experimental.categories.Category;
28  
29  @Category(SmallTests.class)
30  public class TestTaskMonitor {
31  
32    @Test
33    public void testTaskMonitorBasics() {
34      TaskMonitor tm = new TaskMonitor();
35      assertTrue("Task monitor should start empty",
36          tm.getTasks().isEmpty());
37      
38      // Make a task and fetch it back out
39      MonitoredTask task = tm.createStatus("Test task");
40      MonitoredTask taskFromTm = tm.getTasks().get(0);
41      
42      // Make sure the state is reasonable.
43      assertEquals(task.getDescription(), taskFromTm.getDescription());
44      assertEquals(-1, taskFromTm.getCompletionTimestamp());
45      assertEquals(MonitoredTask.State.RUNNING, taskFromTm.getState());
46      
47      // Mark it as finished
48      task.markComplete("Finished!");
49      assertEquals(MonitoredTask.State.COMPLETE, task.getState());
50      
51      // It should still show up in the TaskMonitor list
52      assertEquals(1, tm.getTasks().size());
53      
54      // If we mark its completion time back a few minutes, it should get gced
55      task.expireNow();
56      assertEquals(0, tm.getTasks().size());
57    }
58    
59    @Test
60    public void testTasksGetAbortedOnLeak() throws InterruptedException {
61      final TaskMonitor tm = new TaskMonitor();
62      assertTrue("Task monitor should start empty",
63          tm.getTasks().isEmpty());
64      
65      final AtomicBoolean threadSuccess = new AtomicBoolean(false);
66      // Make a task in some other thread and leak it
67      Thread t = new Thread() {
68        @Override
69        public void run() {
70          MonitoredTask task = tm.createStatus("Test task");    
71          assertEquals(MonitoredTask.State.RUNNING, task.getState());
72          threadSuccess.set(true);
73        }
74      };
75      t.start();
76      t.join();
77      // Make sure the thread saw the correct state
78      assertTrue(threadSuccess.get());
79      
80      // Make sure the leaked reference gets cleared
81      System.gc();
82      System.gc();
83      System.gc();
84      
85      // Now it should be aborted 
86      MonitoredTask taskFromTm = tm.getTasks().get(0);
87      assertEquals(MonitoredTask.State.ABORTED, taskFromTm.getState());
88    }
89    
90    @Test
91    public void testTaskLimit() throws Exception {
92      TaskMonitor tm = new TaskMonitor();
93      for (int i = 0; i < TaskMonitor.MAX_TASKS + 10; i++) {
94        tm.createStatus("task " + i);
95      }
96      // Make sure it was limited correctly
97      assertEquals(TaskMonitor.MAX_TASKS, tm.getTasks().size());
98      // Make sure we culled the earlier tasks, not later
99      // (i.e. tasks 0 through 9 should have been deleted)
100     assertEquals("task 10", tm.getTasks().get(0).getDescription());
101   }
102 
103 
104 }
105