Last active
August 22, 2016 12:51
-
-
Save favila/fbd3348a6666386ee063bf1981ad5541 to your computer and use it in GitHub Desktop.
Investigations into transit bug: https://github.com/cognitect/transit-java/issues/20 http://dev.clojure.org/jira/browse/CLJS-1761 http://dev.clojure.org/jira/browse/CLJS-1759
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mytransit; | |
import com.cognitect.transit.TransitFactory; | |
import com.cognitect.transit.WriteHandler; | |
import com.cognitect.transit.Writer; | |
import java.io.ByteArrayOutputStream; | |
import java.io.OutputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class ParallelStressTest { | |
static Map<Class, WriteHandler<?, ?>> customHandlersSingleton = customHandlers(); | |
static Map<Class, WriteHandler<?, ?>> customHandlers() { | |
HashMap<Class, WriteHandler<?, ?>> h = new HashMap<Class, WriteHandler<?, ?>>(); | |
h.put(String.class, customWriteHandler()); | |
return h; | |
} | |
static WriteHandler customWriteHandler() { | |
return new WriteHandler() { | |
@Override | |
public String tag(Object o) { | |
return "s"; | |
} | |
@Override | |
public Object rep(Object o) { | |
return "OVERRIDDEN"; | |
} | |
@Override | |
public String stringRep(Object o) { | |
return null; | |
} | |
@Override | |
public WriteHandler getVerboseHandler() { | |
return new WriteHandler() { | |
@Override | |
public String tag(Object o) { | |
return "s"; | |
} | |
@Override | |
public Object rep(Object o) { | |
return "OVERRIDDEN"; | |
} | |
@Override | |
public String stringRep(Object o) { | |
return null; | |
} | |
@Override | |
public WriteHandler getVerboseHandler() { | |
return null; | |
} | |
}; | |
} | |
}; | |
} | |
static String write(String o, TransitFactory.Format format, Map<Class, WriteHandler<?, ?>> customHandlers) { | |
try { | |
OutputStream out = new ByteArrayOutputStream(); | |
Writer w = TransitFactory.writer(format, out, customHandlers); | |
w.write(o); | |
return out.toString(); | |
} catch (Throwable e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static void runParallel(Runnable r) { | |
int n = Runtime.getRuntime().availableProcessors() * 2; | |
ThreadGroup tg = new ThreadGroup("runParallel"); | |
for (int i = 0; i < n; i++) { | |
new Thread(tg, r).start(); | |
} | |
} | |
public static void main(String[] args) { | |
runParallel(new WriteTestManyHandlers()); | |
} | |
static class WriteTestOneHandlers implements Runnable { | |
public void run() { | |
while (true) { | |
String result; | |
try { | |
result = write("0", TransitFactory.Format.JSON, customHandlersSingleton); | |
} catch (RuntimeException e) { | |
System.out.println(e); | |
return; | |
} | |
try { | |
assert "\"OVERRIDDEN\"".equals(result); | |
} catch (AssertionError e) { | |
System.out.println(e); | |
return; | |
} | |
} | |
} | |
} | |
static class WriteTestManyHandlers implements Runnable { | |
public void run() { | |
public void run() { | |
while (true) { | |
String result; | |
try { | |
result = write("0", TransitFactory.Format.JSON, customHandlers()); | |
} catch (RuntimeException e) { | |
System.out.println(e); | |
return; | |
} | |
try { | |
assert "\"OVERRIDDEN\"".equals(result); | |
} catch (AssertionError e) { | |
System.out.println(e); | |
return; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extra
run
is inWriteTestManyHandlers
.