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 org.apache.directmemory.DirectMemory;
23  import org.apache.directmemory.measures.Ram;
24  import org.apache.directmemory.memory.AllocationPolicy;
25  import org.apache.directmemory.memory.MemoryManagerService;
26  import org.apache.directmemory.memory.MemoryManagerServiceImpl;
27  import org.apache.directmemory.memory.Pointer;
28  import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
29  import org.junit.Test;
30  
31  import java.io.IOException;
32  import java.io.Serializable;
33  
34  import static org.junit.Assert.*;
35  
36  public class CacheServiceImplTest
37  {
38  
39      @Test
40      public void testOffHeapExceedMemoryReturnNullWhenTrue()
41          throws IOException
42      {
43          AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
44          MemoryManagerService<byte[]> memoryManager = new MemoryManagerServiceImpl<byte[]>( allocationPolicy, true );
45          CacheService<Integer, byte[]> cache =
46              new DirectMemory<Integer, byte[]>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize( Ram.Mb( 1 ) ).newCacheService();
47  
48          for ( int i = 0; i < 1000; i++ )
49          {
50              Pointer<byte[]> pointer = cache.put( i, new byte[1024] );
51              if ( ( i % 100 ) == 0 )
52              {
53                  System.out.println( pointer );
54              }
55          }
56          assertTrue( "This test ensures that no unexpected errors/behaviours occurs when heap space is full", true );
57  
58          cache.close();
59      }
60  
61      private static class MyBean
62          implements Serializable
63      {
64          private static final long serialVersionUID = -8865690921195047235L;
65  
66          private String name;
67  
68          /**
69           * @return the name
70           */
71          @SuppressWarnings( "unused" )
72          public String getName()
73          {
74              return name;
75          }
76  
77          /**
78           * @param name the name to set
79           */
80          public void setName( String name )
81          {
82              this.name = name;
83          }
84      }
85  
86      @Test
87      public void testEntryIsNoMoreAvailableAfterExpiry()
88          throws InterruptedException, IOException
89      {
90          AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
91          MemoryManagerService<MyBean> memoryManager = new MemoryManagerServiceImpl<MyBean>( allocationPolicy, true );
92          CacheService<Integer, MyBean> cache =
93              new DirectMemory<Integer, MyBean>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize( Ram.Mb( 1 ) ).newCacheService();
94          /*
95           * let the scan run every 10s
96           */
97          cache.scheduleDisposalEvery( 3 * 1000 );
98          /*
99           * entry should be expired but not freed after 1s in the cache
100          */
101         MyBean originalEntry = new MyBean();
102         originalEntry.setName( "the name" );
103         cache.put( 1, originalEntry, 1 * 1000 );
104         Pointer<MyBean> pointer = cache.getPointer( 1 );
105         assertNotNull( pointer );
106         assertFalse( pointer.isExpired() );
107         assertFalse( pointer.isFree() );
108         /*
109          * wait for 2s to be sure the entry has been expired
110          */
111         Thread.sleep( 2000 );
112         pointer = cache.getPointer( 1 );
113         assertNotNull( pointer );
114         assertTrue( pointer.isExpired() );
115         assertFalse( pointer.isFree() );
116         /*
117          * wait for 11s to be sure the entry has been evicted
118          */
119         Thread.sleep( 4000 );
120         pointer = cache.getPointer( 1 );
121         assertNotNull( pointer );
122         assertTrue( pointer.isExpired() );
123         assertTrue( pointer.isFree() );
124 
125         cache.close();
126     }
127 
128 }