Skip to content

Commit 3749c10

Browse files
authored
0.39.0
* Testng * Fix reconnection logic.
1 parent 5a99ab6 commit 3749c10

30 files changed

+393
-361
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
subprojects {
22

3-
apply plugin: 'java'
43
apply plugin: 'jacoco'
4+
apply plugin: 'java'
55
apply plugin: 'maven'
66

77
group 'org.iot-dsa'
8-
version '0.38.1'
8+
version '0.39.0'
99

1010
sourceCompatibility = 1.6
1111
targetCompatibility = 1.6
@@ -38,5 +38,5 @@ subprojects {
3838
}
3939

4040
wrapper {
41-
gradleVersion = '4.10.1'
41+
gradleVersion = '4.10.2'
4242
}

dslink-v2/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ artifacts {
44
}
55

66
dependencies {
7-
testImplementation 'junit:junit:[4.12,)'
7+
testImplementation 'org.testng:testng:[6.14.3,)'
8+
}
9+
10+
test {
11+
useTestNG()
812
}
913

1014
javadoc {

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/DSSession.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ protected void onDisconnected() {
286286
outgoingResponses.clear();
287287
notifyOutgoing();
288288
try {
289-
readThread.join();
289+
if (Thread.currentThread() != readThread) {
290+
readThread.join();
291+
}
290292
} catch (Exception x) {
291293
debug(getPath(), x);
292294
}
@@ -304,7 +306,9 @@ protected void onDisconnecting() {
304306
connected = false;
305307
notifyOutgoing();
306308
try {
307-
writeThread.join();
309+
if (Thread.currentThread() != writeThread) {
310+
writeThread.join();
311+
}
308312
} catch (Exception x) {
309313
debug(getPath(), x);
310314
}

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/v1/DS1LinkConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ protected void onDisconnected() {
175175
writer = null;
176176
transport = null;
177177
remove(TRANSPORT);
178-
info(getConnectionId() + " disconnected");
178+
debug(getConnectionId() + " disconnected");
179179
}
180180

181181
/**

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/v1/DS1Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected void doRecvMessage() throws IOException {
103103
case ROOT:
104104
break;
105105
case END_INPUT:
106-
return;
106+
throw new IOException("Connection remotely closed");
107107
default:
108108
throw new IOException("Unexpected input: " + reader.last());
109109
}

dslink-v2/src/main/java/org/iot/dsa/conn/DSConnection.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,22 @@ public abstract class DSConnection extends DSNode implements DSIStatus, Runnable
7575
///////////////////////////////////////////////////////////////////////////
7676

7777
/**
78-
* If the connection state is connecting or connected, this will call disconnect. If
79-
* disconnect has already been called, this notifies the subtree if the connection actually
80-
* transitions to disconnected.
78+
* If the connection state is connected, this will call disconnect. Otherwise, if not
79+
* already disconnected this transitions the connection to disconnected.
8180
*
8281
* @param reason Optional
8382
*/
8483
public void connDown(String reason) {
8584
debug(debug() ? "connDown: " + reason : null);
86-
if (getConnectionState().isEngaged()) {
85+
if (getConnectionState().isConnected()) {
8786
put(lastFail, DSDateTime.currentTime());
8887
if (reason != null) {
8988
if (!statusText.getElement().toString().equals(reason)) {
9089
put(statusText, DSString.valueOf(reason));
9190
}
9291
}
9392
disconnect();
94-
} else if (getConnectionState().isDisconnecting()) {
93+
} else if (!getConnectionState().isDisconnected()) {
9594
put(status, getStatus().add(DSStatus.DOWN));
9695
put(state, DSConnectionState.DISCONNECTED);
9796
put(stateTime, DSDateTime.currentTime());
@@ -112,6 +111,7 @@ public void connDown(String reason) {
112111
*/
113112
public void connOk() {
114113
if (getConnectionState().isDisengaged()) {
114+
debug(debug() ? "Not connecting or connected, ignoring connOk()" : null);
115115
return;
116116
}
117117
long now = System.currentTimeMillis();
@@ -164,13 +164,10 @@ public void connect() {
164164
}
165165

166166
/**
167-
* Initiates disconnection logic.
167+
* Transitions the connection to disconnecting then disconnected and makes all the
168+
* respective callbacks.
168169
*/
169170
public void disconnect() {
170-
if (!getConnectionState().isConnected()) {
171-
debug(debug() ? "Not connected, ignoring disconnect()" : null);
172-
return;
173-
}
174171
debug(debug() ? "Disconnect" : null);
175172
put(state, DSConnectionState.DISCONNECTING);
176173
put(stateTime, DSDateTime.currentTime());
@@ -179,8 +176,17 @@ public void disconnect() {
179176
onDisconnect();
180177
} catch (Throwable x) {
181178
error(getPath(), x);
182-
connDown(DSException.makeMessage(x));
183179
}
180+
put(status, getStatus().add(DSStatus.DOWN));
181+
put(state, DSConnectionState.DISCONNECTED);
182+
put(stateTime, DSDateTime.currentTime());
183+
notifyDescendants(this);
184+
try {
185+
onDisconnected();
186+
} catch (Exception x) {
187+
error(error() ? getPath() : null, x);
188+
}
189+
fire(DSConnectionEvent.DISCONNECTED, null);
184190
}
185191

186192
public DSConnectionState getConnectionState() {
@@ -351,7 +357,8 @@ protected void onConnected() {
351357
}
352358

353359
/**
354-
* Close and clean up resources.
360+
* Close and clean up resources, when this returns, the connection will be put into
361+
* the disconnected state, onDisconnected will be called, and descendants will be notified.
355362
*/
356363
protected abstract void onDisconnect();
357364

dslink-v2/src/main/java/org/iot/dsa/dslink/DSLinkConnection.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ protected void onDisconnect() {
122122
if (getTransport() != null) {
123123
getTransport().close();
124124
}
125-
connDown(null);
126125
} catch (Exception x) {
127-
debug(getPath(), x);
128-
connDown(DSException.makeMessage(x));
126+
error(getPath(), x);
129127
}
130128
}
131129

dslink-v2/src/test/java/org/iot/dsa/dslink/DSByteBufferTest.java

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,54 @@
22

33
import com.acuity.iot.dsa.dslink.io.DSByteBuffer;
44
import java.nio.ByteBuffer;
5-
import org.junit.Assert;
6-
import org.junit.Test;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
77

88
/**
99
* @author Aaron Hansen
1010
*/
1111
public class DSByteBufferTest {
1212

13+
@Test
14+
public void comparisonTest() {
15+
ByteBuffer javaBuf = ByteBuffer.allocate(1024);
16+
javaBuf.put((byte) 1);
17+
DSByteBuffer dsBuf = new DSByteBuffer();
18+
dsBuf.put((byte) 1);
19+
byte[] dsry = dsBuf.toByteArray();
20+
byte[] jary = toByteArray(javaBuf);
21+
Assert.assertEquals(dsry.length, jary.length);
22+
Assert.assertEquals(dsry, jary);
23+
//double
24+
javaBuf.putDouble(12345.6);
25+
dsBuf.putDouble(12345.6);
26+
dsry = dsBuf.toByteArray();
27+
jary = toByteArray(javaBuf);
28+
Assert.assertEquals(dsry.length, jary.length);
29+
Assert.assertEquals(dsry, jary);
30+
//int
31+
javaBuf.putInt(12345);
32+
dsBuf.putInt(12345);
33+
dsry = dsBuf.toByteArray();
34+
jary = toByteArray(javaBuf);
35+
Assert.assertEquals(dsry.length, jary.length);
36+
Assert.assertEquals(dsry, jary);
37+
//long
38+
javaBuf.putLong(12345);
39+
dsBuf.putLong(12345);
40+
dsry = dsBuf.toByteArray();
41+
jary = toByteArray(javaBuf);
42+
Assert.assertEquals(dsry.length, jary.length);
43+
Assert.assertEquals(dsry, jary);
44+
//short
45+
javaBuf.putShort((short) 123);
46+
dsBuf.putShort((short) 123);
47+
dsry = dsBuf.toByteArray();
48+
jary = toByteArray(javaBuf);
49+
Assert.assertEquals(dsry.length, jary.length);
50+
Assert.assertEquals(dsry, jary);
51+
}
52+
1353
@Test
1454
public void theTest() throws Exception {
1555
DSByteBuffer buffer = new DSByteBuffer(5);
@@ -19,7 +59,7 @@ public void theTest() throws Exception {
1959
Assert.assertTrue(buffer.available() == tmp.length);
2060
int len = buffer.sendTo(tmp, 0, tmp.length);
2161
Assert.assertTrue(len == tmp.length);
22-
Assert.assertArrayEquals(tmp, new byte[]{1, 2, 3, 4, 5});
62+
Assert.assertEquals(tmp, new byte[]{1, 2, 3, 4, 5});
2363
buffer.put(tmp, 0, tmp.length);
2464
Assert.assertTrue(buffer.read() == 1);
2565
Assert.assertTrue(buffer.read() == 2);
@@ -50,46 +90,6 @@ public void theTest() throws Exception {
5090
Assert.assertTrue(buffer.read() == 3);
5191
}
5292

53-
@Test
54-
public void comparisonTest() {
55-
ByteBuffer javaBuf = ByteBuffer.allocate(1024);
56-
javaBuf.put((byte) 1);
57-
DSByteBuffer dsBuf = new DSByteBuffer();
58-
dsBuf.put((byte) 1);
59-
byte[] dsry = dsBuf.toByteArray();
60-
byte[] jary = toByteArray(javaBuf);
61-
Assert.assertEquals(dsry.length, jary.length);
62-
Assert.assertArrayEquals(dsry, jary);
63-
//double
64-
javaBuf.putDouble(12345.6);
65-
dsBuf.putDouble(12345.6);
66-
dsry = dsBuf.toByteArray();
67-
jary = toByteArray(javaBuf);
68-
Assert.assertEquals(dsry.length, jary.length);
69-
Assert.assertArrayEquals(dsry, jary);
70-
//int
71-
javaBuf.putInt(12345);
72-
dsBuf.putInt(12345);
73-
dsry = dsBuf.toByteArray();
74-
jary = toByteArray(javaBuf);
75-
Assert.assertEquals(dsry.length, jary.length);
76-
Assert.assertArrayEquals(dsry, jary);
77-
//long
78-
javaBuf.putLong(12345);
79-
dsBuf.putLong(12345);
80-
dsry = dsBuf.toByteArray();
81-
jary = toByteArray(javaBuf);
82-
Assert.assertEquals(dsry.length, jary.length);
83-
Assert.assertArrayEquals(dsry, jary);
84-
//short
85-
javaBuf.putShort((short) 123);
86-
dsBuf.putShort((short) 123);
87-
dsry = dsBuf.toByteArray();
88-
jary = toByteArray(javaBuf);
89-
Assert.assertEquals(dsry.length, jary.length);
90-
Assert.assertArrayEquals(dsry, jary);
91-
}
92-
9393
private byte[] toByteArray(ByteBuffer buf) {
9494
byte[] ret = new byte[buf.position()];
9595
buf.flip();

dslink-v2/src/test/java/org/iot/dsa/dslink/DSElementTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import org.iot.dsa.node.DSElementType;
99
import org.iot.dsa.node.DSList;
1010
import org.iot.dsa.node.DSMap;
11-
import org.iot.dsa.time.DSTime;
12-
import org.junit.Assert;
13-
import org.junit.Test;
11+
import org.testng.Assert;
12+
import org.testng.annotations.Test;
1413

1514
/**
1615
* @author Aaron Hansen
@@ -275,6 +274,12 @@ private void testMap(DSMap map) {
275274
Assert.assertTrue(map.removeLast() == tmp);
276275
}
277276

277+
private void testNull(DSElement obj) {
278+
Assert.assertTrue(DSElement.makeNull() == obj);
279+
Assert.assertTrue(obj.isNull());
280+
Assert.assertTrue(obj.getElementType() == DSElementType.NULL);
281+
}
282+
278283
private void testPrimitiveList(DSList list) {
279284
testList(list);
280285
Assert.assertTrue(list.get(0).isBoolean());
@@ -295,10 +300,4 @@ private void testPrimitiveMap(DSMap map) {
295300
Assert.assertTrue(map.get("null").isNull());
296301
}
297302

298-
private void testNull(DSElement obj) {
299-
Assert.assertTrue(DSElement.makeNull() == obj);
300-
Assert.assertTrue(obj.isNull());
301-
Assert.assertTrue(obj.getElementType() == DSElementType.NULL);
302-
}
303-
304303
}

dslink-v2/src/test/java/org/iot/dsa/dslink/DSFlexEnumTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import org.iot.dsa.node.DSElement;
55
import org.iot.dsa.node.DSFlexEnum;
66
import org.iot.dsa.node.DSList;
7-
import org.junit.Assert;
8-
import org.junit.Test;
7+
import org.testng.Assert;
8+
import org.testng.annotations.Test;
99

1010
/**
1111
* @author Aaron Hansen

0 commit comments

Comments
 (0)