This project has retired. For details please refer to its Attic page.
KryoWritableWrapperTest xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.writable.kryo;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Random;
30  
31  import com.google.common.collect.ImmutableBiMap;
32  import com.google.common.collect.ImmutableMap;
33  import org.apache.giraph.conf.GiraphConfiguration;
34  import org.apache.giraph.conf.GiraphConfigurationSettable;
35  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
36  import org.apache.giraph.types.ops.collections.array.WLongArrayList;
37  import org.apache.giraph.utils.WritableUtils;
38  import org.apache.giraph.writable.kryo.markers.NonKryoWritable;
39  import org.apache.hadoop.conf.Configuration;
40  import org.apache.hadoop.io.IntWritable;
41  import org.junit.Assert;
42  import org.junit.Ignore;
43  import org.junit.Test;
44  
45  import com.google.common.collect.ImmutableList;
46  import com.google.common.collect.Iterables;
47  import com.google.common.collect.Iterators;
48  
49  import it.unimi.dsi.fastutil.chars.Char2ObjectMap;
50  import it.unimi.dsi.fastutil.chars.Char2ObjectOpenHashMap;
51  import it.unimi.dsi.fastutil.floats.FloatArrayList;
52  import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
53  import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
54  import it.unimi.dsi.fastutil.longs.LongArrayList;
55  import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
56  
57  
58  
59  /**
60   * Tests some subtle cases of kryo serialization.
61   */
62  public class KryoWritableWrapperTest {
63    public static <T> T kryoSerDeser(T t) throws IOException {
64      KryoWritableWrapper<T> wrapped = new KryoWritableWrapper<>(t);
65      KryoWritableWrapper<T> deser = new KryoWritableWrapper<>();
66      WritableUtils.copyInto(wrapped, deser, true);
67      return deser.get();
68    }
69  
70    @Test
71    public void testArraysAsList() throws IOException {
72      List res = kryoSerDeser(Arrays.asList(1, 2, 3));
73  
74      assertEquals(3, res.size());
75      assertEquals(1, res.get(0));
76      assertEquals(2, res.get(1));
77      assertEquals(3, res.get(2));
78    }
79  
80  
81    @Test
82    public void testArraysAsListMultiRef() throws IOException {
83      List list = Arrays.asList(1, 2, 3);
84      Object obj = new Object();
85      List wanted = Arrays.asList(list, list, obj, obj, null);
86      wanted.set(4, wanted);
87      List res = kryoSerDeser(wanted);
88  
89      assertTrue(res.get(0) == res.get(1));
90      assertTrue(res.get(2) == res.get(3));
91      // TODO see if this can be supported, though this is a rare case:
92      // assertTrue(res == res.get(4));
93    }
94  
95    @Test
96    public void testCollectionsNCopiesList() throws IOException {
97      List res = kryoSerDeser(Collections.nCopies(3, 42));
98  
99      assertEquals(3, res.size());
100     assertEquals(42, res.get(0));
101     assertEquals(42, res.get(1));
102     assertEquals(42, res.get(2));
103   }
104 
105   @Test
106   public void testCollectionsNCopiesObjectList() throws IOException {
107     String testObject = "Hello World!";
108     List<String> res = kryoSerDeser(Collections.nCopies(3, testObject));
109 
110     assertEquals(3, res.size());
111     assertEquals(testObject, res.get(0));
112     assertEquals(testObject, res.get(1));
113     assertEquals(testObject, res.get(2));
114   }
115 
116   @Test
117   public void testUnmodifiableIterator() throws IOException {
118     Iterator<Integer> in = Iterables.concat(
119         Arrays.asList(0, 1),
120         Arrays.asList(2, 3),
121         Arrays.asList(4, 5)).iterator();
122 
123     in.next();
124     in.next();
125     in.next();
126     Iterator res = kryoSerDeser(in);
127 
128     int cnt = 3;
129     for(; res.hasNext(); cnt++) {
130       assertEquals(cnt, res.next());
131     }
132     assertEquals(6, cnt);
133   }
134 
135   @Test
136   public void testIteratorsConcat() throws IOException {
137     Iterator<Integer> in = Iterators.concat(
138         Arrays.asList(0, 1).iterator(),
139         Arrays.asList(2, 3).iterator(),
140         Arrays.asList(4, 5).iterator());
141 
142     in.next();
143     in.next();
144     in.next();
145 
146     Iterator res = kryoSerDeser(in);
147 
148     int cnt = 3;
149     for(; res.hasNext(); cnt++) {
150       assertEquals(cnt, res.next());
151     }
152     assertEquals(6, cnt);
153 
154   }
155 
156   @Test
157   public void testImmutableList() throws IOException {
158     {
159       List res = kryoSerDeser(ImmutableList.of(1, 2));
160       assertEquals(2, res.size());
161       assertEquals(1, res.get(0));
162       assertEquals(2, res.get(1));
163     }
164 
165     {
166       List list = ImmutableList.of(1, 2, 3);
167       Object obj = new Object();
168       List wanted = ImmutableList.of(list, list, obj, obj);
169       List res = kryoSerDeser(wanted);
170 
171       assertTrue(res.get(0) == res.get(1));
172       assertTrue(res.get(2) == res.get(3));
173     }
174   }
175 
176   @Test
177   public void testImmutableMapSerialization() throws IOException {
178     Map original = ImmutableMap.of("x", "y", "y", "z");
179     Map copy = kryoSerDeser(original);
180     assertEquals(original, copy);
181   }
182 
183   @Test
184   public void testImmutableMapSinglePairSerialization() throws IOException {
185     Map original = ImmutableMap.of("x", "y");
186     Map copy = kryoSerDeser(original);
187     assertEquals(original, copy);
188   }
189 
190   @Test
191   public void testImmutableBiMap() throws IOException {
192     Map original = ImmutableBiMap.of("x", "y", "z", "w");
193     Map copy = kryoSerDeser(original);
194     assertEquals(original, copy);
195   }
196 
197   @Test
198   public void testSingletonImmutableBiMapSerialization() throws IOException {
199     Map original = ImmutableBiMap.of("x", "y");
200     Map copy = kryoSerDeser(original);
201     assertEquals(original, copy);
202   }
203 
204   @Test
205   public void testEmptyImmutableBiMap() throws IOException {
206     Map original = ImmutableBiMap.of();
207     Map copy = kryoSerDeser(original);
208     assertEquals(original, copy);
209   }
210 
211   @Test
212   public void testFastutilSet() throws ClassNotFoundException, IOException {
213     LongOpenHashSet set = new LongOpenHashSet();
214     set.add(6);
215     LongOpenHashSet deser = kryoSerDeser(set);
216     deser.add(5);
217     set.add(5);
218     Assert.assertEquals(set, deser);
219   }
220 
221   @Test
222   public void testFastutilLongList() throws ClassNotFoundException, IOException {
223     LongArrayList list = new LongArrayList();
224     list.add(6);
225     LongArrayList deser = kryoSerDeser(list);
226     deser.add(5);
227     list.add(5);
228     Assert.assertEquals(list, deser);
229   }
230 
231   @Test
232   public void testWFastutilLongList() throws ClassNotFoundException, IOException {
233     WLongArrayList list = new WLongArrayList();
234     list.add(6);
235     WLongArrayList deser = kryoSerDeser(list);
236     deser.add(5);
237     list.add(5);
238     Assert.assertEquals(list, deser);
239   }
240 
241 
242   @Test
243   public void testFastutilFloatList() throws ClassNotFoundException, IOException {
244     FloatArrayList list = new FloatArrayList();
245     list.add(6L);
246     FloatArrayList deser = kryoSerDeser(list);
247     deser.add(5L);
248     list.add(5L);
249     Assert.assertEquals(list, deser);
250   }
251 
252   @Test
253   public void testFastutilMap() throws ClassNotFoundException, IOException {
254     Int2BooleanMap list = new Int2BooleanOpenHashMap();
255     list.put(6, true);
256     Int2BooleanMap deser = kryoSerDeser(list);
257     deser.put(5, false);
258     list.put(5, false);
259     Assert.assertEquals(list, deser);
260   }
261 
262   @Test
263   public void testFastutil2ObjMap() throws ClassNotFoundException, IOException {
264     Char2ObjectMap<IntWritable> list = new Char2ObjectOpenHashMap<>();
265     list.put('a', new IntWritable(6));
266     list.put('q', new IntWritable(7));
267     list.put('w', new IntWritable(8));
268     list.put('e', new IntWritable(9));
269     list.put('r', new IntWritable(7));
270     list.put('c', null);
271     Char2ObjectMap<IntWritable> deser = kryoSerDeser(list);
272     deser.put('b', null);
273     list.put('b', null);
274 
275     Assert.assertEquals(list, deser);
276   }
277 
278   @Test
279   @Ignore("Long test used for profiling compiling speed")
280   public void testLongFastutilListProfile() throws ClassNotFoundException, IOException {
281     int n = 100;
282     int rounds = 2000000;
283     LongArrayList list = new LongArrayList(n);
284     for (int i = 0; i < n; i++) {
285       list.add(i);
286     }
287 
288     for (int round = 0; round < rounds; round ++) {
289       LongArrayList deser = kryoSerDeser(list);
290       deser.add(round);
291       list.add(round);
292       Assert.assertEquals(list.size(), deser.size());
293       Assert.assertArrayEquals(list.elements(), deser.elements());
294 
295       list.popLong();
296     }
297   }
298 
299   @Test(expected=RuntimeException.class)
300   public void testRandom() throws ClassNotFoundException, IOException {
301     kryoSerDeser(new Random()).nextBoolean();
302   }
303 
304   private static class TestConf implements GiraphConfigurationSettable {
305     @Override
306     public void setConf(ImmutableClassesGiraphConfiguration configuration) {
307     }
308   }
309 
310   @Test(expected=RuntimeException.class)
311   public void testConfiguration() throws ClassNotFoundException, IOException {
312     kryoSerDeser(new Configuration());
313   }
314 
315   @Test(expected=RuntimeException.class)
316   public void testConfigurable() throws ClassNotFoundException, IOException {
317     kryoSerDeser(new TestConf());
318   }
319 
320   @Test(expected=RuntimeException.class)
321   public void testVertexReceiver() throws ClassNotFoundException, IOException {
322     kryoSerDeser(new NonKryoWritable() {
323     });
324   }
325 
326 
327   @Test
328   public void testBlacklistedClasses() throws ClassNotFoundException, IOException {
329     Assert.assertEquals(kryoSerDeser(Random.class), Random.class);
330     Assert.assertEquals(kryoSerDeser(TestConf.class), TestConf.class);
331     Assert.assertEquals(kryoSerDeser(GiraphConfiguration.class), GiraphConfiguration.class);
332   }
333 
334   @Test(expected=RuntimeException.class)
335   public void testRecursive() throws ClassNotFoundException, IOException {
336     kryoSerDeser(new KryoWritableWrapper<>(new Object())).get().hashCode();
337   }
338 
339   @Test
340   public void testNull() throws ClassNotFoundException, IOException {
341     Assert.assertNull(kryoSerDeser(null));
342   }
343 }