-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add receiveIdleTimeout and sendIdleTimeout config parameters to websocket client and server * Add idleTimeout stack layer * Add integration tests
- Loading branch information
Showing
8 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...-core/src/test/scala/akka/http/impl/engine/ws/WebSocketServerReceiveIdleTimeoutSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package akka.http.impl.engine.ws | ||
|
||
import akka.Done | ||
import akka.http.impl.util.AkkaSpecWithMaterializer | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.model.AttributeKeys.webSocketUpgrade | ||
import akka.http.scaladsl.model.Uri.apply | ||
import akka.http.scaladsl.model.ws._ | ||
import akka.stream.scaladsl._ | ||
import akka.stream.testkit._ | ||
import akka.testkit._ | ||
import org.scalatest.concurrent.Eventually | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.{Await, Promise} | ||
import scala.util.{Failure, Success} | ||
|
||
class WebSocketServerReceiveIdleTimeoutSpec extends AkkaSpecWithMaterializer( | ||
""" | ||
akka { | ||
stream.materializer.debug.fuzzing-mode=off | ||
http.server.websocket.log-frames = on | ||
http.client.websocket.log-frames = on | ||
http.server.websocket.receive-idle-timeout = 1s | ||
} | ||
""") with Eventually { | ||
|
||
"A WebSocket server" must { | ||
|
||
"terminate the handler flow with an error when elements are not received within receive-idle-timeout" in Utils.assertAllStagesStopped { | ||
import system.dispatcher | ||
val handlerTermination = Promise[Done]() | ||
val handler = Flow[Message].map(identity).watchTermination() { (_, terminationFuture) => | ||
terminationFuture.onComplete { | ||
case Success(_) => handlerTermination.trySuccess(Done) | ||
case Failure(exception) => handlerTermination.tryFailure(exception) | ||
} | ||
} | ||
|
||
val bindingFuture = Http().newServerAt("localhost", 0) | ||
.bindSync({ | ||
_.attribute(webSocketUpgrade).get.handleMessages(handler.recover { case ex => { | ||
handlerTermination.failure(ex) | ||
TextMessage("dummy") | ||
} | ||
}, None) | ||
}) | ||
val binding = Await.result(bindingFuture, 3.seconds.dilated) | ||
val myPort = binding.localAddress.getPort | ||
|
||
Source.maybe.via(Http().webSocketClientFlow(WebSocketRequest("ws://127.0.01:" + myPort))).to(Sink.ignore).run() | ||
|
||
handlerTermination.future.failed.futureValue | ||
binding.unbind() | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ttp-core/src/test/scala/akka/http/impl/engine/ws/WebSocketServerSendIdleTimeoutSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package akka.http.impl.engine.ws | ||
|
||
import akka.Done | ||
import akka.http.impl.util.AkkaSpecWithMaterializer | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.model.AttributeKeys.webSocketUpgrade | ||
import akka.http.scaladsl.model.Uri.apply | ||
import akka.http.scaladsl.model.ws._ | ||
import akka.stream.scaladsl._ | ||
import akka.stream.testkit._ | ||
import akka.testkit._ | ||
import org.scalatest.concurrent.Eventually | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.duration.{Duration, DurationInt} | ||
import scala.concurrent.{Await, Future, Promise} | ||
import scala.util.{Failure, Success} | ||
|
||
class WebSocketServerSendIdleTimeoutSpec extends AkkaSpecWithMaterializer( | ||
""" | ||
akka { | ||
stream.materializer.debug.fuzzing-mode=off | ||
http.server.websocket.log-frames = on | ||
http.client.websocket.log-frames = on | ||
http.server.websocket.send-idle-timeout = 1s | ||
} | ||
""") with Eventually { | ||
|
||
"A WebSocket server" must { | ||
|
||
"terminate the handler flow with an error when elements are not sent within send-idle-timeout" in Utils.assertAllStagesStopped { | ||
import system.dispatcher | ||
val handlerTermination = Promise[Done]() | ||
val handler = Flow[Message].map(identity).watchTermination() { (_, terminationFuture) => | ||
terminationFuture.onComplete { | ||
case Success(_) => handlerTermination.trySuccess(Done) | ||
case Failure(exception) => handlerTermination.tryFailure(exception) | ||
} | ||
} | ||
|
||
val bindingFuture = Http().newServerAt("localhost", 0) | ||
.bindSync({ | ||
_.attribute(webSocketUpgrade).get.handleMessages(handler.recover { case ex => { | ||
handlerTermination.failure(ex) | ||
TextMessage("dummy") | ||
} | ||
}, None) | ||
}) | ||
val binding = Await.result(bindingFuture, 3.seconds.dilated) | ||
val myPort = binding.localAddress.getPort | ||
|
||
Source(1 to 10).map(_ => { | ||
Await.result(Future(Thread.sleep(200)), Duration(3, TimeUnit.SECONDS)) | ||
TextMessage("dummy") | ||
}) | ||
.via(Http().webSocketClientFlow(WebSocketRequest("ws://127.0.01:" + myPort))).to(Sink.ignore).run() | ||
|
||
handlerTermination.future.failed.futureValue | ||
binding.unbind() | ||
} | ||
} | ||
} |