3333import org .apache .log4j .Level ;
3434import org .apache .log4j .Logger ;
3535import org .apache .log4j .spi .LoggingEvent ;
36- import org .junit .BeforeClass ;
3736import org .junit .Test ;
3837import org .mockito .invocation .InvocationOnMock ;
3938import org .mockito .stubbing .Answer ;
@@ -48,36 +47,30 @@ public class AppendOutputRunnerTest {
4847 */
4948 private volatile static int numInvocations = 0 ;
5049
51- @ BeforeClass
52- public static void beforeClass () {
53- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
54- AppendOutputRunner .emptyQueueForUnitTests ();
55- }
56-
5750 @ Test
5851 public void testSingleEvent () throws InterruptedException {
5952 RemoteInterpreterProcessListener listener = mock (RemoteInterpreterProcessListener .class );
60- AppendOutputRunner . appendBuffer ( "note" , "para" , "data\n " ) ;
53+ String [][] buffer = {{ "note" , "para" , "data\n " }} ;
6154
62- loopForCompletingEvents (listener , 1 );
55+ loopForCompletingEvents (listener , 1 , buffer );
6356 verify (listener , times (1 )).onOutputAppend (any (String .class ), any (String .class ), any (String .class ));
6457 verify (listener , times (1 )).onOutputAppend ("note" , "para" , "data\n " );
65- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
6658 }
6759
6860 @ Test
6961 public void testMultipleEventsOfSameParagraph () throws InterruptedException {
7062 RemoteInterpreterProcessListener listener = mock (RemoteInterpreterProcessListener .class );
7163 String note1 = "note1" ;
7264 String para1 = "para1" ;
73- AppendOutputRunner .appendBuffer (note1 , para1 , "data1\n " );
74- AppendOutputRunner .appendBuffer (note1 , para1 , "data2\n " );
75- AppendOutputRunner .appendBuffer (note1 , para1 , "data3\n " );
65+ String [][] buffer = {
66+ {note1 , para1 , "data1\n " },
67+ {note1 , para1 , "data2\n " },
68+ {note1 , para1 , "data3\n " }
69+ };
7670
77- loopForCompletingEvents (listener , 1 );
71+ loopForCompletingEvents (listener , 1 , buffer );
7872 verify (listener , times (1 )).onOutputAppend (any (String .class ), any (String .class ), any (String .class ));
7973 verify (listener , times (1 )).onOutputAppend (note1 , para1 , "data1\n data2\n data3\n " );
80- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
8174 }
8275
8376 @ Test
@@ -87,26 +80,27 @@ public void testMultipleEventsOfDifferentParagraphs() throws InterruptedExceptio
8780 String note2 = "note2" ;
8881 String para1 = "para1" ;
8982 String para2 = "para2" ;
90- AppendOutputRunner .appendBuffer (note1 , para1 , "data1\n " );
91- AppendOutputRunner .appendBuffer (note1 , para2 , "data2\n " );
92- AppendOutputRunner .appendBuffer (note2 , para1 , "data3\n " );
93- AppendOutputRunner .appendBuffer (note2 , para2 , "data4\n " );
94- loopForCompletingEvents (listener , 4 );
83+ String [][] buffer = {
84+ {note1 , para1 , "data1\n " },
85+ {note1 , para2 , "data2\n " },
86+ {note2 , para1 , "data3\n " },
87+ {note2 , para2 , "data4\n " }
88+ };
89+ loopForCompletingEvents (listener , 4 , buffer );
9590
9691 verify (listener , times (4 )).onOutputAppend (any (String .class ), any (String .class ), any (String .class ));
9792 verify (listener , times (1 )).onOutputAppend (note1 , para1 , "data1\n " );
9893 verify (listener , times (1 )).onOutputAppend (note1 , para2 , "data2\n " );
9994 verify (listener , times (1 )).onOutputAppend (note2 , para1 , "data3\n " );
10095 verify (listener , times (1 )).onOutputAppend (note2 , para2 , "data4\n " );
101- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
10296 }
10397
10498 @ Test
10599 public void testClubbedData () throws InterruptedException {
106100 RemoteInterpreterProcessListener listener = mock (RemoteInterpreterProcessListener .class );
107- AppendOutputRunner . setListener (listener );
108- CheckAppendOutputRunner .startScheduler ();
109- Thread thread = new Thread (new BombardEvents ());
101+ AppendOutputRunner runner = new AppendOutputRunner (listener );
102+ CheckAppendOutputRunner .startScheduler (listener , runner );
103+ Thread thread = new Thread (new BombardEvents (runner ));
110104 thread .start ();
111105 thread .join ();
112106 Thread .sleep (1000 );
@@ -117,24 +111,25 @@ public void testClubbedData() throws InterruptedException {
117111 * the unit-test to a pessimistic 100 web-socket calls.
118112 */
119113 verify (listener , atMost (NUM_CLUBBED_EVENTS )).onOutputAppend (any (String .class ), any (String .class ), any (String .class ));
120- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
121114 }
122115
123116 @ Test
124117 public void testWarnLoggerForLargeData () throws InterruptedException {
118+ RemoteInterpreterProcessListener listener = mock (RemoteInterpreterProcessListener .class );
119+ AppendOutputRunner runner = new AppendOutputRunner (listener );
125120 String data = "data\n " ;
126121 int numEvents = 100000 ;
122+
127123 for (int i =0 ; i <numEvents ; i ++) {
128- AppendOutputRunner .appendBuffer ("noteId" , "paraId" , data );
124+ runner .appendBuffer ("noteId" , "paraId" , data );
129125 }
130126
131127 TestAppender appender = new TestAppender ();
132128 Logger logger = Logger .getRootLogger ();
133129 logger .addAppender (appender );
134130 Logger .getLogger (RemoteInterpreterEventPoller .class );
135131
136- RemoteInterpreterProcessListener listener = mock (RemoteInterpreterProcessListener .class );
137- loopForCompletingEvents (listener , 1 );
132+ runner .run ();
138133 List <LoggingEvent > log ;
139134
140135 int warnLogCounter ;
@@ -153,17 +148,22 @@ public void testWarnLoggerForLargeData() throws InterruptedException {
153148 String loggerString = "Processing size for buffered append-output is high: " +
154149 (data .length () * numEvents ) + " characters." ;
155150 assertTrue (loggerString .equals (sizeWarnLogEntry .getMessage ()));
156- CheckAppendOutputRunner .stopSchedulerForUnitTests ();
157151 }
158152
159153 private class BombardEvents implements Runnable {
160154
155+ private final AppendOutputRunner runner ;
156+
157+ private BombardEvents (AppendOutputRunner runner ) {
158+ this .runner = runner ;
159+ }
160+
161161 @ Override
162162 public void run () {
163163 String noteId = "noteId" ;
164164 String paraId = "paraId" ;
165165 for (int i =0 ; i <NUM_EVENTS ; i ++) {
166- AppendOutputRunner .appendBuffer (noteId , paraId , "data\n " );
166+ runner .appendBuffer (noteId , paraId , "data\n " );
167167 }
168168 }
169169 }
@@ -200,11 +200,15 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
200200 }).when (listener ).onOutputAppend (any (String .class ), any (String .class ), any (String .class ));
201201 }
202202
203- private void loopForCompletingEvents (RemoteInterpreterProcessListener listener , int numTimes ) {
203+ private void loopForCompletingEvents (RemoteInterpreterProcessListener listener ,
204+ int numTimes , String [][] buffer ) {
204205 numInvocations = 0 ;
205206 prepareInvocationCounts (listener );
206- AppendOutputRunner .setListener (listener );
207- CheckAppendOutputRunner .startScheduler ();
207+ AppendOutputRunner runner = new AppendOutputRunner (listener );
208+ for (String [] bufferElement : buffer ) {
209+ runner .appendBuffer (bufferElement [0 ], bufferElement [1 ], bufferElement [2 ]);
210+ }
211+ CheckAppendOutputRunner .startScheduler (listener , runner );
208212 long startTimeMs = System .currentTimeMillis ();
209213 while (numInvocations != numTimes ) {
210214 if (System .currentTimeMillis () - startTimeMs > 2000 ) {
0 commit comments