Skip to content

Instantly share code, notes, and snippets.

@favila
Last active August 22, 2016 12:51
Show Gist options
  • Save favila/fbd3348a6666386ee063bf1981ad5541 to your computer and use it in GitHub Desktop.
Save favila/fbd3348a6666386ee063bf1981ad5541 to your computer and use it in GitHub Desktop.
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;
}
}
}
}
}
}
@mfikes
Copy link

mfikes commented Aug 22, 2016

Extra run is in WriteTestManyHandlers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment