View Javadoc

1   package org.apache.directmemory.memory;
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.measures.Ram;
23  import org.apache.directmemory.memory.buffer.MemoryBuffer;
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  import org.junit.Ignore;
27  import org.junit.Test;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.io.IOException;
32  import java.nio.ByteBuffer;
33  import java.util.Random;
34  
35  import static org.junit.Assert.*;
36  
37  @Ignore
38  public class NIOTest
39  {
40  
41      private static Logger logger = LoggerFactory.getLogger( NIOTest.class );
42  
43      @BeforeClass
44      public static void init()
45      {
46          byte[] payload = "012345678901234567890123456789012345678901234567890123456789".getBytes();
47  
48          logger.info( "init" );
49          MemoryManager.init( 1, Ram.Mb( 100 ) );
50  
51          logger.info( "payload size=" + Ram.inKb( payload.length ) );
52          long howMany = ( MemoryManager.capacity() / payload.length );
53          howMany = ( howMany * 50 ) / 100;
54  
55          for ( int i = 0; i < howMany; i++ )
56          {
57              Pointer<Object> p = MemoryManager.store( payload );
58              assertNotNull( p );
59          }
60  
61          logger.info( "" + howMany + " items stored" );
62      }
63  
64      @AfterClass
65      public static void cleanup()
66          throws IOException
67      {
68          MemoryManager.close();
69      }
70  
71      @Test
72      public void nioTest()
73      {
74          Random rnd = new Random();
75          int size = rnd.nextInt( 10 ) * (int) MemoryManager.capacity() / 100;
76          logger.info( "payload size=" + Ram.inKb( size ) );
77          Pointer<Object> p = MemoryManager.allocate( size );
78          MemoryBuffer b = p.getMemoryBuffer();
79          logger.info( "allocated" );
80          assertNotNull( p );
81          assertNotNull( b );
82  
83          // assertTrue( b.isDirect() );
84          assertEquals( 0, b.readerIndex() );
85          assertEquals( size, b.capacity() );
86  
87          byte[] check = MemoryManager.retrieve( p );
88  
89          assertNotNull( check );
90  
91          assertEquals( size, p.getCapacity() );
92          logger.info( "end" );
93      }
94  
95  }