Improve segmented journal actor metrics
[controller.git] / atomix-storage / src / test / java / io / atomix / storage / journal / JournalSegmentDescriptorTest.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.storage.journal;
17
18 import org.junit.Test;
19
20 import java.nio.ByteBuffer;
21
22 import static org.junit.Assert.assertEquals;
23
24 /**
25  * Segment descriptor test.
26  *
27  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
28  */
29 public class JournalSegmentDescriptorTest {
30
31   /**
32    * Tests the segment descriptor builder.
33    */
34   @Test
35   public void testDescriptorBuilder() {
36     JournalSegmentDescriptor descriptor = JournalSegmentDescriptor.builder(ByteBuffer.allocate(JournalSegmentDescriptor.BYTES))
37         .withId(2)
38         .withIndex(1025)
39         .withMaxSegmentSize(1024 * 1024)
40         .withMaxEntries(2048)
41         .build();
42
43     assertEquals(2, descriptor.id());
44     assertEquals(JournalSegmentDescriptor.VERSION, descriptor.version());
45     assertEquals(1025, descriptor.index());
46     assertEquals(1024 * 1024, descriptor.maxSegmentSize());
47     assertEquals(2048, descriptor.maxEntries());
48
49     assertEquals(0, descriptor.updated());
50     long time = System.currentTimeMillis();
51     descriptor.update(time);
52     assertEquals(time, descriptor.updated());
53   }
54
55   /**
56    * Tests copying the segment descriptor.
57    */
58   @Test
59   public void testDescriptorCopy() {
60     JournalSegmentDescriptor descriptor = JournalSegmentDescriptor.builder()
61         .withId(2)
62         .withIndex(1025)
63         .withMaxSegmentSize(1024 * 1024)
64         .withMaxEntries(2048)
65         .build();
66
67     long time = System.currentTimeMillis();
68     descriptor.update(time);
69
70     descriptor = descriptor.copyTo(ByteBuffer.allocate(JournalSegmentDescriptor.BYTES));
71
72     assertEquals(2, descriptor.id());
73     assertEquals(JournalSegmentDescriptor.VERSION, descriptor.version());
74     assertEquals(1025, descriptor.index());
75     assertEquals(1024 * 1024, descriptor.maxSegmentSize());
76     assertEquals(2048, descriptor.maxEntries());
77     assertEquals(time, descriptor.updated());
78   }
79 }