KryoSerializerTest to test KryoSerializer instead of KryoPool
Change-Id: If92e12be730ed2553686f4a3927ddd49f226cf20
Showing
1 changed file
with
93 additions
and
21 deletions
... | @@ -20,6 +20,7 @@ import org.onlab.onos.net.DefaultLink; | ... | @@ -20,6 +20,7 @@ import org.onlab.onos.net.DefaultLink; |
20 | import org.onlab.onos.net.DefaultPort; | 20 | import org.onlab.onos.net.DefaultPort; |
21 | import org.onlab.onos.net.Device; | 21 | import org.onlab.onos.net.Device; |
22 | import org.onlab.onos.net.DeviceId; | 22 | import org.onlab.onos.net.DeviceId; |
23 | +import org.onlab.onos.net.HostLocation; | ||
23 | import org.onlab.onos.net.Link; | 24 | import org.onlab.onos.net.Link; |
24 | import org.onlab.onos.net.LinkKey; | 25 | import org.onlab.onos.net.LinkKey; |
25 | import org.onlab.onos.net.PortNumber; | 26 | import org.onlab.onos.net.PortNumber; |
... | @@ -28,8 +29,10 @@ import org.onlab.onos.net.provider.ProviderId; | ... | @@ -28,8 +29,10 @@ import org.onlab.onos.net.provider.ProviderId; |
28 | import org.onlab.packet.ChassisId; | 29 | import org.onlab.packet.ChassisId; |
29 | import org.onlab.packet.IpAddress; | 30 | import org.onlab.packet.IpAddress; |
30 | import org.onlab.packet.IpPrefix; | 31 | import org.onlab.packet.IpPrefix; |
32 | +import org.onlab.packet.MacAddress; | ||
31 | import org.onlab.util.KryoPool; | 33 | import org.onlab.util.KryoPool; |
32 | 34 | ||
35 | +import com.google.common.collect.ImmutableList; | ||
33 | import com.google.common.collect.ImmutableMap; | 36 | import com.google.common.collect.ImmutableMap; |
34 | import com.google.common.collect.ImmutableSet; | 37 | import com.google.common.collect.ImmutableSet; |
35 | import com.google.common.testing.EqualsTester; | 38 | import com.google.common.testing.EqualsTester; |
... | @@ -61,78 +64,147 @@ public class KryoSerializerTest { | ... | @@ -61,78 +64,147 @@ public class KryoSerializerTest { |
61 | .set("B3", "b3") | 64 | .set("B3", "b3") |
62 | .build(); | 65 | .build(); |
63 | 66 | ||
64 | - private static KryoPool kryos; | 67 | + private KryoSerializer serializer; |
65 | 68 | ||
66 | @BeforeClass | 69 | @BeforeClass |
67 | public static void setUpBeforeClass() throws Exception { | 70 | public static void setUpBeforeClass() throws Exception { |
68 | - kryos = KryoPool.newBuilder() | ||
69 | - .register(KryoPoolUtil.API) | ||
70 | - .register(ImmutableMap.class, new ImmutableMapSerializer()) | ||
71 | - .register(ImmutableSet.class, new ImmutableSetSerializer()) | ||
72 | - .build(); | ||
73 | } | 71 | } |
74 | 72 | ||
75 | @Before | 73 | @Before |
76 | public void setUp() throws Exception { | 74 | public void setUp() throws Exception { |
75 | + serializer = new KryoSerializer() { | ||
76 | + | ||
77 | + @Override | ||
78 | + protected void setupKryoPool() { | ||
79 | + serializerPool = KryoPool.newBuilder() | ||
80 | + .register(KryoPoolUtil.API) | ||
81 | + .build() | ||
82 | + .populate(1); | ||
83 | + } | ||
84 | + }; | ||
77 | } | 85 | } |
78 | 86 | ||
79 | @After | 87 | @After |
80 | public void tearDown() throws Exception { | 88 | public void tearDown() throws Exception { |
81 | - // removing Kryo instance to use fresh Kryo on each tests | ||
82 | - kryos.getKryo(); | ||
83 | } | 89 | } |
84 | 90 | ||
85 | - private static <T> void testSerialized(T original) { | 91 | + private <T> void testSerialized(T original) { |
86 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); | 92 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
87 | - kryos.serialize(original, buffer); | 93 | + serializer.encode(original, buffer); |
88 | buffer.flip(); | 94 | buffer.flip(); |
89 | - T copy = kryos.deserialize(buffer); | 95 | + T copy = serializer.decode(buffer); |
96 | + | ||
97 | + T copy2 = serializer.decode(serializer.encode(original)); | ||
90 | 98 | ||
91 | new EqualsTester() | 99 | new EqualsTester() |
92 | - .addEqualityGroup(original, copy) | 100 | + .addEqualityGroup(original, copy, copy2) |
93 | .testEquals(); | 101 | .testEquals(); |
94 | } | 102 | } |
95 | 103 | ||
96 | 104 | ||
97 | @Test | 105 | @Test |
98 | - public final void testSerialization() { | 106 | + public void testConnectPoint() { |
99 | testSerialized(new ConnectPoint(DID1, P1)); | 107 | testSerialized(new ConnectPoint(DID1, P1)); |
108 | + } | ||
109 | + | ||
110 | + @Test | ||
111 | + public void testDefaultLink() { | ||
100 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); | 112 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); |
101 | - testSerialized(new DefaultPort(DEV1, P1, true)); | ||
102 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); | 113 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); |
114 | + } | ||
115 | + | ||
116 | + @Test | ||
117 | + public void testDefaultPort() { | ||
118 | + testSerialized(new DefaultPort(DEV1, P1, true)); | ||
103 | testSerialized(new DefaultPort(DEV1, P1, true, A1_2)); | 119 | testSerialized(new DefaultPort(DEV1, P1, true, A1_2)); |
120 | + } | ||
121 | + | ||
122 | + @Test | ||
123 | + public void testDeviceId() { | ||
104 | testSerialized(DID1); | 124 | testSerialized(DID1); |
125 | + } | ||
126 | + | ||
127 | + @Test | ||
128 | + public void testImmutableMap() { | ||
105 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); | 129 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); |
106 | testSerialized(ImmutableMap.of(DID1, DEV1)); | 130 | testSerialized(ImmutableMap.of(DID1, DEV1)); |
107 | testSerialized(ImmutableMap.of()); | 131 | testSerialized(ImmutableMap.of()); |
132 | + } | ||
133 | + | ||
134 | + @Test | ||
135 | + public void testImmutableSet() { | ||
108 | testSerialized(ImmutableSet.of(DID1, DID2)); | 136 | testSerialized(ImmutableSet.of(DID1, DID2)); |
109 | testSerialized(ImmutableSet.of(DID1)); | 137 | testSerialized(ImmutableSet.of(DID1)); |
110 | testSerialized(ImmutableSet.of()); | 138 | testSerialized(ImmutableSet.of()); |
139 | + } | ||
140 | + | ||
141 | + @Test | ||
142 | + public void testImmutableList() { | ||
143 | + testSerialized(ImmutableList.of(DID1, DID2)); | ||
144 | + testSerialized(ImmutableList.of(DID1)); | ||
145 | + testSerialized(ImmutableList.of()); | ||
146 | + } | ||
147 | + | ||
148 | + @Test | ||
149 | + public void testIpPrefix() { | ||
111 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); | 150 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); |
151 | + } | ||
152 | + | ||
153 | + @Test | ||
154 | + public void testIpAddress() { | ||
112 | testSerialized(IpAddress.valueOf("192.168.0.1")); | 155 | testSerialized(IpAddress.valueOf("192.168.0.1")); |
156 | + } | ||
157 | + | ||
158 | + @Test | ||
159 | + public void testMacAddress() { | ||
160 | + testSerialized(MacAddress.valueOf("12:34:56:78:90:ab")); | ||
161 | + } | ||
162 | + | ||
163 | + @Test | ||
164 | + public void testLinkKey() { | ||
113 | testSerialized(LinkKey.linkKey(CP1, CP2)); | 165 | testSerialized(LinkKey.linkKey(CP1, CP2)); |
166 | + } | ||
167 | + | ||
168 | + @Test | ||
169 | + public void testNodeId() { | ||
114 | testSerialized(new NodeId("SomeNodeIdentifier")); | 170 | testSerialized(new NodeId("SomeNodeIdentifier")); |
171 | + } | ||
172 | + | ||
173 | + @Test | ||
174 | + public void testPortNumber() { | ||
115 | testSerialized(P1); | 175 | testSerialized(P1); |
176 | + } | ||
177 | + | ||
178 | + @Test | ||
179 | + public void testProviderId() { | ||
116 | testSerialized(PID); | 180 | testSerialized(PID); |
117 | testSerialized(PIDA); | 181 | testSerialized(PIDA); |
118 | - testSerialized(new NodeId("bar")); | 182 | + } |
183 | + | ||
184 | + @Test | ||
185 | + public void testMastershipTerm() { | ||
119 | testSerialized(MastershipTerm.of(new NodeId("foo"), 2)); | 186 | testSerialized(MastershipTerm.of(new NodeId("foo"), 2)); |
120 | } | 187 | } |
121 | 188 | ||
122 | @Test | 189 | @Test |
123 | - public final void testAnnotations() { | 190 | + public void testHostLocation() { |
191 | + testSerialized(new HostLocation(CP1, 1234L)); | ||
192 | + } | ||
193 | + | ||
194 | + @Test | ||
195 | + public void testAnnotations() { | ||
124 | // Annotations does not have equals defined, manually test equality | 196 | // Annotations does not have equals defined, manually test equality |
125 | - final byte[] a1Bytes = kryos.serialize(A1); | 197 | + final byte[] a1Bytes = serializer.encode(A1); |
126 | - SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes); | 198 | + SparseAnnotations copiedA1 = serializer.decode(a1Bytes); |
127 | assertAnnotationsEquals(copiedA1, A1); | 199 | assertAnnotationsEquals(copiedA1, A1); |
128 | 200 | ||
129 | - final byte[] a12Bytes = kryos.serialize(A1_2); | 201 | + final byte[] a12Bytes = serializer.encode(A1_2); |
130 | - SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes); | 202 | + SparseAnnotations copiedA12 = serializer.decode(a12Bytes); |
131 | assertAnnotationsEquals(copiedA12, A1_2); | 203 | assertAnnotationsEquals(copiedA12, A1_2); |
132 | } | 204 | } |
133 | 205 | ||
134 | // code clone | 206 | // code clone |
135 | - public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { | 207 | + protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
136 | SparseAnnotations expected = DefaultAnnotations.builder().build(); | 208 | SparseAnnotations expected = DefaultAnnotations.builder().build(); |
137 | for (SparseAnnotations a : annotations) { | 209 | for (SparseAnnotations a : annotations) { |
138 | expected = DefaultAnnotations.union(expected, a); | 210 | expected = DefaultAnnotations.union(expected, a); | ... | ... |
-
Please register or login to post a comment