use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.
the class GrblCommunicatorTest method testStreamCommands.
/**
* Test of sendStringToComm method, of class GrblCommunicator.
*/
@Test
public void testStreamCommands() {
System.out.println("streamCommands");
MockConnection mc = new MockConnection(mg.in, mg.out);
GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
String term = "\n";
String thirtyNineCharString = "thirty-nine character command here.....";
boolean result;
boolean expResult;
// Make sure CommUtil is still an overly cautious jerk.
LinkedList<GcodeCommand> l = new LinkedList<>();
l.add(new GcodeCommand("12characters"));
assertEquals(13, CommUtils.getSizeOfBuffer(l));
// Make sure GrblUtils hasn't updated RX buffer size.
assertEquals(123, GrblUtils.GRBL_RX_BUFFER_SIZE);
// Add a bunch of commands so that the buffer is full.
// 39*3 + 3 newlines + 3 CommUtils caution = 123 == buffer size.
instance.queueStringForComm(thirtyNineCharString);
instance.queueStringForComm(thirtyNineCharString);
instance.queueStringForComm(thirtyNineCharString);
// Stream them so that there are active commands.
instance.streamCommands();
expResult = true;
result = instance.areActiveCommands();
assertEquals(expResult, result);
// Add another command and stream it so that something is queued.
instance.queueStringForComm(thirtyNineCharString);
instance.streamCommands();
expResult = true;
result = instance.areActiveCommands();
assertEquals(expResult, result);
// Check with MockGrbl to verify the fourth command wasn't sent.
String output = mg.readStringFromGrblBuffer();
String goal = thirtyNineCharString + term + thirtyNineCharString + term + thirtyNineCharString + term;
assertEquals(goal, output);
// Make room for the next command.
mc.sendResponse("ok");
// Send it.
instance.streamCommands();
// Make sure the queued command was sent.
output = mg.readStringFromGrblBuffer();
assertEquals(thirtyNineCharString + term, output);
// Wrap up.
mc.sendResponse("ok");
mc.sendResponse("ok");
mc.sendResponse("ok");
expResult = false;
result = instance.areActiveCommands();
assertEquals(expResult, result);
}
use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.
the class GrblCommunicatorTest method testSendByteImmediately.
/**
* Test of sendStringToComm method, of class GrblCommunicator.
*/
/* This function is private...
@Test
public void testSendStringToComm() {
System.out.println("sendStringToComm");
GrblCommunicator instance = new GrblCommunicator(mg.in, mg.out, cb, asl);
String command = "someCommand";
instance.sendStringToComm(command);
// Make sure the string made it to GRBL.
assertEquals(command, mg.readStringFromGrblBuffer());
}
*/
/**
* Test of sendByteImmediately method, of class GrblCommunicator.
*/
@Test
public void testSendByteImmediately() {
System.out.println("sendByteImmediately");
MockConnection mc = new MockConnection(mg.in, mg.out);
GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
// Ctrl-C is a common byte to send immediately.
byte b = 0x18;
try {
instance.sendByteImmediately(b);
} catch (Exception e) {
fail("sendByteImmediately threw an exception: " + e.getMessage());
}
// Make sure the string made it to GRBL.
assertEquals(b, mg.readByteFromGrblBuffer());
// Test my readByteFromGrblBuffer command (should be empty).
assertEquals(0x0, mg.readByteFromGrblBuffer());
try {
// Buffer multiple bytes
instance.sendByteImmediately((byte) 0x18);
instance.sendByteImmediately((byte) 0x19);
instance.sendByteImmediately((byte) 0x20);
instance.sendByteImmediately((byte) 0x21);
} catch (Exception e) {
fail("sendByteImmediately threw an exception: " + e.getMessage());
}
assertEquals(0x18, mg.readByteFromGrblBuffer());
assertEquals(0x19, mg.readByteFromGrblBuffer());
assertEquals(0x20, mg.readByteFromGrblBuffer());
assertEquals(0x21, mg.readByteFromGrblBuffer());
// Test my readByteFromGrblBuffer command (should be empty).
assertEquals(0x0, mg.readByteFromGrblBuffer());
}
use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.
the class GrblCommunicatorTest method errorResponseShouldPauseTheCommunicator.
@Test
public void errorResponseShouldPauseTheCommunicator() {
System.out.println("streamCommands");
MockConnection mc = new MockConnection(mg.in, mg.out);
GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
instance.setSingleStepMode(true);
String thirtyEightCharString = "thirty-nine character command here....";
// Add a bunch of commands so that the buffer is full.
// 39*3 + 3 newlines + 3 CommUtils caution = 123 == buffer size.
instance.queueStringForComm("1" + thirtyEightCharString);
instance.queueStringForComm("2" + thirtyEightCharString);
instance.queueStringForComm("3" + thirtyEightCharString);
instance.queueStringForComm("4" + thirtyEightCharString);
// We should have no active comands and a buffer of four commands
assertEquals(0, asl.size());
assertEquals(4, cb.size());
assertFalse(instance.isPaused());
// First command active, second is cached (not tested) and two queued.
instance.streamCommands();
assertEquals(1, asl.size());
assertEquals(2, cb.size());
assertFalse(instance.isPaused());
// Complete first command, second becomes active, third is cached, one left in the queue.
// First command ok
mc.sendResponse("ok");
assertEquals(1, asl.size());
assertEquals(1, cb.size());
assertFalse(instance.isPaused());
// After an error on the second command it's completed but the third command is still cached and queue is not changed.
// Second command error
mc.sendResponse("error");
assertEquals(0, asl.size());
assertEquals(1, cb.size());
assertTrue("The communicator should be paused", instance.isPaused());
// Resumes the queue and the third command is sent and fourth command becomes active
instance.resumeSend();
assertEquals(1, asl.size());
assertEquals(0, cb.size());
assertFalse(instance.isPaused());
// Complete third command, the fourth become active
mc.sendResponse("ok");
assertEquals(1, asl.size());
assertEquals(0, cb.size());
assertFalse(instance.isPaused());
// Complete fourth and last command
mc.sendResponse("ok");
assertEquals(0, asl.size());
assertEquals(0, cb.size());
assertFalse(instance.isPaused());
}
use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.
the class GrblCommunicatorTest method testAreActiveCommands.
/**
* Test of areActiveCommands method, of class GrblCommunicator.
*/
@Test
public void testAreActiveCommands() {
System.out.println("areActiveCommands");
MockConnection mc = new MockConnection(mg.in, mg.out);
GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
boolean expResult = false;
boolean result = instance.areActiveCommands();
// Empty case.
assertEquals(expResult, result);
// Add a command and stream it (activate it).
instance.queueStringForComm("command one");
instance.streamCommands();
expResult = true;
result = instance.areActiveCommands();
assertEquals(expResult, result);
// Add another command and stream it.
instance.queueStringForComm("command one");
instance.streamCommands();
expResult = true;
result = instance.areActiveCommands();
assertEquals(expResult, result);
// Send result to communicator.
instance.responseMessage("ok");
instance.responseMessage("ok");
expResult = false;
result = instance.areActiveCommands();
assertEquals(expResult, result);
}
Aggregations