Search in sources :

Example 1 with PowerMockito

use of org.powermock.api.mockito.PowerMockito in project flink by apache.

the class RocksDBMemoryControllerUtilsTest method testCreateSharedResourcesWithExpectedCapacity.

@Test
public void testCreateSharedResourcesWithExpectedCapacity() {
    PowerMockito.mockStatic(RocksDBMemoryControllerUtils.class);
    final AtomicLong actualCacheCapacity = new AtomicLong(0L);
    final AtomicLong actualWbmCapacity = new AtomicLong(0L);
    when(RocksDBMemoryControllerUtils.allocateRocksDBSharedResources(anyLong(), anyDouble(), anyDouble(), anyBoolean())).thenCallRealMethod();
    when(RocksDBMemoryControllerUtils.calculateActualCacheCapacity(anyLong(), anyDouble())).thenCallRealMethod();
    when(RocksDBMemoryControllerUtils.calculateWriteBufferManagerCapacity(anyLong(), anyDouble())).thenCallRealMethod();
    // because PowerMockito cannot mock on native static method easily,
    // we introduce `createCache` and `createWriteBufferManager` wrappers here.
    when(RocksDBMemoryControllerUtils.createCache(anyLong(), anyDouble())).thenAnswer((Answer<LRUCache>) invocation -> {
        Object[] arguments = invocation.getArguments();
        actualCacheCapacity.set((long) arguments[0]);
        return (LRUCache) invocation.callRealMethod();
    });
    when(RocksDBMemoryControllerUtils.createWriteBufferManager(anyLong(), any(Cache.class))).thenAnswer((Answer<WriteBufferManager>) invocation -> {
        Object[] arguments = invocation.getArguments();
        actualWbmCapacity.set((long) arguments[0]);
        return (WriteBufferManager) invocation.callRealMethod();
    });
    long totalMemorySize = 2048L;
    double writeBufferRatio = 0.5;
    double highPriPoolRatio = 0.1;
    RocksDBSharedResources rocksDBSharedResources = RocksDBMemoryControllerUtils.allocateRocksDBSharedResources(totalMemorySize, writeBufferRatio, highPriPoolRatio, false);
    long expectedCacheCapacity = RocksDBMemoryControllerUtils.calculateActualCacheCapacity(totalMemorySize, writeBufferRatio);
    long expectedWbmCapacity = RocksDBMemoryControllerUtils.calculateWriteBufferManagerCapacity(totalMemorySize, writeBufferRatio);
    assertThat(actualCacheCapacity.get(), is(expectedCacheCapacity));
    assertThat(actualWbmCapacity.get(), is(expectedWbmCapacity));
    assertThat(rocksDBSharedResources.getWriteBufferManagerCapacity(), is(expectedWbmCapacity));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Cache(org.rocksdb.Cache) PowerMockito.when(org.powermock.api.mockito.PowerMockito.when) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) LRUCache(org.rocksdb.LRUCache) WriteBufferManager(org.rocksdb.WriteBufferManager) Answer(org.mockito.stubbing.Answer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) NativeLibraryLoader(org.rocksdb.NativeLibraryLoader) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) LRUCache(org.rocksdb.LRUCache) WriteBufferManager(org.rocksdb.WriteBufferManager) Cache(org.rocksdb.Cache) LRUCache(org.rocksdb.LRUCache) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

IOException (java.io.IOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 TemporaryFolder (org.junit.rules.TemporaryFolder)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)1 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 Answer (org.mockito.stubbing.Answer)1 PowerMockito (org.powermock.api.mockito.PowerMockito)1 PowerMockito.when (org.powermock.api.mockito.PowerMockito.when)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 PowerMockRunner (org.powermock.modules.junit4.PowerMockRunner)1