View Javadoc

1   package org.apache.directmemory.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.Iterator;
24  
25  import org.apache.directmemory.DirectMemory;
26  import org.apache.directmemory.cache.CacheService;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertNotNull;
33  import static org.junit.Assert.assertNull;
34  import static org.junit.Assert.assertTrue;
35  
36  /**
37   * Testcase for {@link CacheValuesIterable}
38   */
39  public class CacheValuesIterableTest
40  {
41      private CacheService<String, Long> cache;
42  
43      @Before
44      public void setUp() {
45          cache = new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).newCacheService(); 
46      }
47      
48      @After
49      public void cleanUp() throws IOException {
50          cache.clear();
51          cache.close();
52      }
53  
54      @Test
55      public void simpleStrictCacheValuesIteratorTest()
56          throws Exception
57      {
58          assertNull( cache.retrieve( "a" ) );
59          assertNotNull( cache.put( "a", 1L ) );
60          assertNotNull( cache.put( "b", 2L ) );
61          assertNotNull( cache.put( "c", 3L ) );
62          assertNotNull( cache.retrieve( "a" ) );
63          assertNotNull( cache.retrieve( "b" ) );
64          assertNotNull( cache.retrieve( "c" ) );
65          assertEquals( 1L, cache.retrieve( "a" ).longValue() );
66  
67          CacheValuesIterable<String, Long> cacheValuesIterable = new CacheValuesIterable<String, Long>( cache );
68          for ( Long longVal : cacheValuesIterable )
69          {
70              assertNotNull( longVal );
71              assertTrue( longVal > 0 );
72          }
73      }
74  
75      @Test
76      public void simpleNonStrictCacheValuesIteratorTest()
77          throws Exception
78      {
79          assertNull( cache.retrieve( "a" ) );
80          assertNotNull( cache.put( "a", 1L ) );
81          assertNotNull( cache.put( "b", 2L ) );
82          assertNotNull( cache.put( "c", 3L ) );
83          assertNotNull( cache.retrieve( "a" ) );
84          assertNotNull( cache.retrieve( "b" ) );
85          assertNotNull( cache.retrieve( "c" ) );
86          assertEquals( 1L, cache.retrieve( "a" ).longValue() );
87  
88          CacheValuesIterable<String, Long> cacheValuesIterable = new CacheValuesIterable<String, Long>( cache, false );
89          int count = 0;
90          for ( Long longVal : cacheValuesIterable )
91          {
92              count++;
93              assertNotNull( longVal );
94              assertTrue( longVal > 0 );
95          }
96          assertEquals( 3, count );
97      }
98  
99      @Test
100     public void nonStrictCacheValuesIteratorShouldSkipExpiredItemsTest() throws Exception
101     {
102         assertNotNull( cache.put( "a", 1L ) );
103         assertNotNull( cache.put( "b", 2L, 1 ) );
104         assertNotNull( cache.put( "c", 3L ) );
105 
106         Thread.sleep( 10 );
107         CacheValuesIterable<String, Long> cacheValuesIterable = new CacheValuesIterable<String, Long>( cache, false );
108         int count = 0;
109         for ( Long longVal : cacheValuesIterable )
110         {
111             count++;
112             assertNotNull( longVal );
113             assertTrue( longVal > 0 );
114         }
115         assertEquals( 2, count );
116     }
117     
118     @Test
119     public void nonStrictCacheValuesIteratorRemoveTest() throws Exception {
120         assertNotNull( cache.put( "a", 1L ) );
121         assertNotNull( cache.put( "b", 2L ) );
122         assertNotNull( cache.put( "c", 3L ) );
123 
124         Iterator<Long> iterator = new CacheValuesIterable<String, Long>( cache, false ).iterator();
125         while ( iterator.hasNext() )
126         {
127             long longVal = iterator.next();
128             if (longVal == 2) {
129                 iterator.remove();
130             }
131         }
132         int count = 0;
133         iterator = new CacheValuesIterable<String, Long>( cache, false ).iterator();
134         while (iterator.hasNext()) {
135             iterator.next();
136             count++;
137         }
138         assertEquals( 2, count);
139 
140     }
141 }