Bump versions 9.0.4-SNAPSHOT
[controller.git] / atomix-storage / src / test / java / io / atomix / utils / serializer / KryoOutputPoolTest.java
1 /*
2  * Copyright 2017-2022 Open Networking Foundation and others.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils.serializer;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotSame;
20 import static org.junit.jupiter.api.Assertions.assertSame;
21
22 import com.esotericsoftware.kryo.io.Output;
23 import org.junit.jupiter.api.Test;
24
25 class KryoOutputPoolTest {
26     private final KryoOutputPool kryoOutputPool = new KryoOutputPool();
27
28     @Test
29     void discardOutput() {
30         final var result = new Output[2];
31         kryoOutputPool.run(output -> {
32             result[0] = output;
33             return null;
34         }, KryoOutputPool.MAX_POOLED_BUFFER_SIZE + 1);
35         kryoOutputPool.run(output -> {
36             result[1] = output;
37             return null;
38         }, 0);
39         assertNotSame(result[0], result[1]);
40     }
41
42     @Test
43     void recycleOutput() {
44         final var result = new ByteArrayOutput[2];
45         kryoOutputPool.run(output -> {
46             output.writeInt(1);
47             assertEquals(Integer.BYTES, output.position());
48             result[0] = output;
49             return null;
50         }, 0);
51         assertEquals(0, result[0].position());
52         assertEquals(0, result[0].getByteArrayOutputStream().size());
53         kryoOutputPool.run(output -> {
54             assertEquals(0, output.position());
55             result[1] = output;
56             return null;
57         }, 0);
58         assertSame(result[0], result[1]);
59     }
60 }