Eliminate atomix.utils.memory
[controller.git] / third-party / atomix / utils / src / test / java / io / atomix / utils / serializer / KryoOutputPoolTest.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
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 com.esotericsoftware.kryo.io.Output;
19 import org.junit.Before;
20 import org.junit.Test;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 public class KryoOutputPoolTest {
26
27   private KryoOutputPool kryoOutputPool;
28
29   @Before
30   public void setUp() throws Exception {
31     kryoOutputPool = new KryoOutputPool();
32   }
33
34   @Test
35   public void discardOutput() {
36     final Output[] result = new Output[2];
37     kryoOutputPool.run(output -> {
38       result[0] = output;
39       return null;
40     }, KryoOutputPool.MAX_POOLED_BUFFER_SIZE + 1);
41     kryoOutputPool.run(output -> {
42       result[1] = output;
43       return null;
44     }, 0);
45     assertTrue(result[0] != result[1]);
46   }
47
48   @Test
49   public void recycleOutput() {
50     final ByteArrayOutput[] result = new ByteArrayOutput[2];
51     kryoOutputPool.run(output -> {
52       output.writeInt(1);
53       assertEquals(Integer.BYTES, output.position());
54       result[0] = output;
55       return null;
56     }, 0);
57     assertEquals(0, result[0].position());
58     assertEquals(0, result[0].getByteArrayOutputStream().size());
59     kryoOutputPool.run(output -> {
60       assertEquals(0, output.position());
61       result[1] = output;
62       return null;
63     }, 0);
64     assertTrue(result[0] == result[1]);
65   }
66 }