Skip to content

Commit a8ecd9b

Browse files
committed
WW-4900 Fixes BackgroundProcessTest via synchronization
1 parent 6b13155 commit a8ecd9b

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

core/src/test/java/org/apache/struts2/interceptor/BackgroundProcessTest.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.struts2.interceptor;
2020

21+
import com.mockobjects.servlet.MockHttpServletRequest;
2122
import com.opensymphony.xwork2.ActionContext;
2223
import com.opensymphony.xwork2.mock.MockActionInvocation;
2324
import org.apache.struts2.StrutsInternalTestCase;
@@ -26,28 +27,45 @@
2627
import java.io.ByteArrayOutputStream;
2728
import java.io.ObjectInputStream;
2829
import java.io.ObjectOutputStream;
30+
import java.util.concurrent.Callable;
31+
import java.util.concurrent.Semaphore;
32+
import java.util.concurrent.TimeUnit;
2933

3034
/**
3135
* Test case for BackgroundProcessTest.
3236
*/
3337
public class BackgroundProcessTest extends StrutsInternalTestCase {
3438

3539
public void testSerializeDeserialize() throws Exception {
36-
MockActionInvocation invocation = new MockActionInvocation();
37-
invocation.setResultCode("BackgroundProcessTest.testSerializeDeserialize");
40+
final NotSerializableException expectedException = new NotSerializableException(new MockHttpServletRequest());
41+
final Semaphore lock = new Semaphore(1);
42+
lock.acquire();
43+
MockActionInvocationWithActionInvoker invocation = new MockActionInvocationWithActionInvoker(new Callable<String>() {
44+
@Override
45+
public String call() throws Exception {
46+
lock.release();
47+
throw expectedException;
48+
}
49+
});
3850
invocation.setInvocationContext(ActionContext.getContext());
3951

4052
BackgroundProcess bp = new BackgroundProcess("BackgroundProcessTest.testSerializeDeserialize", invocation
4153
, Thread.MIN_PRIORITY);
54+
if(!lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
55+
lock.release();
56+
fail("background thread did not release lock on timeout");
57+
}
58+
lock.release();
4259

43-
bp.exception = new Exception();
60+
bp.result = "BackgroundProcessTest.testSerializeDeserialize";
4461
bp.done = true;
62+
Thread.sleep(1000);//give a chance to background thread to set exception
63+
assertEquals(expectedException, bp.exception);
4564

4665
ByteArrayOutputStream baos = new ByteArrayOutputStream();
4766
ObjectOutputStream oos = new ObjectOutputStream(baos);
4867
oos.writeObject(bp);
4968
oos.close();
50-
assertTrue("should have serialized data", baos.size() > 0);
5169
byte b[] = baos.toByteArray();
5270
baos.close();
5371

@@ -62,4 +80,25 @@ public void testSerializeDeserialize() throws Exception {
6280
assertEquals(bp.result, deserializedBp.result);
6381
assertEquals(bp.done, deserializedBp.done);
6482
}
83+
84+
85+
private class MockActionInvocationWithActionInvoker extends MockActionInvocation {
86+
private Callable<String> actionInvoker;
87+
88+
MockActionInvocationWithActionInvoker(Callable<String> actionInvoker){
89+
this.actionInvoker = actionInvoker;
90+
}
91+
92+
@Override
93+
public String invokeActionOnly() throws Exception {
94+
return actionInvoker.call();
95+
}
96+
}
97+
98+
private class NotSerializableException extends Exception {
99+
private MockHttpServletRequest notSerializableField;
100+
NotSerializableException(MockHttpServletRequest notSerializableField) {
101+
this.notSerializableField = notSerializableField;
102+
}
103+
}
65104
}

0 commit comments

Comments
 (0)