Repository: hyperledger/quilt (java)

Issue:
Make StreamConnection Closeable
Consider making `StreamConnection` auto closeable in a try-with-resources statement by having it implement [Closeable](https://docs.oracle.com/javase/8/docs/api/java/io/Closeable.html).

Pull request:
Fixes #310 Implements Closeable in StreamConnection
Signed-off-by: Sudheesh Singanamalla <sudheesh@cs.washington.edu>

Patch:
diff --git a/stream-parent/stream-core/src/main/java/org/interledger/stream/StreamConnection.java b/stream-parent/stream-core/src/main/java/org/interledger/stream/StreamConnection.java
index 2f0af0c..54f95ac 100644
--- a/stream-parent/stream-core/src/main/java/org/interledger/stream/StreamConnection.java
+++ b/stream-parent/stream-core/src/main/java/org/interledger/stream/StreamConnection.java
@@ -6,6 +6,8 @@ import org.interledger.core.SharedSecret;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.primitives.UnsignedLong;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.time.Instant;
 import java.util.Objects;
 import java.util.StringJoiner;
@@ -18,7 +20,7 @@ import java.util.concurrent.atomic.AtomicReference;
  * <p>Any given Connection in a JVM manages a unique sequence id that can be incremented for each Stream Packet sent
  * over the connection.</p>
  */
-public class StreamConnection {
+public class StreamConnection implements Closeable {
 
   // NOTE: Integer.MAX_VALUE is 1 less than what we want for our Max per IL-RFC-29.
   public static final UnsignedLong MAX_FRAMES_PER_CONNECTION =
@@ -180,6 +182,11 @@ public class StreamConnection {
         .toString();
   }
 
+  @Override
+  public void close() throws IOException {
+    this.closeConnection();
+  }
+
   /**
    * Valid states for a {@link StreamConnection}. A StreamConnection begins life in the {@link #AVAILABLE} state. Once
    * the first sequence is emitted, the Connection transitions into the {@link #OPEN} state. If a Connection is closed,


Test patch:
diff --git a/stream-parent/stream-core/src/test/java/org/interledger/stream/StreamConnectionTest.java b/stream-parent/stream-core/src/test/java/org/interledger/stream/StreamConnectionTest.java
index f7e97ca..484c4fa 100644
--- a/stream-parent/stream-core/src/test/java/org/interledger/stream/StreamConnectionTest.java
+++ b/stream-parent/stream-core/src/test/java/org/interledger/stream/StreamConnectionTest.java
@@ -1,6 +1,7 @@
 package org.interledger.stream;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
 import static org.mockito.Mockito.mock;
 
 import org.interledger.core.InterledgerAddress;
@@ -14,6 +15,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import java.io.IOException;
 import java.time.Instant;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
@@ -130,6 +132,18 @@ public class StreamConnectionTest {
     assertThat(streamConnection.isClosed()).isTrue();
   }
 
+  @Test
+  public void testStreamConnectionClose() {
+    assertThat(streamConnection.isClosed()).isFalse();
+    try {
+      streamConnection.close();
+    } catch (IOException e) {
+      fail("This error should not have occurred while closing a StreamConnection.");
+      e.printStackTrace();
+    }
+    assertThat(streamConnection.isClosed()).isTrue();
+  }
+
   @Test
   public void transitionConnectionStateAfterConnectionClosed() {
     streamConnection.closeConnection();
@@ -235,4 +249,15 @@ public class StreamConnectionTest {
     StreamConnection nonIdenticalStreamConnection = new StreamConnection(StreamConnectionId.of("foo1"));
     assertThat(streamConnection.hashCode()).isNotEqualTo(nonIdenticalStreamConnection.hashCode());
   }
+
+  @Test
+  public void testToString() {
+    final String matchingSerializedRegex = "StreamConnection\\["
+        + "creationDateTime=[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.\\d+Z, "
+        + "streamConnectionId=\\S+ sequence=\\d+, connectionState=[A-Z]+]";
+
+    final String serializedStreamConnection = streamConnection.toString();
+
+    assertThat(serializedStreamConnection.matches(matchingSerializedRegex)).isTrue();
+  }
 }