View Javadoc

1   /*
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.regionserver.wal;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNotSame;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.HBaseConfiguration;
25  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category({ RegionServerTests.class, SmallTests.class })
31  public class TestSyncFutureCache {
32  
33    @Test
34    public void testSyncFutureCacheLifeCycle() throws Exception {
35      final Configuration conf = HBaseConfiguration.create();
36      final SyncFutureCache cache = new SyncFutureCache(conf);
37      try {
38        SyncFuture future0 = cache.getIfPresentOrNew().reset(0);
39        assertNotNull(future0);
40        // Get another future from the same thread, should be different one.
41        SyncFuture future1 = cache.getIfPresentOrNew().reset(1);
42        assertNotNull(future1);
43        assertNotSame(future0, future1);
44        cache.offer(future1);
45        // Should override.
46        cache.offer(future0);
47        SyncFuture future3 = cache.getIfPresentOrNew();
48        // Should return the cached entry that was first offered back.
49        assertEquals(future3, future0);
50        final SyncFuture[] future4 = new SyncFuture[1];
51        // From a different thread
52        Thread t = new Thread(new Runnable() {
53          @Override public void run() {
54            future4[0] = cache.getIfPresentOrNew().reset(4);
55          }
56        });
57        t.start();
58        t.join();
59        assertNotNull(future4[0]);
60        assertNotSame(future3, future4[0]);
61        // Clean up
62        cache.offer(future3);
63        cache.offer(future4[0]);
64      } finally {
65        cache.clear();
66      }
67    }
68  }