1 package net.sf.atmodem4j.spsw.tests;
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 import java.io.IOException;
31 import java.io.InputStream;
32 import java.text.MessageFormat;
33 import java.util.Arrays;
34 import java.util.Properties;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import net.sf.atmodem4j.spsw.Baudrate;
38 import net.sf.atmodem4j.spsw.DataBits;
39 import net.sf.atmodem4j.spsw.FlowControl;
40 import net.sf.atmodem4j.spsw.GenericTermiosSerialPortSocket;
41 import net.sf.atmodem4j.spsw.Parity;
42 import net.sf.atmodem4j.spsw.SerialPortSocket;
43 import net.sf.atmodem4j.spsw.StopBits;
44 import org.junit.After;
45 import org.junit.Assert;
46 import org.junit.Assume;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Ignore;
50 import org.junit.Test;
51
52
53
54
55 public class TwoPortMultipleBytesTest {
56
57 private static final Logger LOG = Logger.getLogger("SerialTests");
58 private final static int DEFAULT_TEST_BUFFER_SIZE = 1024 * 2;
59
60 public class ReceiverThread extends Thread {
61
62 boolean done;
63 final Object LOCK = new Object();
64 byte[] dataIn;
65 byte[] dataOut;
66 long startTimeStamp;
67 Exception ex;
68 Error err;
69
70 @Override
71 public void run() {
72 try {
73 int offset = 0;
74 while (true) {
75 try {
76 final int count = spc[1].getInputStream().read(dataIn, offset, dataIn.length - offset);
77 if (count > 0) {
78 Assert.assertArrayEquals("Error @Offset: " + offset + " @Count: " + count, Arrays.copyOfRange(dataOut, offset, offset + count), Arrays.copyOfRange(dataIn, offset, offset + count));
79 offset += count;
80 if (offset == dataOut.length) {
81 break;
82 }
83 }
84 LOG.log(Level.FINEST, "Bytes read: {0}", count);
85 if (count <= 0) {
86 if (offset < dataIn.length) {
87 LOG.log(Level.SEVERE, "Bytes missing: {0}", dataIn.length - offset);
88 printArrays("Too short");
89 }
90 break;
91 }
92 } catch (IOException e) {
93 throw new RuntimeException(e);
94 }
95 }
96 LOG.log(Level.INFO, "Byte total read: {0}", offset);
97 LOG.log(Level.INFO, "Receive time in millis:: {0}", (System.currentTimeMillis() - startTimeStamp));
98 synchronized (LOCK) {
99 done = true;
100 LOCK.notifyAll();
101 LOG.log(Level.INFO, "Send Thread finished");
102 }
103 } catch (Exception ex) {
104 synchronized (LOCK) {
105 done = true;
106 this.ex = ex;
107 LOG.log(Level.SEVERE, "Send Thread Exception", ex);
108 LOCK.notifyAll();
109 }
110 } catch (Error err) {
111 synchronized (LOCK) {
112 done = true;
113 this.err = err;
114 printArrays("Error");
115 LOG.log(Level.SEVERE, "Send Thread Error", err);
116 LOCK.notifyAll();
117 }
118 }
119 }
120
121 private void initBuffers(final int size) {
122 dataOut = new byte[size];
123 for (int i = 0; i < size; i++) {
124
125 dataOut[i] = (byte) (Math.round(Math.random() * 0xFF) & 0xFF);
126 }
127 dataIn = new byte[size];
128 }
129
130 private void printArrays(String title) {
131 System.err.println(title);
132 for (int i = 0; i < dataOut.length; i++) {
133 System.err.println(i + "\t: " + dataOut[i] + "\t" + dataIn[i]);
134 }
135 }
136
137 }
138
139 private static final String[] serialPortName = new String[2];
140 private final SerialPortSocket[] spc = new SerialPortSocket[2];
141 private ReceiverThread receiverThread;
142
143 @BeforeClass
144 public static void setUpClass() throws Exception {
145 try (InputStream is = TwoPortMultipleBytesTest.class.getClassLoader().getResourceAsStream("junit-spsw-config.properties")) {
146 if (is == null) {
147 serialPortName[0] = null;
148 serialPortName[1] = null;
149 } else {
150 Properties p = new Properties();
151 p.load(is);
152 serialPortName[0] = p.getProperty("port0", null);
153 serialPortName[1] = p.getProperty("port1", null);
154 }
155 }
156 }
157
158 @Before
159 public void setUp() throws Exception {
160 for (int i = 0; i < serialPortName.length; i++) {
161 if (serialPortName[i] != null) {
162 spc[i] = SerialPortSocket.FACTORY.createSerialPortSocket(serialPortName[i]);
163 receiverThread = new ReceiverThread();
164 } else {
165 spc[i] = null;
166 }
167 }
168 }
169
170 @After
171 public void tearDown() throws Exception {
172 for (int i = 0; i < spc.length; i++) {
173 if (spc[i] != null) {
174 if (spc[i].isOpen()) {
175 spc[i].close();
176 }
177 }
178 spc[i] = null;
179 }
180 receiverThread = null;
181 }
182
183 private void runTest(Baudrate baudrate, int buffersize) throws Exception {
184 LOG.info(MessageFormat.format("do run test @baudrate: {0}, buffer size: {1}", baudrate.value, buffersize));
185
186 receiverThread.initBuffers(buffersize);
187 receiverThread.startTimeStamp = System.currentTimeMillis();
188
189 Assume.assumeNotNull((Object[]) spc);
190 spc[0].openRaw(baudrate, DataBits.DB_8, StopBits.SB_1, Parity.NONE, FlowControl.getFC_RTS_CTS());
191 spc[1].openRaw(spc[0].getBaudrate(), spc[0].getDatatBits(), spc[0].getStopBits(), spc[0].getParity(), spc[0].getFlowControl());
192 printPort(spc[0]);
193
194
195 Assert.assertEquals(0, spc[0].getOutBufferBytesCount());
196 Assert.assertEquals(0, spc[1].getInBufferBytesCount());
197
198 receiverThread.start();
199
200 spc[0].getOutputStream().write(receiverThread.dataOut);
201
202
203 LOG.log(Level.INFO, "Send time in millis:: {0}", (System.currentTimeMillis() - receiverThread.startTimeStamp));
204 synchronized (receiverThread.LOCK) {
205 while (!receiverThread.done) {
206 receiverThread.LOCK.wait();
207 if (receiverThread.ex != null) {
208 throw receiverThread.ex;
209 }
210 if (receiverThread.err != null) {
211 throw receiverThread.err;
212 }
213 }
214 LOG.log(Level.INFO, "Thread finished received");
215 }
216 try {
217 Assert.assertArrayEquals(receiverThread.dataOut, receiverThread.dataIn);
218 } catch (Error err) {
219 if (spc[0] instanceof GenericTermiosSerialPortSocket) {
220 ((GenericTermiosSerialPortSocket) spc[0]).printTermios();
221 ((GenericTermiosSerialPortSocket) spc[1]).printTermios();
222 }
223 throw err;
224 }
225 }
226
227 private void printPort(SerialPortSocket sPort) throws IOException {
228 System.err.println("Baudrate: " + sPort.getBaudrate().value);
229 System.err.println("DataBits: " + sPort.getDatatBits().value);
230 System.err.println("StopBits: " + sPort.getStopBits().value);
231 System.err.println("Parity: " + sPort.getParity());
232 System.err.println("Flowcontrol: " + sPort.getFlowControl());
233
234 }
235
236 @Ignore
237 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 300)
238 public void test_0000300() throws Exception {
239 runTest(Baudrate.B300, DEFAULT_TEST_BUFFER_SIZE);
240 }
241
242 @Ignore
243 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 2400)
244 public void test_0002400() throws Exception {
245 runTest(Baudrate.B2400, DEFAULT_TEST_BUFFER_SIZE);
246 }
247
248 @Ignore
249 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 9600)
250 public void test_0009600() throws Exception {
251 runTest(Baudrate.B9600, DEFAULT_TEST_BUFFER_SIZE);
252 }
253
254 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 19200)
255 public void test_0019200() throws Exception {
256 runTest(Baudrate.B19200, DEFAULT_TEST_BUFFER_SIZE);
257 }
258
259 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 57600)
260 public void test_0057600() throws Exception {
261 runTest(Baudrate.B57600, DEFAULT_TEST_BUFFER_SIZE);
262 }
263
264 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 115200)
265 public void test_0115200() throws Exception {
266 runTest(Baudrate.B115200, DEFAULT_TEST_BUFFER_SIZE);
267 }
268
269 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 230400)
270 public void test_0230400() throws Exception {
271 runTest(Baudrate.B230400, DEFAULT_TEST_BUFFER_SIZE);
272 }
273
274 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 460800)
275 public void test_0460800() throws Exception {
276 runTest(Baudrate.B460800, DEFAULT_TEST_BUFFER_SIZE);
277 }
278
279 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 500000)
280 public void test_0500000() throws Exception {
281 runTest(Baudrate.B500000, DEFAULT_TEST_BUFFER_SIZE);
282 }
283
284 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 576000)
285 public void test_0576000() throws Exception {
286 runTest(Baudrate.B576000, DEFAULT_TEST_BUFFER_SIZE);
287 }
288
289 @Ignore
290 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 1000000)
291 public void test_1000000() throws Exception {
292 runTest(Baudrate.B1000000, DEFAULT_TEST_BUFFER_SIZE);
293 }
294
295 @Ignore
296 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 1152000)
297 public void test_1152000() throws Exception {
298 runTest(Baudrate.B1152000, DEFAULT_TEST_BUFFER_SIZE);
299 }
300
301 @Ignore
302 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 2000000)
303 public void test_2000000() throws Exception {
304 runTest(Baudrate.B2000000, DEFAULT_TEST_BUFFER_SIZE);
305 }
306
307 @Ignore
308 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 3000000)
309 public void test_3000000() throws Exception {
310 runTest(Baudrate.B3000000, DEFAULT_TEST_BUFFER_SIZE);
311 }
312
313 @Ignore
314 @Test(timeout = 1000 + (DEFAULT_TEST_BUFFER_SIZE * 10 * 1000) / 4000000)
315 public void test_4000000() throws Exception {
316 runTest(Baudrate.B4000000, DEFAULT_TEST_BUFFER_SIZE);
317 }
318
319 }