-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88cf93
commit bedeebc
Showing
7 changed files
with
168 additions
and
27 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
53 changes: 53 additions & 0 deletions
53
...rc/main/scala/com/wavesplatform/dex/grpc/integration/tool/RestartableManagedChannel.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,53 @@ | ||
package com.wavesplatform.dex.grpc.integration.tool | ||
|
||
import io.grpc.ManagedChannel | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.duration.Duration | ||
|
||
final class RestartableManagedChannel(mkManagedChannel: () => ManagedChannel) { | ||
|
||
private var channel: ManagedChannel = _ | ||
private var isClosed: Boolean = false | ||
|
||
def stop(): Unit = synchronized { | ||
checkIsClosed() | ||
if (channel != null) { | ||
channel.shutdown() | ||
channel = null | ||
} | ||
} | ||
|
||
def restart(): Unit = synchronized { | ||
checkIsClosed() | ||
if (channel != null) | ||
channel.shutdown() | ||
channel = mkManagedChannel() | ||
} | ||
|
||
def getChannel: ManagedChannel = synchronized { | ||
checkIsClosed() | ||
if (channel == null) | ||
channel = mkManagedChannel() | ||
channel | ||
} | ||
|
||
def shutdown(awaitTime: Duration): Unit = synchronized { | ||
mkClosed() | ||
if (channel != null) { | ||
channel.shutdown() | ||
channel.awaitTermination(awaitTime.toMillis, TimeUnit.MILLISECONDS) | ||
channel = null | ||
} | ||
} | ||
|
||
private def checkIsClosed(): Unit = | ||
if (isClosed) | ||
throw new RuntimeException("managed channel is closed") | ||
|
||
private def mkClosed(): Unit = { | ||
checkIsClosed() | ||
isClosed = true | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...st/scala/com/wavesplatform/dex/grpc/integration/tool/RestartableManagedChannelSuite.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,80 @@ | ||
package com.wavesplatform.dex.grpc.integration.tool | ||
|
||
import com.wavesplatform.dex.WavesIntegrationSuiteBase | ||
import io.grpc.ManagedChannel | ||
import org.scalamock.scalatest.MockFactory | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.duration._ | ||
|
||
class RestartableManagedChannelSuite extends WavesIntegrationSuiteBase with MockFactory { | ||
|
||
"RestartableManagedChannel should" - { | ||
|
||
"getChannel" in { | ||
val channel = mock[ManagedChannel] | ||
val maker = mockFunction[ManagedChannel] | ||
maker.expects().returning(channel).once() | ||
val restartableManagedChannel = new RestartableManagedChannel(maker) | ||
restartableManagedChannel.getChannel shouldBe channel | ||
} | ||
|
||
"shutdown" in testShutdown { (awaitTime, restartableManagedChannel) => | ||
restartableManagedChannel.shutdown(awaitTime) | ||
} | ||
|
||
"not do any ops after shutting down" in testShutdown { (awaitTime, restartableManagedChannel) => | ||
restartableManagedChannel.shutdown(awaitTime) | ||
intercept[RuntimeException](restartableManagedChannel.restart()) | ||
intercept[RuntimeException](restartableManagedChannel.getChannel) | ||
intercept[RuntimeException](restartableManagedChannel.shutdown(awaitTime)) | ||
} | ||
|
||
"restart without triggering getChannel" in { | ||
val channel = mock[ManagedChannel] | ||
val maker = mockFunction[ManagedChannel] | ||
maker.expects().returning(channel).once() | ||
val restartableManagedChannel = new RestartableManagedChannel(maker) | ||
restartableManagedChannel.restart() | ||
restartableManagedChannel.getChannel shouldBe channel | ||
} | ||
|
||
"restart" in { | ||
val channel1 = mock[ManagedChannel] | ||
val channel2 = mock[ManagedChannel] | ||
val maker = mockFunction[ManagedChannel] | ||
maker.expects().returning(channel1).once() | ||
maker.expects().returning(channel2).once() | ||
(channel1.shutdown _).expects().returning(channel1).once() | ||
val restartableManagedChannel = new RestartableManagedChannel(maker) | ||
restartableManagedChannel.getChannel //force channel creation | ||
restartableManagedChannel.restart() | ||
} | ||
|
||
"stop current channel" in { | ||
val channel = mock[ManagedChannel] | ||
(channel.shutdown _).expects().returning(channel).once() | ||
val maker = mockFunction[ManagedChannel] | ||
maker.expects().returning(channel).once() | ||
val restartableManagedChannel = new RestartableManagedChannel(maker) | ||
restartableManagedChannel.getChannel //force channel creation | ||
restartableManagedChannel.stop() | ||
} | ||
} | ||
|
||
private def testShutdown(f: (Duration, RestartableManagedChannel) => Unit): Unit = { | ||
val awaitTime = 10.seconds | ||
val channel = mock[ManagedChannel] | ||
(channel.shutdown _).expects().returning(channel).once() | ||
(channel.awaitTermination(_: Long, _: TimeUnit)) | ||
.expects(awaitTime.toMillis, TimeUnit.MILLISECONDS) | ||
.returning(true) | ||
.once() | ||
val maker = mockFunction[ManagedChannel] | ||
maker.expects().returning(channel).once() | ||
val restartableManagedChannel = new RestartableManagedChannel(maker) | ||
restartableManagedChannel.getChannel //force channel creation | ||
f(awaitTime, restartableManagedChannel) | ||
} | ||
|
||
} |