Skip to content

Commit

Permalink
Merge pull request #139 from f4lco/bugfix/atomic-random-free-port
Browse files Browse the repository at this point in the history
Choose random free ports atomically
  • Loading branch information
javabrett authored Mar 29, 2020
2 parents 12f5651 + a8276a9 commit a6c2a4a
Show file tree
Hide file tree
Showing 26 changed files with 210 additions and 415 deletions.
6 changes: 6 additions & 0 deletions libs/gretty-common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apply from: rootProject.file('common.gradle')

dependencies {
compile "org.codehaus.groovy:groovy:$groovy_version"
compile "org.slf4j:slf4j-api:$slf4j_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.akhikhl.gretty

import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode

@CompileStatic(TypeCheckingMode.SKIP)
class PortUtils {

static final int RANDOM_FREE_PORT = -1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Gretty
*
* Copyright (C) 2013-2015 Andrey Hihlovskiy and contributors.
*
* See the file "LICENSE" for copying and usage permission.
* See the file "CONTRIBUTORS" for complete list of contributors.
*/
package org.akhikhl.gretty

import groovy.util.logging.Slf4j

import java.nio.charset.StandardCharsets
import java.util.concurrent.CompletableFuture

@Slf4j
final class ServiceProtocol {

static Reader createReader(int port = 0) {
final ServerSocket serverSocket = new ServerSocket(port, 1, InetAddress.localHost)
return new Reader(serverSocket)
}

static class Reader implements Closeable {
private final ServerSocket serverSocket

private Reader(final ServerSocket serverSocket) {
this.serverSocket = serverSocket
}

CompletableFuture<String> readMessageAsync() {
return CompletableFuture.supplyAsync(this.&readMessage)
}

synchronized String readMessage() {
def data = new StringBuilder()
Socket acceptSocket = serverSocket.accept()
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(acceptSocket.getInputStream(), StandardCharsets.UTF_8))
while(true) {
String line = reader.readLine()
if(line == '<<EOF>>')
break
data << line
}
} finally {
acceptSocket.close()
}
log.debug 'ServiceProtocol.readMessageFromServerSocket({}) -> {}', serverSocket.getLocalPort(), data
return data
}

int getPort() {
return serverSocket.localPort
}

@Override
void close() {
serverSocket.close()
}
}

static Writer createWriter(int port) {
return new Writer(port)
}

static class Writer {
private final int port

private Writer(final int port) {
this.port = port
}

def write(final String command) {
log.debug 'ServiceProtocol.send({}, {})', port, command
Socket s = new Socket(InetAddress.localHost, port)
try {
OutputStream out = s.getOutputStream()
out.write(("${command}\n<<EOF>>\n").getBytes(StandardCharsets.UTF_8))
out.flush()
} finally {
s.close()
}
}

def writeMayFail(final String command) {
try {
write(command)
} catch (e) {
log.debug(e.getMessage(), e)
}
}

int getPort() {
return port
}
}
}
1 change: 1 addition & 0 deletions libs/gretty-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
compile 'org.bouncycastle:bcprov-jdk15on:1.60'
compile "org.springframework.boot:spring-boot-devtools:$springBootVersion"
compile project(':libs:gretty-common')
}

tasks.processResources {
Expand Down

This file was deleted.

Loading

0 comments on commit a6c2a4a

Please sign in to comment.