-
-
Notifications
You must be signed in to change notification settings - Fork 823
Expand file tree
/
Copy pathBufferedCommunicatorTest.java
More file actions
613 lines (489 loc) · 19 KB
/
Copy pathBufferedCommunicatorTest.java
File metadata and controls
613 lines (489 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Copyright 2015-2018 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender;
import com.willwinder.universalgcodesender.connection.Connection;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.listeners.SerialCommunicatorListener;
import com.willwinder.universalgcodesender.types.GcodeCommand;
import com.willwinder.universalgcodesender.utils.GcodeStreamReader;
import com.willwinder.universalgcodesender.utils.GcodeStreamTest;
import com.willwinder.universalgcodesender.utils.GcodeStreamWriter;
import org.apache.commons.io.FileUtils;
import org.easymock.EasyMock;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.concurrent.LinkedBlockingDeque;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author wwinder
*/
public class BufferedCommunicatorTest {
private static File tempDir;
private final static Connection mockConnection = EasyMock.createMock(Connection.class);
private final static SerialCommunicatorListener mockScl = EasyMock.createMock(SerialCommunicatorListener.class);
private BufferedCommunicator instance;
private LinkedBlockingDeque<String> cb;
private LinkedBlockingDeque<GcodeCommand> asl;
public BufferedCommunicatorTest() {
}
@BeforeClass
static public void setup() throws IOException {
tempDir = GcodeStreamTest.createTempDirectory();
}
@AfterClass
static public void teardown() throws IOException {
FileUtils.forceDeleteOnExit(tempDir);
}
@Before
public void setUp() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
EasyMock.reset(mockConnection, mockScl);
cb = new LinkedBlockingDeque<>();
asl = new LinkedBlockingDeque<>();
instance = new BufferedCommunicatorImpl(cb, asl);
instance.setConnection(mockConnection);
instance.setListenAll(mockScl);
// Initialize private variable.
Field f = AbstractCommunicator.class.getDeclaredField("launchEventsInDispatchThread");
f.setAccessible(true);
f.set(instance, false);
EasyMock.reset(mockConnection, mockScl);
}
/**
* Test of getBufferSize method, of class BufferedCommunicator.
*/
@Test
@Ignore
public void testSingleStepMode() {
System.out.println("testSingleStepMode");
fail("Not implemented yet.");
}
/**
* Test of setSingleBlockMode method of class BufferedCommunicator.
*/
@Test
@Ignore
public void testSingleBlockMode() {
System.out.println("testSingleBlockMode");
fail("Not implemented yet.");
}
/**
* Test of getBufferSize method, of class BufferedCommunicator.
*/
@Test
public void testGetBufferSize() {
System.out.println("getBufferSize");
assertEquals(101, instance.getBufferSize());
}
/**
* Test of queueStringForComm method, of class BufferedCommunicator.
*/
@Test
public void testQueueStringForComm() {
System.out.println("queueStringForComm");
String input = "input";
instance.queueStringForComm(input);
assertEquals(input, cb.getFirst());
}
/**
* Test of streamCommands method, of class BufferedCommunicator.
*/
@Test
public void testSimpleQueueStringsStream() throws Exception {
System.out.println("streamCommands");
String input = "input";
// Check events and connection:
// console message, connection stream, sent event
mockConnection.sendStringToComm(input + "\n");
EasyMock.expect(EasyMock.expectLastCall()).times(2);
mockScl.commandSent(EasyMock.<GcodeCommand>anyObject());
EasyMock.expect(EasyMock.expectLastCall()).times(2);
EasyMock.replay(mockConnection, mockScl);
// Test
instance.queueStringForComm(input);
instance.queueStringForComm(input);
instance.streamCommands();
EasyMock.verify(mockConnection, mockScl);
}
@Test
public void testSimpleStreamStream() throws Exception {
String[] inputs = {"input1", "input2"};
for (String i : inputs) {
mockConnection.sendStringToComm(i + "\n");
EasyMock.expect(EasyMock.expectLastCall());
mockScl.commandSent(EasyMock.<GcodeCommand>anyObject());
EasyMock.expect(EasyMock.expectLastCall());
}
EasyMock.replay(mockConnection, mockScl);
File f = new File(tempDir,"gcodeFile");
try (GcodeStreamWriter gsw = new GcodeStreamWriter(f)) {
for(String i : inputs) {
gsw.addLine("blah", i, null, -1);
}
}
GcodeStreamReader gsr = new GcodeStreamReader(f);
instance.queueStreamForComm(gsr);
instance.streamCommands();
assertEquals("input1, input2, 0 streaming commands.", instance.activeCommandSummary());
EasyMock.verify(mockConnection, mockScl);
}
/**
* Test of areActiveCommands method, of class BufferedCommunicator.
*/
@Test
public void testActiveCommands() throws Exception {
System.out.println("areActiveCommands");
String input = "input";
// Setup 2 active commands.
mockConnection.sendStringToComm(input + "\n");
EasyMock.expect(EasyMock.expectLastCall()).times(2);
mockScl.commandSent(EasyMock.<GcodeCommand>anyObject());
EasyMock.expect(EasyMock.expectLastCall()).times(2);
// Left an active command.
// Active commands complete.
mockScl.rawResponseListener("ok");
EasyMock.expect(EasyMock.expectLastCall()).times(2);
EasyMock.replay(mockConnection, mockScl);
//////////////
// THE TEST //
//////////////
// No active commands.
assertEquals(false, instance.areActiveCommands());
assertEquals(0, instance.numActiveCommands());
// Leave active commands in pipeline.
instance.queueStringForComm(input);
instance.queueStringForComm(input);
instance.streamCommands();
assertEquals(true, instance.areActiveCommands());
assertEquals(2, instance.numActiveCommands());
assertEquals(input + ", " + input, instance.activeCommandSummary());
// Clear out active commands.
instance.responseMessage("ok");
assertEquals(true, instance.areActiveCommands());
instance.responseMessage("ok");
assertEquals(false, instance.areActiveCommands());
assertEquals(0, instance.numActiveCommands());
EasyMock.verify(mockConnection, mockScl);
}
/**
* Test of pauseSend method, of class BufferedCommunicator.
*/
@Test
public void testPauseSendResume() throws Exception {
System.out.println("pauseSend");
String input = "123456789";
for (int i = 0; i < 11; i++) {
mockConnection.sendStringToComm(input + "\n");
EasyMock.expect(EasyMock.expectLastCall());
}
EasyMock.replay(mockConnection);
// Send the first 10 commands, pause 11th
for (int i = 0; i < 11; i++) {
instance.queueStringForComm(input);
}
instance.streamCommands();
instance.pauseSend();
assertEquals("First 10 commands sent.", 10, asl.size());
for (int i = 0; i < 10; i++) {
instance.responseMessage("ok");
}
assertEquals("First 10 commands done.", 0, asl.size());
// Resume and send the last command.
instance.resumeSend();
assertEquals("Last comamnd active.", 1, asl.size());
EasyMock.verify(mockConnection);
}
/**
* Test of cancelSend method, of class BufferedCommunicator.
*/
@Test
public void testCancelSend() {
System.out.println("cancelSend");
// Queue up 200 characters.
String tenChar = "123456789";
for (int i = 0; i < 20; i++) {
instance.queueStringForComm(tenChar);
}
instance.streamCommands();
// Cancel the send, and clear out the 10 active commands.
instance.cancelSend();
for (int i = 0; i < 10; i++) {
instance.responseMessage("ok");
}
assertEquals(false, instance.areActiveCommands());
}
/**
* Test of responseMessage method, of class BufferedCommunicator.
*/
@Test
public void testResponseMessage() throws Exception {
System.out.println("responseMessage");
String first = "not-handled";
mockScl.rawResponseListener(first);
EasyMock.expect(EasyMock.expectLastCall()).once();
mockScl.rawResponseListener("ok");
EasyMock.expect(EasyMock.expectLastCall()).once();
EasyMock.replay(mockScl);
asl.add(new GcodeCommand("command"));
instance.responseMessage(first);
assertEquals(1, asl.size());
instance.responseMessage("ok");
assertEquals(0, asl.size());
}
/**
* Test of openCommPort method, of class BufferedCommunicator.
*/
@Test
public void testOpenCommPort() throws Exception {
System.out.println("openCommPort");
String name = "";
int baud = 0;
boolean expResult = true;
mockConnection.setCommunicator(EasyMock.<AbstractCommunicator>anyObject());
EasyMock.expect(EasyMock.expectLastCall()).once();
EasyMock.expect(mockConnection.openPort()).andReturn(true).once();
EasyMock.replay(mockConnection);
boolean result = instance.openCommPort(ConnectionDriver.JSSC, name, baud);
assertEquals(expResult, result);
EasyMock.verify(mockConnection);
}
/**
* Test of closeCommPort method, of class BufferedCommunicator.
*/
@Test
public void testCloseCommPort() throws Exception {
System.out.println("closeCommPort");
boolean expResult = true;
mockConnection.closePort();
EasyMock.expect(EasyMock.expectLastCall()).once();
EasyMock.replay(mockConnection);
instance.closeCommPort();
EasyMock.verify(mockConnection);
}
/**
* Test of sendByteImmediately method, of class BufferedCommunicator.
*/
@Test
public void testSendByteImmediately() throws Exception {
System.out.println("sendByteImmediately");
byte b = 10;
String tenChar = "123456789";
for (int i = 0; i < 10; i++) {
mockConnection.sendStringToComm(tenChar + "\n");
}
mockConnection.sendByteImmediately(b);
EasyMock.replay(mockConnection);
// Queue up 200 characters.
for (int i = 0; i < 20; i++) {
instance.queueStringForComm(tenChar);
}
instance.streamCommands();
// Make sure the byte is sent.
instance.sendByteImmediately(b);
EasyMock.verify(mockConnection);
}
/**
* Test of sendingCommand method, of class BufferedCommunicatorImpl.
*/
@Test
public void testSendingCommand() {
System.out.println("sendingCommand");
System.out.println("-N/A for abstract class-");
}
/**
* Test of processedCommand method, of class BufferedCommunicatorImpl.
*/
@Test
public void testProcessedCommand() {
System.out.println("processedCommand");
System.out.println("-N/A for abstract class-");
}
@Test
public void testStreamCommandsOrderStringCommandsFirst() throws Exception {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
ArgumentCaptor<String> commandCaptor = ArgumentCaptor.forClass(String.class);
doNothing().when(connection).sendStringToComm(commandCaptor.capture());
// Create a gcode file stream
File gcodeFile = new File(tempDir,"gcodeFile");
GcodeStreamWriter gcodeStreamWriter = new GcodeStreamWriter(gcodeFile);
gcodeStreamWriter.addLine("G0", "G0", null, 0);
gcodeStreamWriter.close();
instance.queueStreamForComm(new GcodeStreamReader(gcodeFile));
instance.queueStringForComm("G1");
// When
instance.streamCommands();
// Then
assertEquals(2, commandCaptor.getAllValues().size());
assertEquals("The first command processed should be the string command", "G1\n", commandCaptor.getAllValues().get(0));
assertEquals("The second command should be from the stream", "G0\n", commandCaptor.getAllValues().get(1));
}
@Test
public void softResetShouldClearBuffersAndResumeOperation() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
asl.add(new GcodeCommand("G0"));
cb.add("G0");
instance.pauseSend();
assertTrue(instance.isPaused());
assertEquals(1, instance.numActiveCommands());
assertEquals(1, instance.numBufferedCommands());
// When
instance.softReset();
// Then
assertFalse("The communicator should resume operation after a reset", instance.isPaused());
assertEquals("There should be no active commands", 0, instance.numActiveCommands());
assertEquals("There should be no buffered manual commands", 0, instance.numBufferedCommands());
}
@Test
public void responseMessageOnErrorShouldPauseTheCommunicator() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
asl.add(new GcodeCommand("G0"));
asl.add(new GcodeCommand("G0"));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertTrue(instance.isPaused());
}
@Test
public void responseMessageOnErrorShouldDispatchPauseEvent() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
SerialCommunicatorListener serialCommunicatorListener = mock(SerialCommunicatorListener.class);
instance.addCommandEventListener(serialCommunicatorListener);
asl.add(new GcodeCommand("G0"));
asl.add(new GcodeCommand("G0"));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertTrue(instance.isPaused());
verify(serialCommunicatorListener, times(1)).communicatorPausedOnError();
}
@Test
public void responseMessageOnErrorOnManualCommandShouldPauseTheCommunicator() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
cb.add("G0");
cb.add("G0");
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertTrue(instance.isPaused());
}
@Test
public void responseMessageOnErrorOnLastManualCommandShouldNotPauseTheCommunicator() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
cb.add("G0");
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertFalse(instance.isPaused());
}
@Test
public void responseMessageOnErrorOnLastCommandShouldNotPauseTheCommunicator() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
asl.add(new GcodeCommand("G0"));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertFalse(instance.isPaused());
}
@Test
public void responseMessageOnErrorOnCommandStreamShouldPauseTheCommunicator() throws IOException, GcodeStreamReader.NotGcodeStreamFile {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
// Create a gcode file stream
File gcodeFile = new File(tempDir, "gcodeFile");
GcodeStreamWriter gcodeStreamWriter = new GcodeStreamWriter(gcodeFile);
gcodeStreamWriter.addLine("G0", "G0", null, 0);
gcodeStreamWriter.addLine("G0", "G0", null, 0);
gcodeStreamWriter.close();
// Stream
instance.queueStreamForComm(new GcodeStreamReader(gcodeFile));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertTrue(instance.isPaused());
}
@Test
public void responseMessageOnErrorOnLastCommandStreamShouldNotPauseTheCommunicator() throws IOException, GcodeStreamReader.NotGcodeStreamFile {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
// Create a gcode file stream
File gcodeFile = new File(tempDir, "gcodeFile");
GcodeStreamWriter gcodeStreamWriter = new GcodeStreamWriter(gcodeFile);
gcodeStreamWriter.addLine("G0", "G0", null, 0);
gcodeStreamWriter.close();
// Stream
instance.queueStreamForComm(new GcodeStreamReader(gcodeFile));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertFalse(instance.isPaused());
}
public class BufferedCommunicatorImpl extends BufferedCommunicator {
BufferedCommunicatorImpl(LinkedBlockingDeque<String> cb, LinkedBlockingDeque<GcodeCommand> asl) {
super(cb, asl);
}
public int getBufferSize() {
return 101;
}
public void sendingCommand(String command) {
}
public boolean processedCommand(String response) {
return (response != null &&
("ok".equals(response) || response.startsWith("error")));
}
public boolean processedCommandIsError(String response) {
return (response != null && response.startsWith("error"));
}
@Override
public String getLineTerminator() {
return "\r\n";
}
}
}