I have an application which from time to time has this stacktrace in logs:
java.lang.ArrayIndexOutOfBoundsException: 514
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:436)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2081)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
at java.util.Calendar.complete(Calendar.java:1312)
at java.util.Calendar.get(Calendar.java:1093)
at java.text.SimpleDateFormat.subFormat(SimpleDateFormat.java:917)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:824)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:796)
at java.text.DateFormat.format(DateFormat.java:314)
at me.myself.i.Message.toString(Message.java:203)
at java.lang.String.valueOf(String.java:2615)
at java.lang.StringBuilder.append(StringBuilder.java:116)
I think the problem might be somewhere in those lines:
public class Message{
private transient DateFormat logDateFormat;
@Override
public String toString() {
final StringBuilder result = new StringBuilder(getClass().getSimpleName());
result.append("Time=").append(logDateFormat.format(new Date(getExpireTime())));
return result.toString();
}
}
I think multiple threads call toString() at the same time, but i have a trouble reproducing this on my local machine:
@Before
public void setUp() {
message = new Message();
pool = Executors.newFixedThreadPool(numOfThreads);
}
@Test
public void multiThreadTest() {
for (int i=0; i<numOfThreads; i++) {
TestJob j = new TestJob(message);
pool.submit(j);
}
pool.shutdown();
while(!pool.isTerminated()){
}
}
class TestJob implements Runnable{
private Message message;
private int n=100;
public TestJob(Message message) {
this.message= message;
}
public void run() {
for (int i=0; i<n; i++) {
try{
System.out.println(message.toString());
} catch(Exception e){
e.printStackTrace();
}
}
}
}
How do i write correct junit test to reproduce this issue?
Since my first test did not reproduce your problem, try this one
it took time but finally I got