java.net.ServerSocket Scala Examples
The following examples show how to use java.net.ServerSocket.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: FlumeStreamSuite.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import scala.collection.JavaConversions._ import scala.collection.mutable.{ArrayBuffer, SynchronizedBuffer} import scala.concurrent.duration._ import scala.language.postfixOps import com.google.common.base.Charsets import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroFlumeEvent, AvroSourceProtocol} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression._ import org.scalatest.{BeforeAndAfter, Matchers} import org.scalatest.concurrent.Eventually._ import org.apache.spark.{Logging, SparkConf, SparkFunSuite} import org.apache.spark.storage.StorageLevel import org.apache.spark.streaming.{Milliseconds, StreamingContext, TestOutputStream} import org.apache.spark.util.Utils class FlumeStreamSuite extends SparkFunSuite with BeforeAndAfter with Matchers with Logging { val conf = new SparkConf().setMaster("local[4]").setAppName("FlumeStreamSuite") var ssc: StreamingContext = null var transceiver: NettyTransceiver = null after { if (ssc != null) { ssc.stop() } if (transceiver != null) { transceiver.close() } } test("flume input stream") { testFlumeStream(testCompression = false) } test("flume input compressed stream") { testFlumeStream(testCompression = true) } private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 2
Source File: SingleServiceSpec.scala From akka-http-spring-boot with Apache License 2.0 | 5 votes |
package com.github.scalaspring.akka.http import java.net.ServerSocket import akka.http.scaladsl.client.RequestBuilding._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.http.scaladsl.unmarshalling.Unmarshal import com.github.scalaspring.scalatest.TestContextManagement import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.ScalaFutures import org.scalatest.{FlatSpec, Matchers} import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.SpringApplicationContextLoader import org.springframework.context.annotation.{Bean, Import} import org.springframework.test.context.ContextConfiguration import resource._ import scala.concurrent.duration._ @ContextConfiguration( loader = classOf[SpringApplicationContextLoader], classes = Array(classOf[SingleServiceSpec.Configuration]) ) class SingleServiceSpec extends FlatSpec with TestContextManagement with AkkaStreamsAutowiredImplicits with Matchers with ScalaFutures with StrictLogging { implicit val patience = PatienceConfig(10.seconds) // Allow time for server startup @Autowired val settings: ServerSettings = null @Autowired val client: HttpClient = null "Echo service" should "echo" in { val name = "name" val future = client.request(Get(s"http://${settings.interface}:${settings.port}/single/echo/$name")) whenReady(future) { response => //logger.info(s"""received response "$response"""") response.status shouldBe OK whenReady(Unmarshal(response.entity).to[String])(_ shouldBe name) } } } object SingleServiceSpec { @Configuration @Import(Array(classOf[AkkaHttpServerAutoConfiguration])) class Configuration extends AkkaHttpServer with EchoService { @Bean def serverSettings = new ServerSettings(port = managed(new ServerSocket(0)).map(_.getLocalPort).opt.get) } trait EchoService extends AkkaHttpService { abstract override def route: Route = { get { path("single"/ "echo" / Segment) { name => complete(name) } } } ~ super.route } }
Example 3
Source File: MultiServiceSpec.scala From akka-http-spring-boot with Apache License 2.0 | 5 votes |
package com.github.scalaspring.akka.http import java.net.ServerSocket import akka.http.scaladsl.client.RequestBuilding._ import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ import akka.http.scaladsl.unmarshalling.Unmarshal import com.github.scalaspring.scalatest.TestContextManagement import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.ScalaFutures import org.scalatest.{FlatSpec, Matchers} import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.SpringApplicationContextLoader import org.springframework.context.annotation.{Bean, Import} import org.springframework.test.context.ContextConfiguration import resource._ import scala.concurrent.duration._ @ContextConfiguration( loader = classOf[SpringApplicationContextLoader], classes = Array(classOf[MultiServiceSpec.Configuration]) ) class MultiServiceSpec extends FlatSpec with TestContextManagement with AkkaStreamsAutowiredImplicits with Matchers with ScalaFutures with StrictLogging { implicit val patience = PatienceConfig((10.seconds)) // Allow time for server startup @Autowired val settings: ServerSettings = null @Autowired val client: HttpClient = null "Echo service" should "echo" in { val name = "name" val future = client.request(Get(s"http://${settings.interface}:${settings.port}/multi/echo/$name")) whenReady(future) { response => response.status shouldBe OK whenReady(Unmarshal(response.entity).to[String])(_ shouldBe name) } } "Reverse service" should "reverse" in { val name = "name" val future = client.request(Get(s"http://${settings.interface}:${settings.port}/multi/reverse/$name")) whenReady(future) { response => response.status shouldBe OK whenReady(Unmarshal(response.entity).to[String])(_ shouldBe name.reverse) } } } object MultiServiceSpec { @Configuration @Import(Array(classOf[AkkaHttpServerAutoConfiguration])) class Configuration extends AkkaHttpServer with EchoService with ReverseService { @Bean def serverSettings = new ServerSettings(port = managed(new ServerSocket(0)).map(_.getLocalPort).opt.get) } trait EchoService extends AkkaHttpService { abstract override def route: Route = { (get & path("multi"/"echo"/Segment)) { name => complete(name) } ~ super.route } } trait ReverseService extends AkkaHttpService { abstract override def route: Route = { (get & path("multi"/"reverse"/Segment)) { name => complete(name.reverse) } } ~ super.route } }
Example 4
Source File: RawTextSender.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.{SparkConf, Logging} import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match //解析使用模式匹配的参数 val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer //多次重复输入数据以填充缓冲区 val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 5
Source File: MQTTTestUtils.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.mqtt import java.net.{ServerSocket, URI} import scala.language.postfixOps import com.google.common.base.Charsets.UTF_8 import org.apache.activemq.broker.{BrokerService, TransportConnector} import org.apache.commons.lang3.RandomUtils import org.eclipse.paho.client.mqttv3._ import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence import org.apache.spark.util.Utils import org.apache.spark.{Logging, SparkConf} private[mqtt] class MQTTTestUtils extends Logging { private val persistenceDir = Utils.createTempDir() private val brokerHost = "localhost" private val brokerPort = findFreePort() private var broker: BrokerService = _ private var connector: TransportConnector = _ def brokerUri: String = { s"$brokerHost:$brokerPort" } def setup(): Unit = { broker = new BrokerService() broker.setDataDirectoryFile(Utils.createTempDir()) connector = new TransportConnector() connector.setName("mqtt") connector.setUri(new URI("mqtt://" + brokerUri)) broker.addConnector(connector) broker.start() } def teardown(): Unit = { if (broker != null) { broker.stop() broker = null } if (connector != null) { connector.stop() connector = null } Utils.deleteRecursively(persistenceDir) } private def findFreePort(): Int = { val candidatePort = RandomUtils.nextInt(1024, 65536) Utils.startServiceOnPort(candidatePort, (trialPort: Int) => { val socket = new ServerSocket(trialPort) socket.close() (null, trialPort) }, new SparkConf())._2 } def publishData(topic: String, data: String): Unit = { var client: MqttClient = null try { val persistence = new MqttDefaultFilePersistence(persistenceDir.getAbsolutePath) client = new MqttClient("tcp://" + brokerUri, MqttClient.generateClientId(), persistence) client.connect() if (client.isConnected) { val msgTopic = client.getTopic(topic) val message = new MqttMessage(data.getBytes(UTF_8)) message.setQos(1) message.setRetained(true) for (i <- 0 to 10) { try { msgTopic.publish(message) } catch { case e: MqttException if e.getReasonCode == MqttException.REASON_CODE_MAX_INFLIGHT => // wait for Spark streaming to consume something from the message queue Thread.sleep(50) } } } } finally { if (client != null) { client.disconnect() client.close() client = null } } } }
Example 6
Source File: FlumeTestUtils.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.util.{List => JList} import scala.collection.JavaConversions._ import com.google.common.base.Charsets.UTF_8 import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroSourceProtocol, AvroFlumeEvent} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.util.Utils import org.apache.spark.SparkConf private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 7
Source File: PageViewGenerator.scala From spark1.52 with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.net.ServerSocket import java.io.PrintWriter import util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01) : _*) def pickFromDistribution[T](inputMap : Map[T, Double]) : T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent() : String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args : Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 8
Source File: AttachingDebuggerIntegrationSpec.scala From scala-debugger with Apache License 2.0 | 5 votes |
package org.scaladebugger.api.debuggers import java.net.ServerSocket import java.util.concurrent.atomic.AtomicBoolean import org.scaladebugger.api.utils.JDITools import org.scaladebugger.test.helpers.ParallelMockFunSpec import test.{ApiTestUtilities, VirtualMachineFixtures} import scala.util.Try class AttachingDebuggerIntegrationSpec extends ParallelMockFunSpec with VirtualMachineFixtures with ApiTestUtilities { describe("AttachingDebugger") { it("should be able to attach to a running JVM process") { withProcess((port, process) => { val attachingDebugger = AttachingDebugger(port) val attachedToVirtualMachine = new AtomicBoolean(false) // Need to keep retrying until process is ready to be attached to eventually { attachingDebugger.start(_ => attachedToVirtualMachine.set(true)) } // Keep checking back until we have successfully attached eventually { attachedToVirtualMachine.get() should be (true) } }) } } private def withProcess[T](testCode: (Int, Process) => T): T = { val jvmProcess = createProcess() val result = Try(testCode(jvmProcess._1, jvmProcess._2)) destroyProcess(jvmProcess._2) result.get } private def createProcess(): (Int, Process) = { val port = { val socket = new ServerSocket(0) val _port = socket.getLocalPort socket.close() _port } (port, JDITools.spawn( className = "org.scaladebugger.test.misc.AttachingMain", server = true, suspend = true, port = port )) } private def destroyProcess(process: Process): Unit = process.destroy() }
Example 9
Source File: ListeningDebuggerIntegrationSpec.scala From scala-debugger with Apache License 2.0 | 5 votes |
package org.scaladebugger.api.debuggers import java.net.ServerSocket import java.util.concurrent.atomic.AtomicInteger import org.scaladebugger.api.utils.JDITools import org.scaladebugger.test.helpers.ParallelMockFunSpec import test.{ApiTestUtilities, VirtualMachineFixtures} import scala.util.Try class ListeningDebuggerIntegrationSpec extends ParallelMockFunSpec with VirtualMachineFixtures with ApiTestUtilities { describe("ListeningDebugger") { it("should be able to listen for multiple connecting JVM processes") { withProcessCreator((address, port, createProcess) => { val totalJvmProcesses = 3 val currentConnectedCount = new AtomicInteger(0) // Start listening for JVM connections val listeningDebugger = ListeningDebugger(hostname = address, port = port) listeningDebugger.start(_ => currentConnectedCount.incrementAndGet()) // Verify that our listening debugger can actually support multiple // connections (it should as a socket listener) if (!listeningDebugger.supportsMultipleConnections) { alert( "Listening debuggers do not support multiple connections on this JVM!" ) } // Spawn our JVM processes (1 to totalJvmProcesses).foreach(_ => createProcess()) // Keep checking back until we have successfully connected all JVMs eventually { currentConnectedCount.get() should be (totalJvmProcesses) } }) } } private def withProcessCreator[T](testCode: (String, Int, () => Process) => T): T = { val (address, port) = { val socket = new ServerSocket(0) val _address = socket.getInetAddress.getHostName val _port = socket.getLocalPort socket.close() (_address, _port) } var jvmProcesses: Seq[Process] = Nil def createProcess(port: Int): Process = { val process = JDITools.spawn( className = "org.scaladebugger.test.misc.ListeningMain", server = false, suspend = true, port = port ) jvmProcesses +:= process process } val result = Try(testCode(address, port, () => createProcess(port))) // Clean up any leftover processes jvmProcesses.foreach(p => Try(p.destroy())) result.get } }
Example 10
Source File: UISuite.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.ui import java.net.ServerSocket import scala.io.Source import scala.util.{Failure, Success, Try} import org.eclipse.jetty.servlet.ServletContextHandler import org.scalatest.concurrent.Eventually._ import org.scalatest.time.SpanSugar._ import org.apache.spark.LocalSparkContext._ import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite} class UISuite extends SparkFunSuite { private def newSparkContext(): SparkContext = { val conf = new SparkConf() .setMaster("local") .setAppName("test") .set("spark.ui.enabled", "true") val sc = new SparkContext(conf) assert(sc.ui.isDefined) sc } ignore("basic ui visibility") { withSpark(newSparkContext()) { sc => // test if the ui is visible, and all the expected tabs are visible eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL(sc.ui.get.appUIAddress).mkString assert(!html.contains("random data that should not be present")) assert(html.toLowerCase.contains("stages")) assert(html.toLowerCase.contains("storage")) assert(html.toLowerCase.contains("environment")) assert(html.toLowerCase.contains("executors")) } } } ignore("visibility at localhost:4040") { withSpark(newSparkContext()) { sc => // test if visible from http://localhost:4040 eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL("http://localhost:4040").mkString assert(html.toLowerCase.contains("stages")) } } } test("jetty selects different port under contention") { val server = new ServerSocket(0) val startPort = server.getLocalPort val serverInfo1 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) val serverInfo2 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) // Allow some wiggle room in case ports on the machine are under contention val boundPort1 = serverInfo1.boundPort val boundPort2 = serverInfo2.boundPort assert(boundPort1 != startPort) assert(boundPort2 != startPort) assert(boundPort1 != boundPort2) serverInfo1.server.stop() serverInfo2.server.stop() server.close() } test("jetty binds to port 0 correctly") { val serverInfo = JettyUtils.startJettyServer( "0.0.0.0", 0, Seq[ServletContextHandler](), new SparkConf) val server = serverInfo.server val boundPort = serverInfo.boundPort assert(server.getState === "STARTED") assert(boundPort != 0) Try { new ServerSocket(boundPort) } match { case Success(s) => fail("Port %s doesn't seem used by jetty server".format(boundPort)) case Failure(e) => } } test("verify appUIAddress contains the scheme") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val uiAddress = ui.appUIAddress val uiHostPort = ui.appUIHostPort assert(uiAddress.equals("http://" + uiHostPort)) } } test("verify appUIAddress contains the port") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val splitUIAddress = ui.appUIAddress.split(':') val boundPort = ui.boundPort assert(splitUIAddress(2).toInt == boundPort) } } }
Example 11
Source File: RawTextSender.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.{SparkConf, Logging} import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 12
Source File: PageViewGenerator.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.io.PrintWriter import java.net.ServerSocket import java.util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01): _*) def pickFromDistribution[T](inputMap: Map[T, Double]): T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent(): String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args: Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println(s"Listening on port: $port") while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println(s"Got client connected from: ${socket.getInetAddress}") val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 13
Source File: PageViewGenerator.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.examples.streaming.clickstream import java.net.ServerSocket import java.io.PrintWriter import util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01) : _*) def pickFromDistribution[T](inputMap : Map[T, Double]) : T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent() : String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args : Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } }
Example 14
Source File: GraphiteEndpointSinkTest.scala From kafka-lag-exporter with Apache License 2.0 | 5 votes |
package com.lightbend.kafkalagexporter import org.scalatest._ class GraphiteEndpointSinkTest extends fixture.FreeSpec with Matchers { class GraphiteServer extends Thread { import java.net.ServerSocket import java.io._ val server = new ServerSocket(0) var line = "" override def run() : Unit = { val connection = server.accept val input = new BufferedReader(new InputStreamReader(connection.getInputStream)) line = input.readLine() server.close(); } } case class Fixture(server: GraphiteServer) type FixtureParam = Fixture override def withFixture(test: OneArgTest): Outcome = { val server = new GraphiteServer() server.start() test(Fixture(server)) } "GraphiteEndpointSinkImpl should" - { "report only metrics which match the regex" in { fixture => val sink = GraphiteEndpointSink(List("kafka_consumergroup_group_max_lag"), Map("cluster" -> Map.empty), Some(GraphiteConfig("localhost", fixture.server.server.getLocalPort(), None))) sink.report(Metrics.GroupValueMessage(Metrics.MaxGroupOffsetLagMetric, "cluster", "group", 100)) sink.report(Metrics.GroupValueMessage(Metrics.MaxGroupTimeLagMetric, "cluster", "group", 1)) fixture.server.join() fixture.server.line should startWith ("cluster.group.kafka_consumergroup_group_max_lag 100.0 ") } } }
Example 15
Source File: RawTextSender.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 16
Source File: FlumeTestUtils.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.{List => JList} import java.util.Collections import scala.collection.JavaConverters._ import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroFlumeEvent, AvroSourceProtocol} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.util.Utils import org.apache.spark.SparkConf private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 17
Source File: PageViewGenerator.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.io.PrintWriter import java.net.ServerSocket import java.util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01): _*) def pickFromDistribution[T](inputMap: Map[T, Double]): T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent(): String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args: Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 18
Source File: WireMockUtils.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.audit import java.io.IOException import java.net.ServerSocket object WireMockUtils { def availablePort: Int = { var port = 9876 var socket: ServerSocket = null try { socket = new ServerSocket(0) port = socket.getLocalPort } catch { case ex: IOException => } finally { if (socket != null) { try { socket.close() } catch { case ex: IOException => } } } port } }
Example 19
Source File: FreePortFinder.scala From spark-druid-olap with Apache License 2.0 | 5 votes |
package org.sparklinedata.druid.testenv import java.net.ServerSocket import scala.collection.mutable.ArrayBuffer trait FreePortFinder { val MIN_PORT_NUMBER = 8000 val MAX_PORT_NUMBER = 49151 def findFreePorts(numPorts: Int = 1): List[Int] = { val freePorts = ArrayBuffer[Int]() (MIN_PORT_NUMBER until MAX_PORT_NUMBER).foreach { i => if (available(i)) { freePorts += i } if (freePorts.size == numPorts) { return freePorts.toList } } throw new RuntimeException(s"Could not find $numPorts ports between " + MIN_PORT_NUMBER + " and " + MAX_PORT_NUMBER) } def available(port: Int): Boolean = { var serverSocket: Option[ServerSocket] = None try { serverSocket = Some(new ServerSocket(port)) serverSocket.get.setReuseAddress(true); return true } catch { case _: Throwable => return false } finally { serverSocket.map(_.close()) } } }
Example 20
Source File: WebappTestSupports.scala From pizza-auth-3 with MIT License | 5 votes |
package moe.pizza.auth.webapp import java.net.{Socket, InetSocketAddress, ServerSocket} import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import com.fasterxml.jackson.module.scala.DefaultScalaModule import moe.pizza.auth.config.ConfigFile.ConfigFile import scala.concurrent.{Future, Await} import scala.io.Source import scala.util.Try import scala.concurrent.duration._ import scala.concurrent.ExecutionContext.Implicits.global object WebappTestSupports { val OM = new ObjectMapper(new YAMLFactory()) OM.registerModule(DefaultScalaModule) def readTestConfig(): ConfigFile = { val config = Source .fromURL(getClass.getResource("/config.yml")) .getLines() .mkString("\n") val conf = OM.readValue[ConfigFile](config, classOf[ConfigFile]) conf } }
Example 21
Source File: MQTTTestUtils.scala From bahir with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.mqtt import java.net.{ServerSocket, URI} import java.nio.charset.StandardCharsets import scala.language.postfixOps import org.apache.activemq.broker.{BrokerService, TransportConnector} import org.apache.activemq.usage.SystemUsage import org.apache.commons.lang3.RandomUtils import org.eclipse.paho.client.mqttv3._ import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.util.Utils private[mqtt] class MQTTTestUtils extends Logging { private val persistenceDir = Utils.createTempDir() private val brokerHost = "localhost" private val brokerPort = findFreePort() private var broker: BrokerService = _ private var systemUsage: SystemUsage = _ private var connector: TransportConnector = _ def brokerUri: String = { s"$brokerHost:$brokerPort" } def setup(): Unit = { broker = new BrokerService() broker.setDataDirectoryFile(Utils.createTempDir()) broker.getSystemUsage().setSendFailIfNoSpace(false) systemUsage = broker.getSystemUsage() systemUsage.getStoreUsage().setLimit(1024L * 1024 * 256); // 256 MB (default: 100 GB) systemUsage.getTempUsage().setLimit(1024L * 1024 * 128); // 128 MB (default: 50 GB) connector = new TransportConnector() connector.setName("mqtt") connector.setUri(new URI("mqtt://" + brokerUri)) broker.addConnector(connector) broker.start() } def teardown(): Unit = { if (broker != null) { broker.stop() broker = null } if (connector != null) { connector.stop() connector = null } Utils.deleteRecursively(persistenceDir) } private def findFreePort(): Int = { val candidatePort = RandomUtils.nextInt(1024, 65536) Utils.startServiceOnPort(candidatePort, (trialPort: Int) => { val socket = new ServerSocket(trialPort) socket.close() (null, trialPort) }, new SparkConf())._2 } def publishData(topic: String, data: String): Unit = { var client: MqttClient = null try { val persistence = new MqttDefaultFilePersistence(persistenceDir.getAbsolutePath) client = new MqttClient("tcp://" + brokerUri, MqttClient.generateClientId(), persistence) client.connect() if (client.isConnected) { val msgTopic = client.getTopic(topic) val message = new MqttMessage(data.getBytes(StandardCharsets.UTF_8)) message.setQos(1) message.setRetained(true) for (i <- 0 to 10) { try { msgTopic.publish(message) } catch { case e: MqttException if e.getReasonCode == MqttException.REASON_CODE_MAX_INFLIGHT => // wait for Spark streaming to consume something from the message queue Thread.sleep(50) } } } } finally { if (client != null) { client.disconnect() client.close() client = null } } } }
Example 22
Source File: ServiceSpec.scala From play-soap with Apache License 2.0 | 5 votes |
package play.soap.sbtplugin.tester import java.net.ServerSocket import java.util.concurrent.atomic.AtomicBoolean import javax.xml.ws.Endpoint import javax.xml.ws.handler.soap._ import javax.xml.ws.handler.MessageContext import org.apache.cxf.jaxws.EndpointImpl import play.soap.testservice.client._ import scala.collection.JavaConverters._ import scala.concurrent.{Await, Future} import scala.concurrent.duration._ import scala.reflect.ClassTag import play.api.test._ import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder abstract class ServiceSpec extends PlaySpecification { val servicePath: String def await[T](future: Future[T]): T = Await.result(future, 10.seconds) def withClient[T](block: Service => T): T = withApp { app => val client = app.injector.instanceOf[ServiceClient] val service = getServiceFromClient(client) block(service) } def withApp[T](block: Application => T): T = withService { port => implicit val app = new GuiceApplicationBuilder() .configure("play.soap.address" -> s"http://localhost:$port/$servicePath") .build Helpers.running(app) { block(app) } } def withService[T](block: Int => T): T = { val port = findAvailablePort() val impl = createServiceImpl() val endpoint = Endpoint.publish(s"http://localhost:$port/$servicePath", impl) try { block(port) } finally { endpoint.stop() // Need to shutdown whole engine. Note, Jetty's shutdown doesn't seem to happen synchronously, have to wait // a few seconds for the port to be released. This is why we use a different port each time. endpoint.asInstanceOf[EndpointImpl].getBus.shutdown(true) } } def findAvailablePort() = { val socket = new ServerSocket(0) try { socket.getLocalPort } finally { socket.close() } } }
Example 23
Source File: TCPListener.scala From scala-loci with Apache License 2.0 | 5 votes |
package loci package communicator package tcp import java.io.IOException import java.net.{InetAddress, ServerSocket, SocketException} import java.util.concurrent.Executors import java.util.concurrent.atomic.AtomicBoolean import scala.util.{Failure, Success, Try} import scala.util.control.NonFatal private class TCPListener( port: Int, interface: String, properties: TCP.Properties) extends Listener[TCP] { protected def startListening(connectionEstablished: Connected[TCP]): Try[Listening] = try { val running = new AtomicBoolean(true) val socket = new ServerSocket(port, 0, InetAddress.getByName(interface)) val executor = Executors.newCachedThreadPool() def terminate() = { try socket.close() catch { case _: IOException => } executor.shutdown() } new Thread() { override def run() = try while (true) { val connection = socket.accept() if (connection != null) executor.execute(new Runnable { def run() = TCPHandler.handleConnection( connection, properties, TCPListener.this, { connection => connectionEstablished.fire(Success(connection)) }) }) } catch { case exception: SocketException => if (running.getAndSet(false)) { terminate() connectionEstablished.fire(Failure(exception)) } } }.start() Success(new Listening { def stopListening(): Unit = if (running.getAndSet(false)) terminate() }) } catch { case NonFatal(exception) => Failure(exception) } }
Example 24
Source File: CountStreamProducer.scala From Apache-Spark-2x-Machine-Learning-Cookbook with MIT License | 5 votes |
package spark.ml.cookbook.chapter13 import java.io.{BufferedOutputStream, PrintWriter} import java.net.Socket import java.net.ServerSocket import scala.util.Random class CountSreamThread(socket: Socket) extends Thread { val villians = Array("Bane", "Thanos", "Loki", "Apocalypse", "Red Skull", "The Governor", "Sinestro", "Galactus", "Doctor Doom", "Lex Luthor", "Joker", "Magneto", "Darth Vader") override def run(): Unit = { println("Connection accepted") val out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream())) println("Producing Data") while (true) { out.println(villians(Random.nextInt(villians.size))) Thread.sleep(10) } println("Done Producing") } } object CountStreamProducer { def main(args: Array[String]): Unit = { val ss = new ServerSocket(9999) while (true) { println("Accepting Connection...") new CountSreamThread(ss.accept()).start() } } }
Example 25
Source File: system_channel.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{InputStream, OutputStream} import java.net.{ServerSocket, InetAddress} object System_Channel { def apply(): System_Channel = new System_Channel } class System_Channel private { private val server = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")) val server_name: String = "127.0.0.1:" + server.getLocalPort override def toString: String = server_name def rendezvous(): (OutputStream, InputStream) = { val socket = server.accept socket.setTcpNoDelay(true) (socket.getOutputStream, socket.getInputStream) } def accepted() { server.close } }
Example 26
Source File: system_channel.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{InputStream, OutputStream} import java.net.{ServerSocket, InetAddress} object System_Channel { def apply(): System_Channel = new System_Channel } class System_Channel private { private val server = new ServerSocket(0, 50, Server.localhost) val address: String = Server.print_address(server.getLocalPort) val password: String = UUID.random().toString override def toString: String = address def shutdown() { server.close } def rendezvous(): (OutputStream, InputStream) = { val socket = server.accept try { val out_stream = socket.getOutputStream val in_stream = socket.getInputStream if (Byte_Message.read_line(in_stream).map(_.text) == Some(password)) (out_stream, in_stream) else { out_stream.close in_stream.close error("Failed to connect system channel: bad password") } } finally { shutdown() } } }
Example 27
Source File: system_channel.scala From libisabelle with Apache License 2.0 | 5 votes |
package isabelle import java.io.{InputStream, OutputStream} import java.net.{ServerSocket, InetAddress} object System_Channel { def apply(): System_Channel = new System_Channel } class System_Channel private { private val server = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")) val server_name: String = "127.0.0.1:" + server.getLocalPort override def toString: String = server_name def rendezvous(): (OutputStream, InputStream) = { val socket = server.accept socket.setTcpNoDelay(true) (socket.getOutputStream, socket.getInputStream) } def accepted() { server.close } }
Example 28
Source File: PortProvider.scala From ksql-streams with Apache License 2.0 | 5 votes |
package com.landoop.kstreams.sql.cluster import java.net.{InetAddress, ServerSocket} object PortProvider { def appy(count: Int): Vector[Int] = { (1 to count).map { _ => val serverSocket = new ServerSocket(0, 0, InetAddress.getLocalHost) val port = serverSocket.getLocalPort serverSocket.close() port }.toVector } def one: Int = appy(1).head }
Example 29
Source File: SchemaRegistryOps.scala From embedded-kafka-schema-registry with MIT License | 5 votes |
package net.manub.embeddedkafka.schemaregistry.ops import java.net.{ServerSocket, URI} import java.util.Properties import io.confluent.kafka.schemaregistry.rest.{ SchemaRegistryConfig, SchemaRegistryRestApplication } import io.confluent.rest.RestConfig import net.manub.embeddedkafka.EmbeddedServer import net.manub.embeddedkafka.ops.RunningServersOps import net.manub.embeddedkafka.schemaregistry.{EmbeddedKafkaConfig, EmbeddedSR} import scala.jdk.CollectionConverters._ def stopSchemaRegistry(): Unit = runningServers.stopAndRemove(isEmbeddedSR) private[embeddedkafka] def isEmbeddedSR(server: EmbeddedServer): Boolean = server.isInstanceOf[EmbeddedSR] private[embeddedkafka] def schemaRegistryPort( restApp: SchemaRegistryRestApplication ): Int = { val listeners = restApp.getConfiguration.originalProperties .getProperty(RestConfig.LISTENERS_CONFIG) URI.create(listeners).getPort } }
Example 30
Source File: PortProvider.scala From kafka-testing with Apache License 2.0 | 5 votes |
package com.landoop.kafka.testing import java.net.{InetAddress, ServerSocket} object PortProvider { def appy(count: Int): Vector[Int] = { (1 to count).map { _ => val serverSocket = new ServerSocket(0, 0, InetAddress.getLocalHost) val port = serverSocket.getLocalPort serverSocket.close() port }.toVector } def one: Int = appy(1).head }
Example 31
Source File: ConnectionParameters.scala From almond with BSD 3-Clause "New" or "Revised" License | 5 votes |
package almond.channels import java.net.ServerSocket import almond.channels.zeromq.{ZeromqConnection, ZeromqThreads} import almond.logger.LoggerContext import almond.util.Secret import cats.effect.IO final case class ConnectionParameters( ip: String, transport: String, stdin_port: Int, control_port: Int, hb_port: Int, shell_port: Int, iopub_port: Int, key: Secret[String], signature_scheme: Option[String], kernel_name: Option[String] = None // jupyter seems to add this ) { def uri(channel: Channel): String = { val port = channel match { case Channel.Requests => shell_port case Channel.Control => control_port case Channel.Publish => iopub_port case Channel.Input => stdin_port } s"$transport://$ip:$port" } def heartbeatUri: String = s"$transport://$ip:$hb_port" def channels( bind: Boolean, threads: ZeromqThreads, logCtx: LoggerContext, identityOpt: Option[String] = None ): IO[ZeromqConnection] = ZeromqConnection(this, bind, identityOpt, threads, logCtx) } object ConnectionParameters { def randomPort(): Int = { val s = new ServerSocket(0) val port = s.getLocalPort s.close() port } def randomLocal(): ConnectionParameters = ConnectionParameters( "localhost", "tcp", randomPort(), randomPort(), randomPort(), randomPort(), randomPort(), Secret.randomUuid(), Some("hmac-sha256") ) }
Example 32
Source File: IndexerElasticSearchService.scala From elastic-indexer4s with MIT License | 5 votes |
package com.yannick_cw.elastic_indexer4s.specs import java.io.IOException import java.net.ServerSocket import com.sksamuel.elastic4s.http.{ElasticClient, ElasticProperties} import com.sksamuel.elastic4s.testkit.ClientProvider import com.whisk.docker.{DockerContainer, DockerKit, DockerPortMapping, DockerReadyChecker} import scala.annotation.tailrec import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} trait IndexerElasticSearchService extends DockerKit with ClientProvider { val DefaultElasticsearchHttpPort = 9200 val DefaultElasticsearchClientPort = 9300 val ElasticsearchHttpPort = getFreePort val ElasticsearchClientPort = getFreePort override val StartContainersTimeout = 60.seconds override def client: ElasticClient = ElasticClient(ElasticProperties(s"http://localhost:$ElasticsearchHttpPort")) val elasticsearchContainer: DockerContainer = DockerContainer("docker.elastic.co/elasticsearch/elasticsearch:6.4.0") .withPortMapping( DefaultElasticsearchHttpPort -> DockerPortMapping(Some(ElasticsearchHttpPort)), DefaultElasticsearchClientPort -> DockerPortMapping(Some(ElasticsearchClientPort)) ) .withEnv("discovery.type=single-node", "http.host=0.0.0.0", "transport.host=127.0.0.1", "xpack.security.enabled=false") .withReadyChecker( DockerReadyChecker .HttpResponseCode(DefaultElasticsearchHttpPort, "/", Some("0.0.0.0")) .within(100.millis) .looped(20, 1250.millis)) abstract override def dockerContainers: List[DockerContainer] = elasticsearchContainer :: super.dockerContainers @tailrec private final def getFreePort: Int = { Try(new ServerSocket(0)) match { case Success(socket) => val port = socket.getLocalPort socket.close() port case Failure(_: IOException) => getFreePort case Failure(e) => throw e } } }
Example 33
Source File: GraphiteMockServer.scala From kafka-offset-monitor-graphite with Apache License 2.0 | 5 votes |
package pl.allegro.tech.kafka.offset.monitor.graphite import java.io.InputStream import java.lang import java.net.ServerSocket import java.util.concurrent.{Callable, ExecutorService, Executors} import com.jayway.awaitility.Awaitility._ import com.jayway.awaitility.Duration class GraphiteMockServer(port: Int) { var serverSocket: ServerSocket = null val executor: ExecutorService = Executors.newFixedThreadPool(10) @volatile var listen: Boolean = false var expectedMetrics: scala.collection.mutable.Map[String, Double] = scala.collection.mutable.Map() var receivedMetrics: scala.collection.mutable.Map[String, Double] = scala.collection.mutable.Map() def start() { serverSocket = new ServerSocket(port) listen = true handleConnections() } private def handleConnections() { executor.execute(new Runnable { override def run() { while(listen) { readData(serverSocket.accept().getInputStream()) } } }) } private def readData(stream: InputStream) { executor.execute(new Runnable { override def run() { scala.io.Source.fromInputStream(stream).getLines().foreach((line) => handleMetric(line)) } }) } private def handleMetric(metricLine: String) { val metric = metricLine.split(" ")(0) val value = metricLine.split(" ")(1) if(expectedMetrics.contains(metric)) { receivedMetrics += (metric -> value.toDouble) } } def stop() { listen = false serverSocket.close() } def reset() { expectedMetrics.clear() receivedMetrics.clear() } def expectMetric(metricNamePattern: String, value: Double) { expectedMetrics += (metricNamePattern -> value) } def waitUntilReceived() { await.atMost(Duration.FIVE_SECONDS).until(new Callable[lang.Boolean] { override def call(): lang.Boolean = { expectedMetrics.forall { case (k, v) => receivedMetrics.get(k).exists( (rv) => v == rv ) } } }) } }
Example 34
Source File: HelloWorldSpec.scala From play-soap with Apache License 2.0 | 5 votes |
package play.soap.sbtplugin.tester import java.net.ServerSocket import java.util.concurrent.atomic.AtomicBoolean import javax.xml.ws.Endpoint import javax.xml.ws.handler.soap._ import javax.xml.ws.handler.MessageContext import org.apache.cxf.jaxws.EndpointImpl import play.soap.testservice.client._ import scala.collection.JavaConverters._ import scala.concurrent.{Await, Future} import scala.concurrent.duration._ import scala.reflect.ClassTag import play.api.test._ import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder import play.soap.testservice.HelloWorldImpl class HelloWorldSpec extends ServiceSpec { sequential "HelloWorld" should { "say hello" in withClient { client => await(client.sayHello("world")) must_== "Hello world" } "say hello to many people" in withClient { client => await(client.sayHelloToMany(java.util.Arrays.asList("foo", "bar"))).asScala must_== List("Hello foo", "Hello bar") } "say hello to one user" in withClient { client => val user = new User user.setName("world") await(client.sayHelloToUser(user)).getUser.getName must_== "world" } "say hello with an exception" in withClient { client => await(client.sayHelloException("world")) must throwA[HelloException_Exception].like { case e => e.getMessage must_== "Hello world" } } "dont say hello" in withClient { client => await(client.dontSayHello()) must_== ((): Unit) } "allow adding custom handlers" in { val invoked = new AtomicBoolean() withApp { app => val client = app.injector.instanceOf[HelloWorldService].helloWorld(new SOAPHandler[SOAPMessageContext] { def getHeaders = null def handleMessage(context: SOAPMessageContext) = { invoked.set(true) true } def close(context: MessageContext) = () def handleFault(context: SOAPMessageContext) = true }) await(client.sayHello("world")) must_== "Hello world" invoked.get() must_== true } } } override type ServiceClient = HelloWorldService override type Service = HelloWorld override implicit val serviceClientClass: ClassTag[HelloWorldService] = ClassTag(classOf[HelloWorldService]) override def getServiceFromClient(c: ServiceClient): Service = c.helloWorld override def createServiceImpl(): Any = new HelloWorldImpl val servicePath: String = "helloWorld" }
Example 35
Source File: HttpFeatureTest.scala From diffy with GNU Affero General Public License v3.0 | 5 votes |
package ai.diffy import java.net.ServerSocket import ai.diffy.examples.http.ExampleServers import ai.diffy.proxy.DifferenceProxy import com.google.common.collect.ImmutableMap import com.google.inject.Stage import com.twitter.finagle.Http import com.twitter.finagle.http.Status import com.twitter.finagle.util.DefaultTimer import com.twitter.finatra.http.EmbeddedHttpServer import com.twitter.inject.Test import com.twitter.util.{Await, Duration, Future, FuturePool} class HttpFeatureTest extends Test { def getPort(): Int = { val s = new ServerSocket(0) val port = s.getLocalPort s.close() port } val env@Seq(p,s,c,d) = Seq.fill(4)(getPort()) val environment = FuturePool.unboundedPool(ExampleServers.main(env.take(3).map(_.toString).toArray)) val diffy = new MainService lazy val differenceProxy = diffy.injector.instance[DifferenceProxy] val server = new EmbeddedHttpServer( twitterServer = diffy, flags = Map( "proxy.port" -> s":$d", "candidate" -> s"localhost:$c", "master.primary" -> s"localhost:$p", "master.secondary" -> s"localhost:$s", "serviceName" -> "myHttpService", "service.protocol" -> "http", "summary.email" ->"test" ), stage = Stage.PRODUCTION ) test("verify startup") { server.assertHealthy() } test("verify DifferenceCollector") { assert(differenceProxy.collector.fields.isEmpty) Await.result(Http.fetchUrl(s"http://localhost:$d/json?Twitter").liftToTry) var tries = 0 while(differenceProxy.outstandingRequests.get() > 0 && tries < 10) { Await.result(Future.sleep(Duration.fromSeconds(1))(DefaultTimer.twitter)) tries = tries + 1 } assert(!differenceProxy.collector.fields.isEmpty) } test("verify present differences via API") { val response = Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/undefined_endpoint/stats")) assertResult(Status.Ok)(response.status) assert(response.getContentString().contains(""""differences":1""")) } test("verify absent endpoint in API") { val response = Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/json/stats")) assertResult(Status.Ok)(response.status) assertResult("""{"error":"key not found: json"}""")(response.getContentString()) } Seq( "/api/1/overview", "/api/1/report", "/api/1/endpoints", "/api/1/endpoints/json/stats", "/api/1/endpoints/json/fields/result.200.values.value.name.PrimitiveDifference/results", "/api/1/endpoints/json/fields/result.200.values.value.name.PrimitiveDifference/results/0" ) foreach { endpoint => test(s"ping ${endpoint}") { val response = Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}${endpoint}")) assertResult(Status.Ok)(response.status) } } }
Example 36
Source File: ThriftFeatureTest.scala From diffy with GNU Affero General Public License v3.0 | 5 votes |
package ai.diffy import java.io.{File, FileOutputStream} import java.net.ServerSocket import java.nio.file.Files import java.util.zip.{ZipEntry, ZipOutputStream} import ai.diffy.examples.thrift.ExampleServers import ai.diffy.proxy.DifferenceProxy import ai.diffy.thriftscala.Adder import com.google.inject.Stage import com.twitter.finagle.http.Status import com.twitter.finagle.util.DefaultTimer import com.twitter.finagle.{Http, ThriftMux} import com.twitter.finatra.http.EmbeddedHttpServer import com.twitter.inject.Test import com.twitter.util.{Await, Duration, Future, FuturePool} import scala.io.Source class ThriftFeatureTest extends Test { def getPort(): Int = { val s = new ServerSocket(0) val port = s.getLocalPort s.close() port } val env@Seq(p,s,c,d) = Seq.fill(4)(getPort()) val environment = FuturePool.unboundedPool(ExampleServers.main(env.take(3).map(_.toString).toArray)) val diffy = new MainService lazy val differenceProxy = diffy.injector.instance[DifferenceProxy] val thriftFile = new File("src/test/thrift/example.thrift") val data = Source.fromInputStream(Files.newInputStream(thriftFile.toPath), "UTF-8").mkString val thriftJar = Files.createTempFile("thrift", "jar") thriftJar.toFile.deleteOnExit() val out = new ZipOutputStream(new FileOutputStream(thriftJar.toFile)) out.putNextEntry(new ZipEntry(thriftFile.getAbsolutePath)) out.write(data.getBytes) out.closeEntry() out.close() val server = new EmbeddedHttpServer( twitterServer = diffy, flags = Map( "proxy.port" -> s":$d", "candidate" -> s"localhost:$c", "master.primary" -> s"localhost:$p", "master.secondary" -> s"localhost:$s", "serviceName" -> "myThriftService", "service.protocol" -> "thrift", "thrift.jar" -> thriftJar.toAbsolutePath.toString, "thrift.serviceClass" -> "Adder", "summary.email" -> "test" ), stage = Stage.PRODUCTION ) val client = ThriftMux.client.build[Adder.MethodPerEndpoint](s"localhost:$d") test("verify startup") { server.assertHealthy() } test("verify DifferenceCollector") { assert(differenceProxy.collector.fields.isEmpty) Await.result(client.add(1, 1).liftToTry) var tries = 0 while(differenceProxy.outstandingRequests.get() > 0 && tries < 10) { Await.result(Future.sleep(Duration.fromSeconds(1))(DefaultTimer)) tries = tries + 1 } assert(!differenceProxy.collector.fields.isEmpty) } test("verify present differences via API") { val response = Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/add/stats")) assertResult(Status.Ok)(response.status) assert(response.getContentString().contains(""""differences":1""")) } test("verify absent endpoint in API") { val response = Await.result(Http.fetchUrl(s"http://${server.externalHttpHostAndPort}/api/1/endpoints/subtract/stats")) assertResult(Status.Ok)(response.status) assertResult("""{"error":"key not found: subtract"}""")(response.getContentString()) } }
Example 37
Source File: UISuite.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.ui import java.net.ServerSocket import scala.io.Source import scala.util.{Failure, Success, Try} import org.eclipse.jetty.servlet.ServletContextHandler import org.scalatest.concurrent.Eventually._ import org.scalatest.time.SpanSugar._ import org.apache.spark.LocalSparkContext._ import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite} class UISuite extends SparkFunSuite { private def newSparkContext(): SparkContext = { val conf = new SparkConf() .setMaster("local") .setAppName("test") .set("spark.ui.enabled", "true") val sc = new SparkContext(conf) assert(sc.ui.isDefined) sc } ignore("basic ui visibility") { withSpark(newSparkContext()) { sc => // test if the ui is visible, and all the expected tabs are visible eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL(sc.ui.get.appUIAddress).mkString assert(!html.contains("random data that should not be present")) assert(html.toLowerCase.contains("stages")) assert(html.toLowerCase.contains("storage")) assert(html.toLowerCase.contains("environment")) assert(html.toLowerCase.contains("executors")) } } } ignore("visibility at localhost:4040") { withSpark(newSparkContext()) { sc => // test if visible from http://localhost:4040 eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL("http://localhost:4040").mkString assert(html.toLowerCase.contains("stages")) } } } test("jetty selects different port under contention") { val server = new ServerSocket(0) val startPort = server.getLocalPort val serverInfo1 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) val serverInfo2 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) // Allow some wiggle room in case ports on the machine are under contention val boundPort1 = serverInfo1.boundPort val boundPort2 = serverInfo2.boundPort assert(boundPort1 != startPort) assert(boundPort2 != startPort) assert(boundPort1 != boundPort2) serverInfo1.server.stop() serverInfo2.server.stop() server.close() } test("jetty binds to port 0 correctly") { val serverInfo = JettyUtils.startJettyServer( "0.0.0.0", 0, Seq[ServletContextHandler](), new SparkConf) val server = serverInfo.server val boundPort = serverInfo.boundPort assert(server.getState === "STARTED") assert(boundPort != 0) Try { new ServerSocket(boundPort) } match { case Success(s) => fail("Port %s doesn't seem used by jetty server".format(boundPort)) case Failure(e) => } } test("verify appUIAddress contains the scheme") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val uiAddress = ui.appUIAddress val uiHostPort = ui.appUIHostPort assert(uiAddress.equals("http://" + uiHostPort)) } } test("verify appUIAddress contains the port") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val splitUIAddress = ui.appUIAddress.split(':') val boundPort = ui.boundPort assert(splitUIAddress(2).toInt == boundPort) } } }
Example 38
Source File: RawTextSender.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.{SparkConf, Logging} import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 39
Source File: MQTTTestUtils.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.mqtt import java.net.{ServerSocket, URI} import scala.language.postfixOps import com.google.common.base.Charsets.UTF_8 import org.apache.activemq.broker.{BrokerService, TransportConnector} import org.apache.commons.lang3.RandomUtils import org.eclipse.paho.client.mqttv3._ import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence import org.apache.spark.util.Utils import org.apache.spark.{Logging, SparkConf} private[mqtt] class MQTTTestUtils extends Logging { private val persistenceDir = Utils.createTempDir() private val brokerHost = "localhost" private val brokerPort = findFreePort() private var broker: BrokerService = _ private var connector: TransportConnector = _ def brokerUri: String = { s"$brokerHost:$brokerPort" } def setup(): Unit = { broker = new BrokerService() broker.setDataDirectoryFile(Utils.createTempDir()) connector = new TransportConnector() connector.setName("mqtt") connector.setUri(new URI("mqtt://" + brokerUri)) broker.addConnector(connector) broker.start() } def teardown(): Unit = { if (broker != null) { broker.stop() broker = null } if (connector != null) { connector.stop() connector = null } Utils.deleteRecursively(persistenceDir) } private def findFreePort(): Int = { val candidatePort = RandomUtils.nextInt(1024, 65536) Utils.startServiceOnPort(candidatePort, (trialPort: Int) => { val socket = new ServerSocket(trialPort) socket.close() (null, trialPort) }, new SparkConf())._2 } def publishData(topic: String, data: String): Unit = { var client: MqttClient = null try { val persistence = new MqttDefaultFilePersistence(persistenceDir.getAbsolutePath) client = new MqttClient("tcp://" + brokerUri, MqttClient.generateClientId(), persistence) client.connect() if (client.isConnected) { val msgTopic = client.getTopic(topic) val message = new MqttMessage(data.getBytes(UTF_8)) message.setQos(1) message.setRetained(true) for (i <- 0 to 10) { try { msgTopic.publish(message) } catch { case e: MqttException if e.getReasonCode == MqttException.REASON_CODE_MAX_INFLIGHT => // wait for Spark streaming to consume something from the message queue Thread.sleep(50) } } } } finally { if (client != null) { client.disconnect() client.close() client = null } } } }
Example 40
Source File: FlumeTestUtils.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.util.{List => JList} import java.util.Collections import scala.collection.JavaConverters._ import com.google.common.base.Charsets.UTF_8 import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroSourceProtocol, AvroFlumeEvent} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.util.Utils import org.apache.spark.SparkConf private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 41
Source File: PageViewGenerator.scala From BigDatalog with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.net.ServerSocket import java.io.PrintWriter import util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01) : _*) def pickFromDistribution[T](inputMap : Map[T, Double]) : T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent() : String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args : Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 42
Source File: RawTextSender.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 43
Source File: FlumeTestUtils.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.{List => JList} import java.util.Collections import scala.collection.JavaConverters._ import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroFlumeEvent, AvroSourceProtocol} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.SparkConf import org.apache.spark.util.Utils private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 44
Source File: CatalogControllersSpec.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.IOException import java.net.ServerSocket import akka.actor.ActorSystem import akka.stream.ActorMaterializer import catalog_manager.yaml.MetaCatalog import org.specs2.mutable.Specification import play.api.Application import play.api.http.Status import play.api.routing.Router import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.json.{JsArray, JsValue, Json} import play.api.libs.ws.WSResponse import play.api.libs.ws.ahc.AhcWSClient import play.api.test._ import it.gov.daf.catalogmanager import it.gov.daf.catalogmanager.client.Catalog_managerClient import scala.concurrent.duration.Duration import scala.concurrent.{Await, Future} class CatalogControllersSpec extends Specification { def application: Application = GuiceApplicationBuilder().build() import catalog_manager.yaml.BodyReads.MetaCatalogReads "The catalog-manager" should { "Call catalog-manager/v1/dataset-catalogs return ok status" in new WithServer(app = application, port = 9000) { WsTestClient.withClient { implicit client => val response: WSResponse = Await.result[WSResponse](client. url(s"http://localhost:9001/catalog-manager/v1/dataset-catalogs"). execute, Duration.Inf) println(response.status) response.status must be equalTo Status.OK } } "Call catalog-manager/v1/dataset-catalogs return a non empty list if" + "you have error maybe is necessaty to add data to db" in new WithServer(app = application, port = 9000) { WsTestClient.withClient { implicit client => val response: WSResponse = Await.result[WSResponse](client. url(s"http://localhost:9001/catalog-manager/v1/dataset-catalogs"). execute, Duration.Inf) println(response.status) println("ALE") println(response.body) val json: JsValue = Json.parse(response.body) json.as[JsArray].value.size must be greaterThan (0) } } "The catalog-manager" should { "Call catalog-manager/v1/dataset-catalogs/{logical_uri} return ok status" in new WithServer(app = application, port = 9000) { val logicalUri = "daf://dataset/std/standard/standard/uri_cultura/standard" val url = s"http://localhost:9001/catalog-manager/v1/dataset-catalogs/$logicalUri" println(url) WsTestClient.withClient { implicit client => val response: WSResponse = Await.result[WSResponse](client. url(url). execute, Duration.Inf) println(response.status) response.status must be equalTo Status.OK } } } "The catalog-manager" should { "Call catalog-manager/v1/dataset-catalogs/{anything} return 401" in new WithServer(app = application, port = 9000) { val logicalUri = "anything" val url = s"http://localhost:9001/catalog-manager/v1/dataset-catalogs/$logicalUri" println(url) WsTestClient.withClient { implicit client => val response: WSResponse = Await.result[WSResponse](client. url(url). execute, Duration.Inf) println(response.status) response.status must be equalTo 401 } } } } }
Example 45
Source File: ClientTestBase.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.algebra.client import java.net.ServerSocket import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options import endpoints4s.algebra import org.scalatest.concurrent.ScalaFutures import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll} import scala.concurrent.Future import scala.concurrent.duration._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec trait ClientTestBase[T <: algebra.Endpoints] extends AnyWordSpec with Matchers with ScalaFutures with BeforeAndAfterAll with BeforeAndAfter { override implicit def patienceConfig: PatienceConfig = PatienceConfig(15.seconds, 10.millisecond) val wiremockPort = findOpenPort val wireMockServer = new WireMockServer(options().port(wiremockPort)) override def beforeAll(): Unit = wireMockServer.start() override def afterAll(): Unit = wireMockServer.stop() before { wireMockServer.resetAll() } def findOpenPort: Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } val client: T def call[Req, Resp]( endpoint: client.Endpoint[Req, Resp], args: Req ): Future[Resp] def encodeUrl[A](url: client.Url[A])(a: A): String }
Example 46
Source File: ServerInterpreterTest.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.http4s.server import java.net.ServerSocket import cats.effect.{ContextShift, IO, Timer} import endpoints4s.{Invalid, Valid} import endpoints4s.algebra.server.{ BasicAuthenticationTestSuite, DecodedUrl, EndpointsTestSuite, JsonEntitiesFromSchemasTestSuite, SumTypedEntitiesTestSuite, TextEntitiesTestSuite } import org.http4s.server.Router import org.http4s.{HttpRoutes, Uri} import org.http4s.server.blaze.BlazeServerBuilder import org.http4s.syntax.kleisli._ import scala.concurrent.ExecutionContext class ServerInterpreterTest extends EndpointsTestSuite[EndpointsTestApi] with BasicAuthenticationTestSuite[EndpointsTestApi] with JsonEntitiesFromSchemasTestSuite[EndpointsTestApi] with TextEntitiesTestSuite[EndpointsTestApi] with SumTypedEntitiesTestSuite[EndpointsTestApi] { val serverApi = new EndpointsTestApi() def decodeUrl[A](url: serverApi.Url[A])(rawValue: String): DecodedUrl[A] = { val uri = Uri.fromString(rawValue).getOrElse(sys.error(s"Illegal URI: $rawValue")) url.decodeUrl(uri) match { case None => DecodedUrl.NotMatched case Some(Invalid(errors)) => DecodedUrl.Malformed(errors) case Some(Valid(a)) => DecodedUrl.Matched(a) } } private def serveGeneralEndpoint[Req, Resp]( endpoint: serverApi.Endpoint[Req, Resp], request2response: Req => Resp )(runTests: Int => Unit): Unit = { val port = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global) implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global) val service = HttpRoutes.of[IO](endpoint.implementedBy(request2response)) val httpApp = Router("/" -> service).orNotFound val server = BlazeServerBuilder[IO](ExecutionContext.global) .bindHttp(port, "localhost") .withHttpApp(httpApp) server.resource.use(_ => IO(runTests(port))).unsafeRunSync() } def serveEndpoint[Resp]( endpoint: serverApi.Endpoint[_, Resp], response: => Resp )(runTests: Int => Unit): Unit = serveGeneralEndpoint(endpoint, (_: Any) => response)(runTests) def serveIdentityEndpoint[Resp]( endpoint: serverApi.Endpoint[Resp, Resp] )(runTests: Int => Unit): Unit = serveGeneralEndpoint(endpoint, identity[Resp])(runTests) }
Example 47
Source File: IOUtil.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.log.io import java.io._ import java.net.{ServerSocket, URL} import java.nio.charset.StandardCharsets object IOUtil { def withResource[Resource <: AutoCloseable, U](resource: Resource)(body: Resource => U): U = { try { body(resource) } finally { resource.close } } def withTempFile[U](name: String, suffix: String = ".tmp", dir: String = "target")(body: File => U) = { val d = new File(dir) d.mkdirs() val f = File.createTempFile(name, suffix, d) try { body(f) } finally { f.delete() } } def randomPort: Int = unusedPort def unusedPort: Int = { withResource(new ServerSocket(0)) { socket => socket.getLocalPort } } def findPath(path: String): Option[File] = findPath(new File(path)) def findPath(path: File): Option[File] = { if (path.exists()) { Some(path) } else { val defaultPath = new File(new File(System.getProperty("prog.home", "")), path.getPath) if (defaultPath.exists()) { Some(defaultPath) } else { None } } } def readAsString(f: File): String = { readAsString(f.toURI.toURL) } def readAsString(url: URL): String = { withResource(url.openStream()) { in => readAsString(in) } } def readAsString(resourcePath: String): String = { require(resourcePath != null, s"resourcePath is null") Resource .find(resourcePath) .map(readAsString(_)) .getOrElse { val file = findPath(new File(resourcePath)) if (file.isEmpty) { throw new FileNotFoundException(s"Not found ${resourcePath}") } readAsString(new FileInputStream(file.get)) } } def readAsString(in: InputStream): String = { readFully(in) { data => new String(data, StandardCharsets.UTF_8) } } def readFully[U](in: InputStream)(f: Array[Byte] => U): U = { val byteArray = withResource(new ByteArrayOutputStream) { b => val buf = new Array[Byte](8192) withResource(in) { src => var readBytes = 0 while ({ readBytes = src.read(buf); readBytes != -1 }) { b.write(buf, 0, readBytes) } } b.toByteArray } f(byteArray) } }
Example 48
Source File: FluencyTest.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airframe.fluentd import java.io.BufferedInputStream import java.net.ServerSocket import java.util.concurrent.atomic.AtomicBoolean import javax.annotation.{PostConstruct, PreDestroy} import wvlet.airframe.codec.PrimitiveCodec.ValueCodec import wvlet.airframe._ import wvlet.airspec.AirSpec import wvlet.log.LogSupport import wvlet.log.io.IOUtil case class MockFluentdConfig(port: Int) trait MockFluentd extends LogSupport { lazy val socket = bind { config: MockFluentdConfig => new ServerSocket(config.port) } val shutdown = new AtomicBoolean(false) val t = new Thread(new Runnable { override def run(): Unit = { val clientSocket = socket.accept() val out = clientSocket.getOutputStream val in = new BufferedInputStream(clientSocket.getInputStream) while (!shutdown.get()) { var b = new Array[Byte](8192) var totalReadLen = 0 var readLen = in.read(b) while (readLen != -1) { val nextReadLen = in.read(b, totalReadLen, readLen) totalReadLen += readLen readLen = nextReadLen } if (totalReadLen > 0) { val v = ValueCodec.unpackMsgPack(b, 0, totalReadLen) logger.debug(s"Received event: ${v}") } } } }) @PostConstruct def start: Unit = { debug(s"starting MockFluentd") t.start() } @PreDestroy def stop: Unit = { debug(s"stopping MockFluentd") shutdown.set(true) socket.close() t.interrupt() } } case class FluencyMetric(id: Int, name: String) extends TaggedMetric { def metricTag = "fluency_metric" } class FluencyTest extends AirSpec { private val fluentdPort = IOUtil.randomPort protected override val design: Design = { newDesign .bind[MockFluentdConfig].toInstance(new MockFluentdConfig(fluentdPort)) .bind[MockFluentd].toEagerSingleton .add( fluentd .withFluentdLogger( port = fluentdPort, // Do not send ack for simplicity ackResponseMode = false ) ) } def `should send metrics to fluentd through Fluency`(f: MetricLoggerFactory): Unit = { // Use a regular emit method f.getLogger.emit("mytag", Map("data" -> "hello")) // Use object metric logger val l = f.getTypedLogger[FluencyMetric] l.emit(FluencyMetric(1, "leo")) f.getLoggerWithTagPrefix("system").emit("mytag", Map("data" -> "metric value")) } test( "test extended time", design = fluentd.withFluentdLogger(port = fluentdPort, ackResponseMode = false, useExtendedEventTime = true) ) { f: MetricLoggerFactory => val l = f.getLogger l.emit("mytag", Map("data" -> "hello")) l.emitMsgPack("tag", Array(0xc6.toByte)) } }
Example 49
Source File: EventsTestHelper.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.monitoring.metrics import java.net.ServerSocket import akka.actor.ActorSystem import akka.stream.ActorMaterializer import com.typesafe.config.Config import org.apache.openwhisk.core.monitoring.metrics.OpenWhiskEvents.MetricConfig import pureconfig._ import pureconfig.generic.auto._ trait EventsTestHelper { protected def createConsumer(kport: Int, globalConfig: Config, recorder: MetricRecorder)( implicit system: ActorSystem, materializer: ActorMaterializer) = { val settings = OpenWhiskEvents .eventConsumerSettings(OpenWhiskEvents.defaultConsumerConfig(globalConfig)) .withBootstrapServers(s"localhost:$kport") val metricConfig = loadConfigOrThrow[MetricConfig](globalConfig, "user-events") EventConsumer(settings, Seq(recorder), metricConfig) } protected def freePort(): Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } }
Example 50
Source File: S3Minio.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.s3 import java.net.ServerSocket import actionContainers.ActionContainer import akka.actor.ActorSystem import akka.stream.ActorMaterializer import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials} import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration import com.amazonaws.services.s3.AmazonS3ClientBuilder import com.typesafe.config.ConfigFactory import common.{SimpleExec, StreamLogging} import org.scalatest.{BeforeAndAfterAll, FlatSpec} import org.apache.openwhisk.common.{Logging, TransactionId} import org.apache.openwhisk.core.database.{AttachmentStore, DocumentSerializer} import scala.concurrent.duration._ import scala.reflect.ClassTag trait S3Minio extends FlatSpec with BeforeAndAfterAll with StreamLogging { def makeS3Store[D <: DocumentSerializer: ClassTag]()(implicit actorSystem: ActorSystem, logging: Logging, materializer: ActorMaterializer): AttachmentStore = { val config = ConfigFactory.parseString(s""" |whisk { | s3 { | alpakka { | aws { | credentials { | provider = static | access-key-id = "$accessKey" | secret-access-key = "$secretAccessKey" | } | region { | provider = static | default-region = us-west-2 | } | } | endpoint-url = "http://localhost:$port" | } | bucket = "$bucket" | $prefixConfig | } |} """.stripMargin).withFallback(ConfigFactory.load()) S3AttachmentStoreProvider.makeStore[D](config) } private val accessKey = "TESTKEY" private val secretAccessKey = "TESTSECRET" private val port = freePort() private val bucket = "test-ow-travis" private def prefixConfig = { if (bucketPrefix.nonEmpty) s"prefix = $bucketPrefix" else "" } protected def bucketPrefix: String = "" override protected def beforeAll(): Unit = { super.beforeAll() dockerExec( s"run -d -e MINIO_ACCESS_KEY=$accessKey -e MINIO_SECRET_KEY=$secretAccessKey -p $port:9000 minio/minio server /data") println(s"Started minio on $port") createTestBucket() } override def afterAll(): Unit = { super.afterAll() val containerId = dockerExec("ps -q --filter ancestor=minio/minio") containerId.split("\n").map(_.trim).foreach(id => dockerExec(s"stop $id")) println(s"Stopped minio container") } def createTestBucket(): Unit = { val endpoint = new EndpointConfiguration(s"http://localhost:$port", "us-west-2") val client = AmazonS3ClientBuilder.standard .withPathStyleAccessEnabled(true) .withEndpointConfiguration(endpoint) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretAccessKey))) .build org.apache.openwhisk.utils.retry(client.createBucket(bucket), 6, Some(1.minute)) println(s"Created bucket $bucket") } private def dockerExec(cmd: String): String = { implicit val tid: TransactionId = TransactionId.testing val command = s"${ActionContainer.dockerCmd} $cmd" val cmdSeq = command.split(" ").map(_.trim).filter(_.nonEmpty) val (out, err, code) = SimpleExec.syncRunCmd(cmdSeq) assert(code == 0, s"Error occurred for command '$command'. Exit code: $code, Error: $err") out } private def freePort(): Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } }
Example 51
Source File: TestZooKeeper.scala From mango with Apache License 2.0 | 5 votes |
package com.kakao.mango.zk import java.io.{File, IOException} import java.net.{ServerSocket, Socket} import java.util.concurrent.TimeUnit import com.kakao.mango.concurrent.NamedExecutors import com.kakao.mango.logging.{LogLevelOverrider, Logging} import com.kakao.shaded.guava.io.Files import org.apache.zookeeper.server.persistence.FileTxnSnapLog import org.apache.zookeeper.server.{ServerCnxnFactory, ServerConfig, ZooKeeperServer} import org.scalatest.{BeforeAndAfterAll, Suite} trait TestZooKeeper extends BeforeAndAfterAll with Logging { this: Suite => val zkServerPort = 2181 val zkServerExecutor = NamedExecutors.single("zookeeper-server") var zk: ZooKeeperConnection = _ override protected def beforeAll(): Unit = { logger.info("Launching a standalone ZooKeeper server for testing...") try { val socket = new ServerSocket(zkServerPort) socket.close() } catch { case e: IOException => throw new RuntimeException(s"TCP port $zkServerPort is required for tests but not available") } zkServerExecutor.submit { LogLevelOverrider.error("org.apache.zookeeper") val datadir = Files.createTempDir().getAbsolutePath val config = new ServerConfig config.parse(Array(zkServerPort.toString, datadir)) val zkServer = new ZooKeeperServer zkServer.setTxnLogFactory(new FileTxnSnapLog(new File(datadir), new File(datadir))) zkServer.setTickTime(6000) zkServer.setMinSessionTimeout(6000) zkServer.setMaxSessionTimeout(6000) val cnxnFactory = ServerCnxnFactory.createFactory try { cnxnFactory.configure(config.getClientPortAddress, 60) cnxnFactory.startup(zkServer) cnxnFactory.join() } catch { case _: InterruptedException => logger.info("ZooKeeper server interrupted; shutting down...") cnxnFactory.shutdown() cnxnFactory.join() if (zkServer.isRunning) { zkServer.shutdown() } logger.info("ZooKeeper server stopped") } } var connected = false while (!connected) { logger.info("Waiting for ZooKeeper server to launch...") try { val socket = new Socket("localhost", zkServerPort) logger.info("ZooKeeper server is available") socket.close() zk = ZooKeeperConnection(s"localhost:$zkServerPort") connected = true } catch { case _: IOException => Thread.sleep(1000) // retry } } super.beforeAll() } override protected def afterAll(): Unit = { try super.afterAll() finally { zk.close() logger.info("Interrupting ZooKeeper server...") zkServerExecutor.shutdownNow() while (!zkServerExecutor.awaitTermination(1, TimeUnit.SECONDS)) { logger.info("awaiting ZooKeeper server termination...") } logger.info("ZooKeeper server terminated") } } }
Example 52
Source File: TestTargetServer.scala From scalajs-reactjs with MIT License | 5 votes |
package io.github.shogowada.scalajs.reactjs.example import java.net.ServerSocket import org.eclipse.jetty.server.Server import org.eclipse.jetty.server.handler.ResourceHandler class TestTargetServer(project: String) { private var server: Server = _ def start(): Unit = { val maybeTarget = sys.props.get(s"target.path.$project") assert(maybeTarget.isDefined) val target = maybeTarget.get println(s"Target path for $project: $target") val port = freePort println(s"Target port for $project: $port") server = new Server(port) val handler = new ResourceHandler() handler.setResourceBase(s"$target") server.setHandler(handler) server.start() println(s"Target host for $project: $host") } def freePort: Int = { var socket: ServerSocket = null var port: Int = 0 try { socket = new ServerSocket(0) socket.setReuseAddress(true) port = socket.getLocalPort } finally { socket.close() } port } def host: String = s"http://localhost:${server.getURI.getPort}/classes" def stop(): Unit = { server.stop() } } object TestTargetServers { val customVirtualDOM = new TestTargetServer("custom-virtual-dom") val helloWorld = new TestTargetServer("helloworld") val helloWorldFunction = new TestTargetServer("helloworld-function") val interactiveHelloWorld = new TestTargetServer("interactive-helloworld") val lifecycle = new TestTargetServer("lifecycle") val reduxDevTools = new TestTargetServer("redux-devtools") val reduxMiddleware = new TestTargetServer("redux-middleware") val router = new TestTargetServer("router") val routerRedux = new TestTargetServer("router-redux") val style = new TestTargetServer("style") val todoApp = new TestTargetServer("todo-app") val todoAppRedux = new TestTargetServer("todo-app-redux") customVirtualDOM.start() helloWorld.start() helloWorldFunction.start() interactiveHelloWorld.start() lifecycle.start() reduxDevTools.start() reduxMiddleware.start() routerRedux.start() router.start() style.start() todoApp.start() todoAppRedux.start() sys.addShutdownHook(() => { customVirtualDOM.stop() helloWorld.stop() helloWorldFunction.stop() interactiveHelloWorld.stop() lifecycle.stop() reduxDevTools.stop() reduxMiddleware.stop() routerRedux.stop() router.stop() style.stop() todoApp.stop() todoAppRedux.stop() }) }
Example 53
Source File: AbstractEngineCreator.scala From Linkis with Apache License 2.0 | 5 votes |
package com.webank.wedatasphere.linkis.enginemanager import java.net.ServerSocket import com.webank.wedatasphere.linkis.common.conf.DWCArgumentsParser import com.webank.wedatasphere.linkis.common.utils.Utils import com.webank.wedatasphere.linkis.enginemanager.conf.EngineManagerConfiguration import com.webank.wedatasphere.linkis.enginemanager.exception.EngineManagerErrorException import com.webank.wedatasphere.linkis.enginemanager.impl.UserTimeoutEngineResource import com.webank.wedatasphere.linkis.enginemanager.process.{CommonProcessEngine, ProcessEngine, ProcessEngineBuilder} import com.webank.wedatasphere.linkis.protocol.engine.{EngineCallback, RequestEngine} import com.webank.wedatasphere.linkis.rpc.Sender import com.webank.wedatasphere.linkis.server.{JMap, toScalaMap} import org.apache.commons.io.IOUtils import scala.collection.mutable.ArrayBuffer abstract class AbstractEngineCreator extends EngineCreator { private val inInitPorts = ArrayBuffer[Int]() private def getAvailablePort: Int = synchronized { var port = AbstractEngineCreator.getNewPort while(inInitPorts.contains(port)) port = AbstractEngineCreator.getNewPort inInitPorts += port port } def removePort(port: Int): Unit = inInitPorts -= port protected def createProcessEngineBuilder(): ProcessEngineBuilder protected def getExtractSpringConfigs(requestEngine: RequestEngine): JMap[String, String] = { val springConf = new JMap[String, String] requestEngine.properties.keysIterator.filter(_.startsWith("spring.")).foreach(key => springConf.put(key.substring(7), requestEngine.properties.get(key))) springConf } protected def createEngine(processEngineBuilder:ProcessEngineBuilder,parser:DWCArgumentsParser):ProcessEngine={ processEngineBuilder.getEngineResource match { case timeout: UserTimeoutEngineResource => new CommonProcessEngine(processEngineBuilder, parser, timeout.getTimeout) case _ => new CommonProcessEngine(processEngineBuilder, parser) } } override def create(ticketId: String, engineRequest: EngineResource, request: RequestEngine): Engine = { val port = getAvailablePort val processEngineBuilder = createProcessEngineBuilder() processEngineBuilder.setPort(port) processEngineBuilder.build(engineRequest, request) val parser = new DWCArgumentsParser var springConf = Map("spring.application.name" -> EngineManagerConfiguration.ENGINE_SPRING_APPLICATION_NAME.getValue, "server.port" -> port.toString, "spring.profiles.active" -> "engine", "logging.config" -> "classpath:log4j2-engine.xml", "eureka.client.serviceUrl.defaultZone" -> EngineManagerReceiver.getSpringConf("eureka.client.serviceUrl.defaultZone")) springConf = springConf ++: getExtractSpringConfigs(request).toMap parser.setSpringConf(springConf) var dwcConf = Map("ticketId" -> ticketId, "creator" -> request.creator, "user" -> request.user) ++: EngineCallback.callbackToMap(EngineCallback(Sender.getThisServiceInstance.getApplicationName, Sender.getThisServiceInstance.getInstance)) if(request.properties.exists{case (k, v) => k.contains(" ") || (v != null && v.contains(" "))}) throw new EngineManagerErrorException(30000, "Startup parameters contain spaces!(启动参数中包含空格!)") dwcConf = dwcConf ++: request.properties.toMap parser.setDWCConf(dwcConf) val engine = createEngine(processEngineBuilder,parser) engine.setTicketId(ticketId) engine.setPort(port) engine match { case commonEngine: CommonProcessEngine => commonEngine.setUser(request.user) case _ => } engine } } object AbstractEngineCreator { private[enginemanager] def getNewPort: Int = { val socket = new ServerSocket(0) Utils.tryFinally(socket.getLocalPort)(IOUtils.closeQuietly(socket)) } }
Example 54
Source File: SocketService.scala From HadoopLearning with MIT License | 5 votes |
package com.utils import java.io.DataOutputStream import java.net.ServerSocket object SocketService { def main(args: Array[String]): Unit = { new Thread(new SparkSocket()).start() } class SparkSocket extends Runnable { override def run(): Unit = { val server = new ServerSocket(8880) while (true) { println("等待客户端进行连接....") val socket = server.accept() //发送给客户端数据 val stream = new DataOutputStream(socket.getOutputStream()) println("发送消息了....") stream.writeUTF("The Indian government has decided2 to scrap3 a controversial 12% tax on the feminine") //五百毫秒停顿一下 stream.flush() stream.close() } } } }
Example 55
Source File: CounterTest.scala From endpoints4s with MIT License | 5 votes |
package quickstart import java.net.ServerSocket import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest, StatusCodes} import akka.http.scaladsl.server.Directives._ import org.scalatest.BeforeAndAfterAll import scala.concurrent.Await import scala.concurrent.duration.DurationInt import org.scalatest.freespec.AsyncFreeSpec class CounterTest extends AsyncFreeSpec with BeforeAndAfterAll { implicit val actorSystem: ActorSystem = ActorSystem() val routes = CounterServer.routes ~ DocumentationServer.routes val interface = "0.0.0.0" val port = findOpenPort() val server = Http().bindAndHandle(routes, interface, port) override protected def afterAll(): Unit = { Await.result( Await.result(server, 10.seconds).terminate(3.seconds), 15.seconds ) Await.result(actorSystem.terminate(), 5.seconds) super.afterAll() } "CounterServer" - { "Query counter value" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/current-value")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) assert(entity.data.utf8String == "{\"value\":0}") } } "Increment counter value" in { val request = HttpRequest( method = HttpMethods.POST, uri = uri("/increment"), entity = HttpEntity(ContentTypes.`application/json`, "{\"step\":1}") ) for { response <- Http().singleRequest(request) } yield { assert(response.status == StatusCodes.OK) } } "Query API documentation" in { for { response <- Http().singleRequest( HttpRequest(uri = uri("/documentation.json")) ) entity <- response.entity.toStrict(1.second) } yield { assert(response.status == StatusCodes.OK) assert(entity.contentType == ContentTypes.`application/json`) } } } def findOpenPort(): Int = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } def uri(suffix: String) = s"http://$interface:$port$suffix" }
Example 56
Source File: Iot_managerSpec.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.{IOException, File => JFile} import java.net.ServerSocket import java.util.Base64 import better.files._ import it.gov.daf.iotmanager.client.Iot_managerClient import org.specs2.mutable.Specification import org.specs2.specification.BeforeAfterAll import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.ahc.AhcWSClient import play.api.libs.ws.{WSAuthScheme, WSResponse} import play.api.test.{WithServer, WsTestClient} import scala.concurrent.Await import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.util.{Failure, Try} import org.apache.solr.client.solrj.embedded.JettyConfig import org.apache.solr.client.solrj.embedded.JettySolrRunner import org.eclipse.jetty.servlet.ServletHolder @SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Throw")) class Iot_managerSpec extends Specification with BeforeAfterAll { import Iot_managerSpec._ def getAvailablePort: Int = { try { val socket = new ServerSocket(0) try { socket.getLocalPort } finally { socket.close() } } catch { case e: IOException => throw new IllegalStateException(s"Cannot find available port: ${e.getMessage}", e) } } def application: Application = GuiceApplicationBuilder(). configure("hadoop_conf_dir" -> s"${ServiceSpec.confPath.pathAsString}"). configure("pac4j.authenticator" -> "test"). build() "The security_manager" should { "manage user tokens correctly" in new WithServer(app = application, port = getAvailablePort) { print("ciao ciao") } } override def beforeAll(): Unit = { val solrXml = new Nothing("/solr/home/solr.xml") val solrHomeDir = solrXml.getParentFile val port = 8080 val context = "/solr" // use org.apache.solr.client.solrj.embedded.JettySolrRunner val jettySolr = new Nothing(solrHomeDir.getAbsolutePath, context, port) val waitUntilTheSolrWebAppHasStarted = true jettySolr.start(waitUntilTheSolrWebAppHasStarted) } override def afterAll(): Unit = { jettySolr.stop() } } @SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null")) object Iot_managerSpec { }
Example 57
Source File: ServiceSpec.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.{IOException, File => JFile} import java.net.ServerSocket import java.util.Base64 import it.gov.daf.securitymanager.client.Security_managerClient import org.specs2.mutable.Specification import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.ahc.AhcWSClient import play.api.libs.ws.{WSAuthScheme, WSResponse} import play.api.test.{WithServer, WsTestClient} import scala.concurrent.Await import play.api.libs.concurrent.Execution.Implicits.defaultContext import scala.concurrent.duration.Duration //@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Throw")) class ServiceSpec extends Specification { def getAvailablePort: Int = { try { val socket = new ServerSocket(0) try { socket.getLocalPort } finally { socket.close() } } catch { case e: IOException => throw new IllegalStateException(s"Cannot find available port: ${e.getMessage}", e) } } def application: Application = GuiceApplicationBuilder(). configure("pac4j.authenticator" -> "test"). build() "The security_manager" should { "manage user tokens correctly" in new WithServer(app = application, port = getAvailablePort) { private val token = WsTestClient.withClient { implicit client => val response: WSResponse = Await.result[WSResponse](client. url(s"http://localhost:$port/security-manager/v1/token"). withAuth("david", "david", WSAuthScheme.BASIC). execute, Duration.Inf) response.body } val ws: AhcWSClient = AhcWSClient() val plainCreds = "david:david" val plainCredsBytes = plainCreds.getBytes val base64CredsBytes = Base64.getEncoder.encode(plainCredsBytes) val base64Creds = new String(base64CredsBytes) val client = new Security_managerClient(ws)(s"http://localhost:$port") val token2 = Await.result(client.token(s"Basic $base64Creds"), Duration.Inf) s""""$token2"""" must be equalTo token Await.result(client.token(s"Bearer $token2").map(token => s""""$token""""), Duration.Inf) must be equalTo token } } }
Example 58
Source File: ServiceSpec.scala From daf with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.{File, FileNotFoundException, IOException} import java.net.ServerSocket import java.util.Base64 import it.gov.daf.entitymanager.Entity import it.gov.daf.entitymanager.client.Entity_managerClient import org.specs2.mutable.Specification import org.specs2.specification.BeforeAfterAll import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.ahc.AhcWSClient import play.api.test.WithServer import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.util.{Failure, Random, Try} @SuppressWarnings( Array( "org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Throw", "org.wartremover.warts.Var" ) ) class ServiceSpec extends Specification with BeforeAfterAll { def getAvailablePort: Int = { try { val socket = new ServerSocket(0) try { socket.getLocalPort } finally { socket.close() } } catch { case e: IOException => throw new IllegalStateException(s"Cannot find available port: ${e.getMessage}", e) } } private def constructTempDir(dirPrefix: String): Try[File] = Try { val rndrange = 10000000 val file = new File(System.getProperty("java.io.tmpdir"), s"$dirPrefix${Random.nextInt(rndrange)}") if (!file.mkdirs()) throw new RuntimeException("could not create temp directory: " + file.getAbsolutePath) file.deleteOnExit() file } private def deleteDirectory(path: File): Boolean = { if (!path.exists()) { throw new FileNotFoundException(path.getAbsolutePath) } var ret = true if (path.isDirectory) path.listFiles().foreach(f => ret = ret && deleteDirectory(f)) ret && path.delete() } var tmpDir: Try[File] = Failure[File](new Exception("")) def application: Application = GuiceApplicationBuilder(). configure("pac4j.authenticator" -> "test"). configure("janusgraph.storage.directory" -> s"${tmpDir.map(_.getCanonicalPath).getOrElse("db")}/berkeleyje"). configure("janusgraph.index.search.directory" -> s"${tmpDir.map(_.getCanonicalPath).getOrElse("db")}/lucene"). build() "The entity_manager" should { "create an entity and retrieve it correctly" in new WithServer(app = application, port = getAvailablePort) { val ws: AhcWSClient = AhcWSClient() val plainCreds = "david:david" val plainCredsBytes = plainCreds.getBytes val base64CredsBytes = Base64.getEncoder.encode(plainCredsBytes) val base64Creds = new String(base64CredsBytes) val client = new Entity_managerClient(ws)(s"http://localhost:$port") val result = Await.result(client.createEntity(s"Basic $base64Creds", Entity("DAVID")), Duration.Inf) val entity = Await.result(client.getEntity(s"Basic $base64Creds", "DAVID"), Duration.Inf) entity must beEqualTo(Entity("DAVID")) } } override def beforeAll(): Unit = tmpDir = constructTempDir("test") override def afterAll(): Unit = tmpDir.foreach(deleteDirectory(_)) }
Example 59
Source File: StreamingModelProducer.scala From AI with Apache License 2.0 | 5 votes |
package com.bigchange.streaming import java.io.PrintWriter import java.net.ServerSocket import breeze.linalg.DenseVector import scala.util.Random object StreamingModelProducer { def main(args: Array[String]) { val maxEvent = 100 val numFeatures = 100 val random = new Random() // 生成服从正太分布的稠密向量函数 def generateRandomArray(n: Int) = Array.tabulate(n)(_ => random.nextGaussian()) // 一个确定的随机模型权重向量 val w = new DenseVector(generateRandomArray(numFeatures)) val intercept = random.nextGaussian() * 10 // 生成一些随机数据事件 def generateNoisyData(n:Int) = { (1 to n).map { i => val x = new DenseVector(generateRandomArray(numFeatures)) // 随机特征向量 val y = w.dot(x) val noisy = y + intercept // 目标值 (noisy, x) } } // 创建网络生成器 val listener = new ServerSocket(9999) println("listener port:" + listener.getLocalPort) while(true) { val socket = listener.accept() new Thread() { override def run() = { println("get client from:" + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream, true) while (true) { Thread.sleep(1000) val num = random.nextInt(maxEvent) val productEvents = generateNoisyData(num) productEvents.foreach { case(y, x) => out.write(y + "\t" + x.data.mkString(",")) out.write("\n") } out.flush() println(s"created $num events") } socket.close() } }.start() } } }
Example 60
Source File: BaseTarget.scala From scala-json-rpc with MIT License | 5 votes |
package io.github.shogowada.scala.jsonrpc.example.test.utils import java.net.ServerSocket import org.apache.http.client.methods.HttpGet import org.apache.http.impl.client.HttpClientBuilder import scala.util.Try trait BaseTarget { lazy val port = freePort() lazy val url = s"http://localhost:$port" lazy val jarLocation = System.getProperty("jarLocation") def healthCheckUrl = url def freePort(): Int = { val server = new ServerSocket(0) val localPort = server.getLocalPort server.close() localPort } private def startProcess(jarLocation: String, port: Int): Process = { new ProcessBuilder( "java", s"-Dport=$port", "-jar", jarLocation ).start() } private def waitUntilReady(): Unit = { Range(0, 10).toStream .map(_ => { Thread.sleep(1000) val client = HttpClientBuilder.create().build() val maybeCode = Try { val response = client.execute(new HttpGet(healthCheckUrl)) val code = response.getStatusLine.getStatusCode response.close() code }.toOption client.close() maybeCode }) .filter(code => code.contains(200)) .head } val target = startProcess(jarLocation, port) Runtime.getRuntime.addShutdownHook(new Thread() { override def run(): Unit = { target.destroy() } }) waitUntilReady() }
Example 61
Source File: RawTextSender.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 62
Source File: FlumeTestUtils.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.{List => JList} import java.util.Collections import scala.collection.JavaConverters._ import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroFlumeEvent, AvroSourceProtocol} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.util.Utils import org.apache.spark.SparkConf private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 63
Source File: PageViewGenerator.scala From drizzle-spark with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.io.PrintWriter import java.net.ServerSocket import java.util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01): _*) def pickFromDistribution[T](inputMap: Map[T, Double]): T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent(): String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args: Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 64
Source File: ProxyServer.scala From devbox with Apache License 2.0 | 5 votes |
package cmdproxy import java.io.BufferedReader import java.io.InputStreamReader import java.io.OutputStreamWriter import java.io.PrintWriter import java.net.InetAddress import java.net.ServerSocket import java.net.Socket import scala.util.Using import devbox.logger.FileLogger import os.RelPath import ujson.ParseException import upickle.default.{macroRW, ReadWriter} case class Request(workingDir: String, cmd: Seq[String]) object Request { implicit val rw: ReadWriter[Request] = macroRW } val localDir: Map[os.RelPath, os.Path] = dirMapping.map(_.swap).toMap def start(): Unit = { logger.info(s"Starting command proxy server, listening at ${socket.getInetAddress}:${socket.getLocalPort}") (new Thread("Git Proxy Thread") { override def run(): Unit = { while (!socket.isClosed) { Using(socket.accept()) { handleConnection } recover { case e: Exception => logger.error(s"Error handling request ${e.getMessage}") case e: java.net.SocketException if e.getMessage == "Socket closed" => logger.error(s"Git proxy socket closed") } } } }).start() } def handleConnection(conn: Socket): Unit = try { logger.info(s"Accepting connection from ${conn.getInetAddress}") val in = new BufferedReader(new InputStreamReader(conn.getInputStream, ProxyServer.CHARSET_NAME)) val out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream, ProxyServer.CHARSET_NAME)) upickle.default.read[Request](in.readLine()) match { case Request(dir, args) => val workingDir = localDir .collect{case (remote, local) if RelPath(dir).startsWith(remote) => local / RelPath(dir).relativeTo(remote) } .head // being cautious here and only execute "git" commands if (args.headOption.exists((_ == "git"))) { logger.info(s"Executing `${args.mkString(" ")}` in $workingDir") val proc = os.proc(args).call( workingDir, mergeErrIntoOut = true, stdout = os.ProcessOutput.Readlines(str => out.println(upickle.default.write(Left[String, Int](str))) ), check = false, timeout = 10000 ) out.println(upickle.default.write(Right[String, Int](proc.exitCode))) } else { val msg = s"Not executing non-git commend: `${args.mkString(" ")}`." logger.info(msg) out.println(upickle.default.write(Right[String, Int](1))) } out.flush() } } catch { case e: ParseException => logger.error(s"Error parsing incoming json request: ${e.getMessage}") } } object ProxyServer { val DEFAULT_PORT = 20280 val CHARSET_NAME = "UTF-8" }
Example 65
Source File: LoggerSimulation.scala From BigData-News with Apache License 2.0 | 5 votes |
package com.vita.spark.utils import java.io.PrintWriter import java.net.ServerSocket class LoggerSimulation { } object LoggerSimulation { var numIndex = 0 /** * 生成一个字母 * * @param 字母的下标 * @return 生成的字母 */ def gennerateContent(index: Int): String = { import scala.collection.mutable.ListBuffer val charList = ListBuffer[Char](); for (i <- 65 to 90) { charList += i.toChar } val charArray = charList.toArray charArray(index).toString(); } def gennerateNumber(): String = { // numIndex += 1 // return numIndex.toString return "a,b,c,d,e,f" } /** * 生成随机下标 * * @return 返回一个下标 */ def index = { import java.util.Random val rdm = new Random() rdm.nextInt(7) } /** * 启动一个main方法来创建一个serversockt发送消息 * * @param args 端口,发送的时间间隔 */ def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage:<port><millisecond>") System.exit(1); } val listener = new ServerSocket(args(0).toInt) println("已经做好连接的准备-------") while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from:" + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream, true) while (true) { Thread.sleep(args(1).toLong) // val content = gennerateContent(index) val content = gennerateNumber() println(content) out.write(content + "\n") out.flush() } socket.close() } }.start() } } }
Example 66
Source File: StreamSQLExample.scala From carbondata with Apache License 2.0 | 5 votes |
package org.apache.carbondata.examples import java.net.ServerSocket import org.apache.carbondata.examples.util.ExampleUtils // scalastyle:off println object StreamSQLExample { def main(args: Array[String]) { val spark = ExampleUtils.createSparkSession("StructuredStreamingExample", 4) val requireCreateTable = true val recordFormat = "json" // can be "json" or "csv" if (requireCreateTable) { // drop table if exists previously spark.sql(s"DROP TABLE IF EXISTS sink") spark.sql("DROP TABLE IF EXISTS source") // Create target carbon table and populate with initial data spark.sql( s""" | CREATE TABLE sink( | id INT, | name STRING, | salary FLOAT, | file struct<school:array<string>, age:int> | ) | STORED AS carbondata | TBLPROPERTIES( | 'streaming'='true', 'sort_columns'='') """.stripMargin) } spark.sql( s""" | CREATE TABLE source ( | id INT, | name STRING, | salary FLOAT, | file struct<school:array<string>, age:int> | ) | STORED AS carbondata | TBLPROPERTIES( | 'streaming'='source', | 'format'='socket', | 'host'='localhost', | 'port'='7071', | 'record_format'='$recordFormat' | ) """.stripMargin) val serverSocket = new ServerSocket(7071) // start ingest streaming job spark.sql( s""" | CREATE STREAM ingest ON TABLE sink | STMPROPERTIES( | 'trigger' = 'ProcessingTime', | 'interval' = '3 seconds') | AS SELECT * FROM source """.stripMargin) // start writing data into the socket import StructuredStreamingExample.{showTableCount, writeSocket} val thread1 = writeSocket(serverSocket, recordFormat) val thread2 = showTableCount(spark, "sink") System.out.println("type enter to interrupt streaming") System.in.read() thread1.interrupt() thread2.interrupt() serverSocket.close() // stop streaming job spark.sql("DROP STREAM ingest").show spark.stop() System.out.println("streaming finished") } } // scalastyle:on println
Example 67
Source File: TestUtils.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair import java.io.File import java.net.ServerSocket import akka.actor.ActorRef import akka.event.DiagnosticLoggingAdapter import akka.testkit import akka.testkit.{TestActor, TestProbe} import fr.acinq.eclair.channel.Channel import fr.acinq.eclair.wire.LightningMessage object TestUtils { def forwardOutgoingToPipe(peer: TestProbe, pipe: ActorRef): Unit = { peer.setAutoPilot(new testkit.TestActor.AutoPilot { override def run(sender: ActorRef, msg: Any): TestActor.AutoPilot = msg match { case Channel.OutgoingMessage(msg: LightningMessage, _: ActorRef) => pipe tell (msg, sender) TestActor.KeepRunning case _ => TestActor.KeepRunning } }) } }
Example 68
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http import java.net.{ServerSocket, URI} import java.util.concurrent.TimeoutException import org.scalatest.concurrent.ScalaFutures import org.scalatest.BeforeAndAfterAll import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler} import org.webbitserver.netty.NettyWebServer import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.WSClient import play.api.test.WsTestClient import play.api.{Configuration, Play} import uk.gov.hmrc.http.HeaderCarrier import uk.gov.hmrc.play.http.ws.WSHttp import uk.gov.hmrc.play.test.TestHttpCore import scala.concurrent.{Await, ExecutionContext} import scala.concurrent.duration.DurationInt class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll { import ExecutionContext.Implicits.global lazy val fakeApplication = GuiceApplicationBuilder(configuration = Configuration("play.ws.timeout.request" -> "1000ms")).build() override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } WsTestClient.withClient{ client => "HttpCalls" should { "be gracefully timeout when no response is received within the 'timeout' frame" in { val http = new WSHttp with TestHttpCore { override val wsClient = fakeApplication.injector.instanceOf[WSClient] } // get an unused port val ss = new ServerSocket(0) ss.close() val executor = ExecutionContext.global // fromExecutorService(ExecutionContext.global) val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}") val ws = new NettyWebServer(executor, ss.getLocalSocketAddress, publicUri) try { //starts web server ws.add( "/test", new DelayedHttpHandler(executor, 2000, new StringHttpHandler("application/json", "{name:'pong'}"))) ws.start().get() implicit val hc = HeaderCarrier() val start = System.currentTimeMillis() intercept[TimeoutException] { //make request to web server Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds) } val diff = (System.currentTimeMillis() - start).toInt // there is test execution delay around 700ms diff should be >= 1000 diff should be < 2500 } finally { ws.stop() } } } } }
Example 69
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http import java.net.{ServerSocket, URI} import java.util.concurrent.TimeoutException import org.scalatest.concurrent.ScalaFutures import org.scalatest.BeforeAndAfterAll import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler} import org.webbitserver.netty.NettyWebServer import play.api.Play import play.api.test.FakeApplication import uk.gov.hmrc.http.HeaderCarrier import uk.gov.hmrc.play.http.ws.WSHttp import uk.gov.hmrc.play.test.TestHttpCore import scala.concurrent.Await import scala.concurrent.duration.DurationInt import scala.concurrent.ExecutionContext.Implicits.global class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll { lazy val fakeApplication = FakeApplication(additionalConfiguration = Map("ws.timeout.request" -> "1000")) override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } "HttpCalls" should { "be gracefully timeout when no response is received within the 'timeout' frame" in { val http = new WSHttp with TestHttpCore // get an unused port val ss = new ServerSocket(0) ss.close() val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}") val ws = new NettyWebServer(global, ss.getLocalSocketAddress, publicUri) try { //starts web server ws.add( "/test", new DelayedHttpHandler(global, 2000, new StringHttpHandler("application/json", "{name:'pong'}"))) ws.start().get() implicit val hc = HeaderCarrier() val start = System.currentTimeMillis() intercept[TimeoutException] { //make request to web server Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds) } val diff = (System.currentTimeMillis() - start).toInt // there is test execution delay around 700ms diff should be >= 1000 diff should be < 2500 } finally { ws.stop() } } } }
Example 70
Source File: wireMockEndpoints.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http.ws import java.net.ServerSocket import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ import scala.util.Try trait WireMockEndpoints { val host: String = "localhost" val endpointPort: Int = PortTester.findPort() val endpointMock = new WireMock(host, endpointPort) val endpointServer: WireMockServer = new WireMockServer(wireMockConfig().port(endpointPort)) val proxyPort: Int = PortTester.findPort(endpointPort) val proxyMock: WireMock = new WireMock(host, proxyPort) val proxyServer: WireMockServer = new WireMockServer(wireMockConfig().port(proxyPort)) def withServers(test: => Unit) { endpointServer.start() proxyServer.start() try { test } finally { Try(endpointServer.stop()) Try(proxyServer.stop()) } } } object PortTester { def findPort(excluded: Int*): Int = (6000 to 7000).find(port => !excluded.contains(port) && isFree(port)).getOrElse(throw new Exception("No free port")) private def isFree(port: Int): Boolean = { val triedSocket = Try { val serverSocket = new ServerSocket(port) Try(serverSocket.close()) serverSocket } triedSocket.isSuccess } }
Example 71
Source File: ProcessOverSocketStreamConnectionProvider.scala From intellij-lsp with Apache License 2.0 | 5 votes |
package com.github.gtache.lsp.client.connection import java.io.{IOException, InputStream, OutputStream} import java.net.{ServerSocket, Socket} import java.util.Objects import com.intellij.openapi.diagnostic.Logger class ProcessOverSocketStreamConnectionProvider(commands: Seq[String], workingDir: String, port: Int = 0) extends ProcessStreamConnectionProvider(commands, workingDir) { import ProcessOverSocketStreamConnectionProvider._ private var socket: Socket = _ private var inputStream: InputStream = _ private var outputStream: OutputStream = _ @throws[IOException] override def start(): Unit = { val serverSocket = new ServerSocket(port) val socketThread = new Thread(() => { try socket = serverSocket.accept catch { case e: IOException => LOG.error(e) } finally try serverSocket.close() catch { case e: IOException => LOG.error(e) } }) socketThread.start() super.start() try { socketThread.join(5000) } catch { case e: InterruptedException => LOG.error(e) } if (socket == null) throw new IOException("Unable to make socket connection: " + toString) //$NON-NLS-1$ inputStream = socket.getInputStream outputStream = socket.getOutputStream } override def getInputStream: InputStream = inputStream override def getOutputStream: OutputStream = outputStream override def getErrorStream: InputStream = inputStream override def stop(): Unit = { super.stop() if (socket != null) try socket.close() catch { case e: IOException => LOG.error(e) } } override def hashCode: Int = { val result = super.hashCode result ^ Objects.hashCode(this.port) } } object ProcessOverSocketStreamConnectionProvider { private val LOG = Logger.getInstance(classOf[ProcessOverSocketStreamConnectionProvider]) }
Example 72
Source File: BasicApp.scala From sbt-docker-compose with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.PrintWriter import java.net.ServerSocket object BasicApp extends App { val text = """HTTP/1.0 200 OK Content-Type: text/html Content-Length: 200 <HTML> <HEAD> <TITLE>Hello, World!</TITLE> </HEAD> <BODY LANG="en-US" BGCOLOR="#e6e6ff" DIR="LTR"> <P ALIGN="CENTER"> <FONT FACE="Arial, sans-serif" SIZE="6">Hello, World!</FONT> </P> </BODY> </HTML>""" val port = 8080 val listener = new ServerSocket(port) while (true) { val sock = listener.accept() new PrintWriter(sock.getOutputStream, true).println(text) sock.shutdownOutput() } }
Example 73
Source File: BasicApp.scala From sbt-docker-compose with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.PrintWriter import java.net.ServerSocket object BasicApp extends App { val text = """HTTP/1.0 200 OK Content-Type: text/html Content-Length: 200 <HTML> <HEAD> <TITLE>Hello, World!</TITLE> </HEAD> <BODY LANG="en-US" BGCOLOR="#e6e6ff" DIR="LTR"> <P ALIGN="CENTER"> <FONT FACE="Arial, sans-serif" SIZE="6">Hello, World!</FONT> </P> </BODY> </HTML>""" val port = 8080 val listener = new ServerSocket(port) while (true) { val sock = listener.accept() new PrintWriter(sock.getOutputStream, true).println(text) sock.shutdownOutput() } }
Example 74
Source File: BasicApp.scala From sbt-docker-compose with BSD 3-Clause "New" or "Revised" License | 5 votes |
import java.io.PrintWriter import java.net.ServerSocket object BasicApp extends App { val text = """HTTP/1.0 200 OK Content-Type: text/html Content-Length: 200 <HTML> <HEAD> <TITLE>Hello, World!</TITLE> </HEAD> <BODY LANG="en-US" BGCOLOR="#e6e6ff" DIR="LTR"> <P ALIGN="CENTER"> <FONT FACE="Arial, sans-serif" SIZE="6">Hello, World!</FONT> </P> </BODY> </HTML>""" val port = 8080 val listener = new ServerSocket(port) while (true) { val sock = listener.accept() new PrintWriter(sock.getOutputStream, true).println(text) sock.shutdownOutput() } }
Example 75
Source File: BlockingIoExample.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter1.scaladsl import java.io.{ BufferedReader, IOException, InputStreamReader, PrintWriter } import java.net.ServerSocket // #snip @throws[IOException] def serve(portNumber: Int): Unit = { //创建一个新的 ServerSocket,用以监听指定端口上的连接请求 val serverSocket = new ServerSocket(portNumber) //对accept()方法的调用将被阻塞,直到一个连接建立 val clientSocket = serverSocket.accept //这些流对象都派生于该套接字的流对象 val in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream)) val out = new PrintWriter(clientSocket.getOutputStream, true) var request: String = in.readLine var response: String = null //处理循环开始 while (request ne null) { if ("Done" != request) { //请求被传递给服务器的处理方法 response = processRequest(request) //服务器的响应被发送给了客户端 out.println(response) //继续执行处理循环 } request = in.readLine } // #snip } private def processRequest(request: String): String = "Processed" }
Example 76
Source File: PlainOioServer.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter4 import java.io.IOException import java.net.ServerSocket import java.nio.charset.Charset class PlainOioServer { @throws[IOException] def serve(port: Int): Unit = { //将服务器绑定到指定端口 val socket = new ServerSocket(port) try { while (true) { val clientSocket = socket.accept System.out.println("Accepted connection from " + clientSocket) //创建一个新的线程来处理该连接 new Thread(() ⇒ { try { //将消息写给已连接的客户端 val out = clientSocket.getOutputStream out.write("Hi!\r\n".getBytes(Charset.forName("UTF-8"))) out.flush() //关闭连接 clientSocket.close() } catch { case e: IOException ⇒ e.printStackTrace() } finally { try { clientSocket.close() } catch { case ex: IOException ⇒ // ignore on close } } }).start() //启动线程 } } catch { case e: IOException ⇒ e.printStackTrace() } } }
Example 77
Source File: FreePort.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.testing.postgresql import java.net.{InetAddress, ServerSocket} import com.daml.ports.Port import scala.annotation.tailrec private[postgresql] object FreePort { @tailrec def find(tries: Int = 10): PortLock.Locked = { val socket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress) val portLock = try { val port = Port(socket.getLocalPort) PortLock.lock(port) } finally { socket.close() } portLock match { case Right(locked) => socket.close() locked case Left(failure) => socket.close() if (tries <= 1) { throw failure } else { find(tries - 1) } } } }
Example 78
Source File: UISuite.scala From SparkCore with Apache License 2.0 | 5 votes |
package org.apache.spark.ui import java.net.ServerSocket import scala.io.Source import scala.util.{Failure, Success, Try} import org.eclipse.jetty.servlet.ServletContextHandler import org.scalatest.FunSuite import org.scalatest.concurrent.Eventually._ import org.scalatest.time.SpanSugar._ import org.apache.spark.LocalSparkContext._ import org.apache.spark.{SparkConf, SparkContext} class UISuite extends FunSuite { private def newSparkContext(): SparkContext = { val conf = new SparkConf() .setMaster("local") .setAppName("test") .set("spark.ui.enabled", "true") val sc = new SparkContext(conf) assert(sc.ui.isDefined) sc } ignore("basic ui visibility") { withSpark(newSparkContext()) { sc => // test if the ui is visible, and all the expected tabs are visible eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL(sc.ui.get.appUIAddress).mkString assert(!html.contains("random data that should not be present")) assert(html.toLowerCase.contains("stages")) assert(html.toLowerCase.contains("storage")) assert(html.toLowerCase.contains("environment")) assert(html.toLowerCase.contains("executors")) } } } ignore("visibility at localhost:4040") { withSpark(newSparkContext()) { sc => // test if visible from http://localhost:4040 eventually(timeout(10 seconds), interval(50 milliseconds)) { val html = Source.fromURL("http://localhost:4040").mkString assert(html.toLowerCase.contains("stages")) } } } test("jetty selects different port under contention") { val server = new ServerSocket(0) val startPort = server.getLocalPort val serverInfo1 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) val serverInfo2 = JettyUtils.startJettyServer( "0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf) // Allow some wiggle room in case ports on the machine are under contention val boundPort1 = serverInfo1.boundPort val boundPort2 = serverInfo2.boundPort assert(boundPort1 != startPort) assert(boundPort2 != startPort) assert(boundPort1 != boundPort2) serverInfo1.server.stop() serverInfo2.server.stop() server.close() } test("jetty binds to port 0 correctly") { val serverInfo = JettyUtils.startJettyServer( "0.0.0.0", 0, Seq[ServletContextHandler](), new SparkConf) val server = serverInfo.server val boundPort = serverInfo.boundPort assert(server.getState === "STARTED") assert(boundPort != 0) Try { new ServerSocket(boundPort) } match { case Success(s) => fail("Port %s doesn't seem used by jetty server".format(boundPort)) case Failure(e) => } } test("verify appUIAddress contains the scheme") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val uiAddress = ui.appUIAddress val uiHostPort = ui.appUIHostPort assert(uiAddress.equals("http://" + uiHostPort)) } } test("verify appUIAddress contains the port") { withSpark(newSparkContext()) { sc => val ui = sc.ui.get val splitUIAddress = ui.appUIAddress.split(':') val boundPort = ui.boundPort assert(splitUIAddress(2).toInt == boundPort) } } }
Example 79
Source File: RawTextSender.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.util import java.io.{ByteArrayOutputStream, IOException} import java.net.ServerSocket import java.nio.ByteBuffer import scala.io.Source import org.apache.spark.SparkConf import org.apache.spark.internal.Logging import org.apache.spark.serializer.KryoSerializer import org.apache.spark.util.IntParam private[streaming] object RawTextSender extends Logging { def main(args: Array[String]) { if (args.length != 4) { // scalastyle:off println System.err.println("Usage: RawTextSender <port> <file> <blockSize> <bytesPerSec>") // scalastyle:on println System.exit(1) } // Parse the arguments using a pattern match val Array(IntParam(port), file, IntParam(blockSize), IntParam(bytesPerSec)) = args // Repeat the input data multiple times to fill in a buffer val lines = Source.fromFile(file).getLines().toArray val bufferStream = new ByteArrayOutputStream(blockSize + 1000) val ser = new KryoSerializer(new SparkConf()).newInstance() val serStream = ser.serializeStream(bufferStream) var i = 0 while (bufferStream.size < blockSize) { serStream.writeObject(lines(i)) i = (i + 1) % lines.length } val array = bufferStream.toByteArray val countBuf = ByteBuffer.wrap(new Array[Byte](4)) countBuf.putInt(array.length) countBuf.flip() val serverSocket = new ServerSocket(port) logInfo("Listening on port " + port) while (true) { val socket = serverSocket.accept() logInfo("Got a new connection") val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec) try { while (true) { out.write(countBuf.array) out.write(array) } } catch { case e: IOException => logError("Client disconnected") } finally { socket.close() } } } }
Example 80
Source File: FlumeTestUtils.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.streaming.flume import java.net.{InetSocketAddress, ServerSocket} import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.{List => JList} import java.util.Collections import scala.collection.JavaConverters._ import org.apache.avro.ipc.NettyTransceiver import org.apache.avro.ipc.specific.SpecificRequestor import org.apache.commons.lang3.RandomUtils import org.apache.flume.source.avro import org.apache.flume.source.avro.{AvroFlumeEvent, AvroSourceProtocol} import org.jboss.netty.channel.ChannelPipeline import org.jboss.netty.channel.socket.SocketChannel import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory import org.jboss.netty.handler.codec.compression.{ZlibDecoder, ZlibEncoder} import org.apache.spark.util.Utils import org.apache.spark.SparkConf private class CompressionChannelFactory(compressionLevel: Int) extends NioClientSocketChannelFactory { override def newChannel(pipeline: ChannelPipeline): SocketChannel = { val encoder = new ZlibEncoder(compressionLevel) pipeline.addFirst("deflater", encoder) pipeline.addFirst("inflater", new ZlibDecoder()) super.newChannel(pipeline) } } }
Example 81
Source File: PageViewGenerator.scala From sparkoscope with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.streaming.clickstream import java.io.PrintWriter import java.net.ServerSocket import java.util.Random // scalastyle:on object PageViewGenerator { val pages = Map("http://foo.com/" -> .7, "http://foo.com/news" -> 0.2, "http://foo.com/contact" -> .1) val httpStatus = Map(200 -> .95, 404 -> .05) val userZipCode = Map(94709 -> .5, 94117 -> .5) val userID = Map((1 to 100).map(_ -> .01): _*) def pickFromDistribution[T](inputMap: Map[T, Double]): T = { val rand = new Random().nextDouble() var total = 0.0 for ((item, prob) <- inputMap) { total = total + prob if (total > rand) { return item } } inputMap.take(1).head._1 // Shouldn't get here if probabilities add up to 1.0 } def getNextClickEvent(): String = { val id = pickFromDistribution(userID) val page = pickFromDistribution(pages) val status = pickFromDistribution(httpStatus) val zipCode = pickFromDistribution(userZipCode) new PageView(page, status, zipCode, id).toString() } def main(args: Array[String]) { if (args.length != 2) { System.err.println("Usage: PageViewGenerator <port> <viewsPerSecond>") System.exit(1) } val port = args(0).toInt val viewsPerSecond = args(1).toFloat val sleepDelayMs = (1000.0 / viewsPerSecond).toInt val listener = new ServerSocket(port) println("Listening on port: " + port) while (true) { val socket = listener.accept() new Thread() { override def run(): Unit = { println("Got client connected from: " + socket.getInetAddress) val out = new PrintWriter(socket.getOutputStream(), true) while (true) { Thread.sleep(sleepDelayMs) out.write(getNextClickEvent()) out.flush() } socket.close() } }.start() } } } // scalastyle:on println
Example 82
Source File: RpcUtil.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.rpc.util import java.net.ServerSocket import akka.actor.ActorSystem import org.bitcoins.rpc.client.common.BitcoindRpcClient import scala.annotation.tailrec import scala.concurrent.Future import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration.DurationInt import scala.util.{Failure, Random, Success, Try} abstract class RpcUtil extends AsyncUtil { def awaitServerShutdown( server: BitcoindRpcClient, duration: FiniteDuration = 300.milliseconds, maxTries: Int = 50)(implicit system: ActorSystem): Future[Unit] = { retryUntilSatisfiedF(() => server.isStoppedF, duration, maxTries) } @tailrec final def randomPort: Int = { val MAX = 65535 // max tcp port number val MIN = 1025 // lowest port not requiring sudo val port = Math.abs(Random.nextInt(MAX - MIN) + (MIN + 1)) val attempt = Try { val socket = new ServerSocket(port) socket.close() socket.getLocalPort } attempt match { case Success(value) => value case Failure(_) => randomPort } } } object RpcUtil extends RpcUtil
Example 83
Source File: WindowsPluginFrontend.scala From protoc-bridge with Apache License 2.0 | 5 votes |
package protocbridge.frontend import java.net.ServerSocket import java.nio.file.{Files, Path, Paths} import protocbridge.ProtocCodeGenerator import scala.concurrent.blocking import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future object WindowsPluginFrontend extends PluginFrontend { case class InternalState(batFile: Path) override def prepare(plugin: ProtocCodeGenerator): (Path, InternalState) = { val ss = new ServerSocket(0) val state = createWindowsScript(ss.getLocalPort) Future { blocking { val client = ss.accept() val response = PluginFrontend.runWithInputStream(plugin, client.getInputStream) client.getOutputStream.write(response) client.close() ss.close() } } (state.batFile, state) } override def cleanup(state: InternalState): Unit = { Files.delete(state.batFile) } private def createWindowsScript(port: Int): InternalState = { val classPath = Paths.get(getClass.getProtectionDomain.getCodeSource.getLocation.toURI) val classPathBatchString = classPath.toString.replace("%", "%%") val batchFile = PluginFrontend.createTempFile( ".bat", s"""@echo off |"${sys .props( "java.home" )}\\bin\\java.exe" -cp "$classPathBatchString" ${classOf[ BridgeApp ].getName} $port """.stripMargin ) InternalState(batchFile) } }
Example 84
Source File: ClientTest.scala From bitcoin-s-spv-node with MIT License | 5 votes |
package org.bitcoins.spvnode.networking import java.net.{InetSocketAddress, ServerSocket} import akka.actor.ActorSystem import akka.io.{Inet, Tcp} import akka.testkit.{ImplicitSender, TestActorRef, TestKit, TestProbe} import org.bitcoins.core.config.TestNet3 import org.bitcoins.core.util.{BitcoinSLogger, BitcoinSUtil} import org.bitcoins.spvnode.messages.control.VersionMessage import org.bitcoins.spvnode.messages.{NetworkPayload, VersionMessage} import org.bitcoins.spvnode.util.BitcoinSpvNodeUtil import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FlatSpecLike, MustMatchers} import scala.concurrent.duration._ import scala.util.Try class ClientTest extends TestKit(ActorSystem("ClientTest")) with FlatSpecLike with MustMatchers with ImplicitSender with BeforeAndAfter with BeforeAndAfterAll with BitcoinSLogger { "Client" must "connect to a node on the bitcoin network, " + "send a version message to a peer on the network and receive a version message back, then close that connection" in { val probe = TestProbe() val client = TestActorRef(Client.props,probe.ref) val remote = new InetSocketAddress(TestNet3.dnsSeeds(0), TestNet3.port) val randomPort = 23521 //random port client ! Tcp.Connect(remote, Some(new InetSocketAddress(randomPort))) //val bound : Tcp.Bound = probe.expectMsgType[Tcp.Bound] val conn : Tcp.Connected = probe.expectMsgType[Tcp.Connected] //make sure the socket is currently bound Try(new ServerSocket(randomPort)).isSuccess must be (false) client ! Tcp.Abort val confirmedClosed = probe.expectMsg(Tcp.Aborted) //make sure the port is now available val boundSocket = Try(new ServerSocket(randomPort)) boundSocket.isSuccess must be (true) boundSocket.get.close() } it must "bind connect to two nodes on one port" in { //NOTE if this test case fails it is more than likely because one of the two dns seeds //below is offline val remote1 = new InetSocketAddress(TestNet3.dnsSeeds(0), TestNet3.port) val remote2 = new InetSocketAddress(TestNet3.dnsSeeds(2), TestNet3.port) val probe1 = TestProbe() val probe2 = TestProbe() val client1 = TestActorRef(Client.props, probe1.ref) val client2 = TestActorRef(Client.props, probe2.ref) val local1 = new InetSocketAddress(TestNet3.port) val options = List(Inet.SO.ReuseAddress(true)) client1 ! Tcp.Connect(remote1,Some(local1),options) probe1.expectMsgType[Tcp.Connected] client1 ! Tcp.Abort val local2 = new InetSocketAddress(TestNet3.port) client2 ! Tcp.Connect(remote2,Some(local2),options) probe2.expectMsgType[Tcp.Connected](5.seconds) client2 ! Tcp.Abort } override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } }
Example 85
Source File: Util.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.util import java.io.{BufferedReader, File, FileInputStream, InputStreamReader} import java.net.{ServerSocket, URI} import scala.concurrent.forkjoin.ThreadLocalRandom import scala.sys.process.Process import scala.util.{Failure, Success, Try} import com.typesafe.config.{Config, ConfigFactory} import org.apache.gearpump.cluster.AppJar import org.apache.gearpump.jarstore.JarStoreClient import org.apache.gearpump.transport.HostPort object Util { val LOG = LogUtil.getLogger(getClass) private val defaultUri = new URI("file:///") private val appNamePattern = "^[a-zA-Z_][a-zA-Z0-9_]+$".r.pattern def validApplicationName(appName: String): Boolean = { appNamePattern.matcher(appName).matches() } def getCurrentClassPath: Array[String] = { val classpath = System.getProperty("java.class.path") val classpathList = classpath.split(File.pathSeparator) classpathList } def version: String = { val home = System.getProperty(Constants.GEARPUMP_HOME) val version = Try { val versionFile = new FileInputStream(new File(home, "VERSION")) val reader = new BufferedReader(new InputStreamReader(versionFile)) val version = reader.readLine().replace("version:=", "") versionFile.close() version } version match { case Success(version) => version case Failure(ex) => LOG.error("failed to read VERSION file, " + ex.getMessage) "Unknown-Version" } } def startProcess(options: Array[String], classPath: Array[String], mainClass: String, arguments: Array[String]): RichProcess = { val java = System.getProperty("java.home") + "/bin/java" val command = List(java) ++ options ++ List("-cp", classPath.mkString(File.pathSeparator), mainClass) ++ arguments LOG.info(s"Starting executor process java $mainClass ${arguments.mkString(" ")} " + s"\n ${options.mkString(" ")}") val logger = new ProcessLogRedirector() val process = Process(command).run(logger) new RichProcess(process, logger) } def resolveJvmSetting(conf: Config): AppJvmSettings = { import org.apache.gearpump.util.Constants._ val appMasterVMArgs = Try(conf.getString(GEARPUMP_APPMASTER_ARGS).split("\\s+") .filter(_.nonEmpty)).toOption val executorVMArgs = Try(conf.getString(GEARPUMP_EXECUTOR_ARGS).split("\\s+") .filter(_.nonEmpty)).toOption val appMasterClassPath = Try( conf.getString(GEARPUMP_APPMASTER_EXTRA_CLASSPATH) .split("[;:]").filter(_.nonEmpty)).toOption val executorClassPath = Try( conf.getString(GEARPUMP_EXECUTOR_EXTRA_CLASSPATH) .split(File.pathSeparator).filter(_.nonEmpty)).toOption AppJvmSettings( JvmSetting(appMasterVMArgs.getOrElse(Array.empty[String]), appMasterClassPath.getOrElse(Array.empty[String])), JvmSetting(executorVMArgs .getOrElse(Array.empty[String]), executorClassPath.getOrElse(Array.empty[String]))) } def asSubDirOfGearpumpHome(dir: String): File = { new File(System.getProperty(Constants.GEARPUMP_HOME), dir) } }
Example 86
Source File: ServerInterpreterTest.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.play.server import java.net.ServerSocket import akka.stream.scaladsl.Source import endpoints4s.{Invalid, Valid} import endpoints4s.algebra.server.{ BasicAuthenticationTestSuite, DecodedUrl, EndpointsTestSuite, ChunkedJsonEntitiesTestSuite, SumTypedEntitiesTestSuite, TextEntitiesTestSuite } import play.api.Mode import play.api.routing.Router import play.api.test.FakeRequest import play.core.server.{DefaultNettyServerComponents, NettyServer, ServerConfig} import scala.concurrent.Future class ServerInterpreterTest extends EndpointsTestSuite[EndpointsTestApi] with BasicAuthenticationTestSuite[EndpointsTestApi] with ChunkedJsonEntitiesTestSuite[EndpointsTestApi] with SumTypedEntitiesTestSuite[EndpointsTestApi] with TextEntitiesTestSuite[EndpointsTestApi] { val serverApi: EndpointsTestApi = { object NettyServerComponents extends DefaultNettyServerComponents { override lazy val serverConfig = ServerConfig(mode = Mode.Test) lazy val router = Router.empty } new EndpointsTestApi( PlayComponents.fromBuiltInComponents(NettyServerComponents), Map.empty ) } def serveEndpoint[Resp]( endpoint: serverApi.Endpoint[_, Resp], response: => Resp )(runTests: Int => Unit): Unit = serveRoutes( serverApi.routesFromEndpoints(endpoint.implementedBy(_ => response)) )(runTests) def serveIdentityEndpoint[Resp]( endpoint: serverApi.Endpoint[Resp, Resp] )(runTests: Int => Unit): Unit = serveRoutes( serverApi.routesFromEndpoints(endpoint.implementedBy(request => request)) )(runTests) def serveStreamedEndpoint[Resp]( endpoint: serverApi.Endpoint[_, serverApi.Chunks[Resp]], response: Source[Resp, _] )(runTests: Int => Unit): Unit = serveRoutes( serverApi.routesFromEndpoints(endpoint.implementedBy(_ => response)) )(runTests) def serveStreamedEndpoint[Req, Resp]( endpoint: serverApi.Endpoint[serverApi.Chunks[Req], Resp], logic: Source[Req, _] => Future[Resp] )( runTests: Int => Unit ): Unit = serveRoutes( serverApi.routesFromEndpoints(endpoint.implementedByAsync(logic)) )(runTests) def serveRoutes(routes: Router.Routes)(runTests: Int => Unit): Unit = { val port = { val socket = new ServerSocket(0) try socket.getLocalPort finally if (socket != null) socket.close() } val config = ServerConfig(mode = Mode.Test, port = Some(port)) val server = NettyServer.fromRouterWithComponents(config)(_ => routes) try { runTests(port) } finally { server.stop() } } def decodeUrl[A](url: serverApi.Url[A])(rawValue: String): DecodedUrl[A] = { val request = FakeRequest("GET", rawValue) url.decodeUrl(request) match { case None => DecodedUrl.NotMatched case Some(Invalid(errors)) => DecodedUrl.Malformed(errors) case Some(Valid(a)) => DecodedUrl.Matched(a) } } }