1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
41 SyncFuture future1 = cache.getIfPresentOrNew().reset(1);
42 assertNotNull(future1);
43 assertNotSame(future0, future1);
44 cache.offer(future1);
45
46 cache.offer(future0);
47 SyncFuture future3 = cache.getIfPresentOrNew();
48
49 assertEquals(future3, future0);
50 final SyncFuture[] future4 = new SyncFuture[1];
51
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
62 cache.offer(future3);
63 cache.offer(future4[0]);
64 } finally {
65 cache.clear();
66 }
67 }
68 }