Skip to content

Commit

Permalink
Adapt timeseries tests to avoid mockito on ByteBuffer failing on java19
Browse files Browse the repository at this point in the history
We get an exception:
    org.mockito.exceptions.base.MockitoException:
    Mockito cannot mock this class: class java.nio.ByteBuffer.

Try to use java.nio APIs to use no resources (sparse, private), not sure
how this works on all platforms..

Signed-off-by: HARPER Jon <jon.harper87@gmail.com>
  • Loading branch information
jonenst committed Feb 28, 2023
1 parent a92bf81 commit dd5485c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.timeseries;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

import java.nio.file.StandardOpenOption;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;

import java.util.List;
import java.util.ArrayList;

/**
* @author Jon Harper <jon.harper at rte-france.com>
*/
abstract class AbstractBigBufferTest {

private Path tempdir;
protected List<FileChannel> channels;

protected ByteBuffer testAllocator(int capacity) {
try {
FileChannel channel = FileChannel.open(
tempdir.resolve(Integer.toString(channels.size())),
StandardOpenOption.READ, StandardOpenOption.WRITE,
StandardOpenOption.CREATE_NEW, StandardOpenOption.SPARSE,
StandardOpenOption.DELETE_ON_CLOSE);
channels.add(channel);
ByteBuffer bytebuffer = channel.map(FileChannel.MapMode.PRIVATE, 0, capacity);
return bytebuffer;
} catch (Exception e) {
throw new RuntimeException("error in allocator test", e);
}
}

@BeforeEach
void before() throws Exception {
tempdir = Files.createTempDirectory("powsybltimeseriestest");
channels = new ArrayList<>();
}

@AfterEach
void after() throws Exception {
for (FileChannel channel : channels) {
channel.close();
}
Files.delete(tempdir);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,19 @@
*/
package com.powsybl.timeseries;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

/**
* @author Jon Harper <jon.harper at rte-france.com>
*/
class BigDoubleBufferTest {
class BigDoubleBufferTest extends AbstractBigBufferTest {

private static final int BUFFER_SIZE_DOUBLES = 1 << 27;

private int allocatorCount;

private ByteBuffer testDoubleAllocator(int capacity) {
allocatorCount++;
ByteBuffer mockbyte = Mockito.mock(ByteBuffer.class);
DoubleBuffer mockdouble = Mockito.mock(DoubleBuffer.class);
when(mockbyte.asDoubleBuffer()).thenReturn(mockdouble);
Map<Integer, Double> map = new HashMap<>();
when(mockdouble.put(anyInt(), anyDouble())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
DoubleBuffer mock = (DoubleBuffer) invocation.getMock();
map.put((int) args[0], (double) args[1]);
return mock;
});
when(mockdouble.get(anyInt())).thenAnswer(invocation -> {
return map.get(invocation.getArguments()[0]);
});
return mockbyte;
}

@BeforeEach
void before() {
allocatorCount = 0;
}

private void bufferTester(long size) {
BigDoubleBuffer buffer = new BigDoubleBuffer(this::testDoubleAllocator, size);
BigDoubleBuffer buffer = new BigDoubleBuffer(this::testAllocator, size);
//Simple writes at the begining
for (int i = 0; i < 10; i++) {
buffer.put(i, i);
Expand Down Expand Up @@ -83,36 +50,36 @@ private void bufferTester(long size) {
@Test
void testSimple() {
bufferTester(10);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testMultipleBuffers() {
bufferTester(200000000);
assertEquals(2, allocatorCount);
assertEquals(2, channels.size());
}

@Test
void testHuge() {
bufferTester(10000000000L);
assertEquals(75, allocatorCount);
assertEquals(75, channels.size());
}

@Test
void testSizeBufferMinus1() {
bufferTester(BUFFER_SIZE_DOUBLES - 1);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testSizeBufferExact() {
bufferTester(BUFFER_SIZE_DOUBLES);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testSizeBufferPlus1() {
bufferTester(BUFFER_SIZE_DOUBLES + 1);
assertEquals(2, allocatorCount);
assertEquals(2, channels.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,19 @@
*/
package com.powsybl.timeseries;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.when;

/**
* @author Jon Harper <jon.harper at rte-france.com>
*/
class BigStringBufferTest {
class BigStringBufferTest extends AbstractBigBufferTest {

private static final int BUFFER_SIZE_INTS = 1 << 28;

private int allocatorCount;

private ByteBuffer testStringAllocator(int capacity) {
allocatorCount++;
ByteBuffer mockbyte = Mockito.mock(ByteBuffer.class);
IntBuffer mockint = Mockito.mock(IntBuffer.class);
when(mockbyte.asIntBuffer()).thenReturn(mockint);
Map<Integer, Integer> map = new HashMap<>();
when(mockint.put(anyInt(), anyInt())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
IntBuffer mock = (IntBuffer) invocation.getMock();
map.put((int) args[0], (int) args[1]);
return mock;
});
when(mockint.get(anyInt())).thenAnswer(invocation -> {
return map.get(invocation.getArguments()[0]);
});
return mockbyte;
}

@BeforeEach
void before() {
allocatorCount = 0;
}

private void bufferTester(long size) {
BigStringBuffer buffer = new BigStringBuffer(this::testStringAllocator, size);
BigStringBuffer buffer = new BigStringBuffer(this::testAllocator, size);
//Simple writes at the begining
for (int i = 0; i < 10; i++) {
buffer.putString(i, Integer.toString(i));
Expand Down Expand Up @@ -84,36 +50,36 @@ private void bufferTester(long size) {
@Test
void testSimple() {
bufferTester(10);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testMultipleBuffers() {
bufferTester(400000000);
assertEquals(2, allocatorCount);
assertEquals(2, channels.size());
}

@Test
void testHuge() {
bufferTester(10000000000L);
assertEquals(38, allocatorCount);
assertEquals(38, channels.size());
}

@Test
void testSizeBufferMinus1() {
bufferTester(BUFFER_SIZE_INTS - 1);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testSizeBufferExact() {
bufferTester(BUFFER_SIZE_INTS);
assertEquals(1, allocatorCount);
assertEquals(1, channels.size());
}

@Test
void testSizeBufferPlus1() {
bufferTester(BUFFER_SIZE_INTS + 1);
assertEquals(2, allocatorCount);
assertEquals(2, channels.size());
}
}

0 comments on commit dd5485c

Please sign in to comment.