Skip to content

Commit

Permalink
Guard for platforms not able to report memory allocation
Browse files Browse the repository at this point in the history
Not all platforms support reporting the memory allocation per thread.
On those platforms dbms.listQueries() reports null.
  • Loading branch information
thobe committed Apr 5, 2017
1 parent 9c624a1 commit 29882cb
Showing 1 changed file with 12 additions and 10 deletions.
Expand Up @@ -30,6 +30,7 @@
import java.util.function.BiConsumer;
import java.util.function.Supplier;

import org.hamcrest.Matcher;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -46,13 +47,16 @@
import static java.util.Collections.singletonMap;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -151,7 +155,7 @@ public void shouldProvideAllocatedBytes() throws Exception
// given
String query = "MATCH (n) SET n.v = n.v + 1";
final Node node;
final long uncachedBytes, cachedBytes;
final Long bytes;
try ( Resource<Node> test = test( db::createNode, Transaction::acquireWriteLock, query ) )
{
node = test.resource();
Expand All @@ -161,9 +165,9 @@ public void shouldProvideAllocatedBytes() throws Exception
// then
assertThat( data, hasKey( "allocatedBytes" ) );
Object allocatedBytes = data.get( "allocatedBytes" );
assertThat( allocatedBytes, instanceOf( Long.class ) );
uncachedBytes = (Long)allocatedBytes;
assertThat( uncachedBytes, greaterThan( 0L ) );
assertThat( allocatedBytes, anyOf( nullValue(), (Matcher) allOf(
instanceOf( Long.class ), greaterThan( 0L ) ) ) );
bytes = (Long) allocatedBytes;
}
// execute a second time, this time the query should be cached, and thus allocate less
try ( Resource<Node> test = test( () -> node, Transaction::acquireWriteLock, query ) )
Expand All @@ -172,12 +176,10 @@ public void shouldProvideAllocatedBytes() throws Exception
Map<String,Object> data = getQueryListing( query );

// then
assertThat( data, hasKey( "allocatedBytes" ) );
Object allocatedBytes = data.get( "allocatedBytes" );
assertThat( allocatedBytes, instanceOf( Long.class ) );
cachedBytes = (Long)allocatedBytes;
assertThat( cachedBytes, greaterThan( 0L ) );
assertThat( cachedBytes, lessThanOrEqualTo( uncachedBytes ) );
@SuppressWarnings( "RedundantCast" )
Matcher<Object> allocatedBytes = bytes == null ? nullValue() : (Matcher) allOf(
greaterThan( 0L ), lessThanOrEqualTo( bytes ) );
assertThat( data, hasEntry( equalTo( "allocatedBytes" ), allocatedBytes ) );
assertSame( node, test.resource() );
}
}
Expand Down

0 comments on commit 29882cb

Please sign in to comment.