1 package org.apache.directmemory.memory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }