View Javadoc

1   package org.apache.directmemory.cache;
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 static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  
26  import org.apache.directmemory.DirectMemory;
27  import org.apache.directmemory.memory.Pointer;
28  import org.apache.directmemory.memory.UnsafeMemoryManagerServiceImpl;
29  import org.junit.Test;
30  
31  public class BasicTest
32  {
33      @Test
34      public void putRetrieveAndUpdate()
35          throws IOException
36      {
37          CacheService<String, Long> cache =
38              new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).newCacheService();
39  
40          assertNull( cache.retrieve( "a" ) );
41          assertNotNull( cache.put( "a", 3L ) );
42          assertNotNull( cache.retrieve( "a" ) );
43          assertEquals( 3L, cache.retrieve( "a" ).longValue() );
44  
45          Pointer<Long> ptr = cache.put( "a", 5L );
46          assertNotNull( ptr );
47          assertFalse( ptr.isExpired() );
48          assertFalse( ptr.isFree() );
49          assertNotNull( "pointer should not be null", cache.retrieve( "a" ) );
50          assertEquals( 5L, cache.retrieve( "a" ).longValue() );
51  
52          cache.close();
53      }
54  
55      @Test
56      public void putRetrieveAndUpdateWithUnsafe()
57          throws IOException
58      {
59          CacheService<String, Long> cache =
60              new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).setMemoryManager( new UnsafeMemoryManagerServiceImpl<Long>() ).newCacheService();
61  
62          assertNull( cache.retrieve( "a" ) );
63          assertNotNull( cache.put( "a", 3L ) );
64          assertNotNull( cache.retrieve( "a" ) );
65          assertEquals( 3L, cache.retrieve( "a" ).longValue() );
66  
67          Pointer<Long> ptr = cache.put( "a", 5L );
68          assertNotNull( ptr );
69          assertFalse( ptr.isExpired() );
70          assertFalse( ptr.isFree() );
71          assertNotNull( "pointer should not be null", cache.retrieve( "a" ) );
72          assertEquals( 5L, cache.retrieve( "a" ).longValue() );
73  
74          cache.close();
75      }
76  
77  }