Skip to content

Commit

Permalink
reproduce exception with PriorityQueue with lambda expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiayi-Liao committed Apr 27, 2020
1 parent 396eacf commit 3f1f826
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ val sharedSettings = mimaDefaultSettings ++ Seq(
scalariformPreferences := formattingPreferences,
scalariformAutoformat := false,
// Twitter Hadoop needs this, sorry 1.7 fans
javacOptions ++= Seq("-target", "1.6", "-source", "1.6", "-Xlint:-options"),
javacOptions in doc := Seq("-source", "1.6"),
javacOptions ++= Seq("-target", "1.8", "-source", "1.8", "-Xlint:-options"),
javacOptions in doc := Seq("-source", "1.8"),
resolvers ++= Seq(
Opts.resolver.sonatypeSnapshots,
Opts.resolver.sonatypeReleases
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.twitter.chill.java;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.util.Comparator;
import java.util.PriorityQueue;

public class TestPriorityQueue {

private static final Kryo kryo = new Kryo();

private static final PriorityQueue<String> test_priority_queue1 =
new PriorityQueue<>((o1, o2) -> o1.length() - o2.length() - 1);

private static final PriorityQueue<String> test_priority_queue2 =
new PriorityQueue<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length() - 1;
}
});

static {
test_priority_queue1.add("12345");
test_priority_queue1.add("123");
test_priority_queue1.add("12456789");

test_priority_queue2.add("12345");
test_priority_queue2.add("123");
test_priority_queue2.add("12456789");
}

public static PriorityQueue<String> serializeAndDeserializeQueue1() {
Output output = new Output(1000, -1);
kryo.writeClassAndObject(output, test_priority_queue1);
Input input = new Input(output.toBytes());
return (PriorityQueue<String>) kryo.readClassAndObject(input);
}

public static PriorityQueue<String> serializeAndDeserializeQueue1(Kryo kryo) {
Output output = new Output(1000, -1);
kryo.writeClassAndObject(output, test_priority_queue1);
Input input = new Input(output.toBytes());
return (PriorityQueue<String>) kryo.readClassAndObject(input);
}

public static PriorityQueue<String> serializeAndDeserializeQueue2(Kryo kryo) {
Output output = new Output(1000, -1);
kryo.writeClassAndObject(output, test_priority_queue2);
Input input = new Input(output.toBytes());
return (PriorityQueue<String>) kryo.readClassAndObject(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ limitations under the License.

package com.twitter.chill.java

import org.scalatest._

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output

import com.esotericsoftware.kryo.io.{Input, Output}
import org.objenesis.strategy.StdInstantiatorStrategy
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
Expand All @@ -34,6 +30,24 @@ class PriorityQueueSpec extends AnyWordSpec with Matchers {
k.readClassAndObject(in).asInstanceOf[A]
}

"A PriorityQueue Serializer" should {
"serialize PriorityQueue with lambda expression" in {

val kryo = new Kryo()
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy)
PriorityQueueSerializer.registrar()(kryo)

// this works well without registered PriorityQueueSerializer
TestPriorityQueue.serializeAndDeserializeQueue1()

// this works well with registered PriorityQueueSerializer and PriorityQueue instance (with non-lambda expression)
TestPriorityQueue.serializeAndDeserializeQueue2(kryo)

// this should work well but failed with PriorityQueueSerializer registered in kryo and PriorityQueue instance (with lambda expression)
TestPriorityQueue.serializeAndDeserializeQueue1(kryo)
}
}

"A PriorityQueue Serializer" should {
"handle PriorityQueue" in {
import scala.collection.JavaConverters._
Expand Down

0 comments on commit 3f1f826

Please sign in to comment.