Implement HTTP client test

Fix #479
This commit is contained in:
Mygod
2015-12-15 19:39:57 +08:00
parent 3108306b6e
commit 62a572ee3a
5 changed files with 99 additions and 7 deletions
@@ -0,0 +1,35 @@
package com.github.shadowsocks.utils
/**
* @author Mygod
*/
object CloseUtils {
type Disconnectable = {
def disconnect()
}
def autoClose[A <: AutoCloseable, B](x: => A)(block: A => B): B = {
var a: Option[A] = None
try {
a = Some(x)
block(a.get)
} finally if (a.nonEmpty) try {
val v = a.get
if (v ne null) v.close
} catch {
case ignore: Exception =>
}
}
def autoDisconnect[A <: Disconnectable, B](x: => A)(block: A => B): B = {
var a: Option[A] = None
try {
a = Some(x)
block(a.get)
} finally if (a.nonEmpty) try {
val v = a.get
if (v ne null) v.disconnect
} catch {
case ignore: Exception =>
}
}
}