-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #8 Added test setup for debugging in UDFs Co-authored-by: Muhammet Orazov <[email protected]>
- Loading branch information
1 parent
1a34f85
commit 599cf57
Showing
10 changed files
with
248 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.exasol.udfdebugging; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public interface Module { | ||
|
||
public Stream<String> getJvmOptions(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.exasol.udfdebugging; | ||
|
||
public interface ModuleFactory { | ||
|
||
/** | ||
* Get if this module is enabled by system property. | ||
* | ||
* @return {@code true} if this module is enabled | ||
*/ | ||
public boolean isEnabled(); | ||
|
||
/** | ||
* Get the name of the property to enable this module. | ||
* | ||
* @return name of the property to enable this module | ||
*/ | ||
public String getModulePropertyName(); | ||
|
||
/** | ||
* Build the {@link Module}. | ||
* | ||
* @param testHostIpAddress IP address of the host running this UDF Test Setup under which UDFs can reach it | ||
* @return built {@link Module} | ||
*/ | ||
public Module buildModule(final String testHostIpAddress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.exasol.udfdebugging; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.exasol.udfdebugging.modules.debugging.DebuggingModuleFactory; | ||
|
||
/** | ||
* Test setup for testing UDFs in the database. | ||
*/ | ||
public class UdfTestSetup { | ||
private static final List<ModuleFactory> AVAILABLE_MODULES = List.of(new DebuggingModuleFactory()); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(UdfTestSetup.class); | ||
private final List<Module> enabledModules; | ||
|
||
/** | ||
* Create a new instance of {@link UdfTestSetup}. | ||
* | ||
* @param testHostIpAddress IP address of the host running this UDF Test Setup under which UDFs can reach it | ||
*/ | ||
public UdfTestSetup(final String testHostIpAddress) { | ||
this.enabledModules = AVAILABLE_MODULES.stream().filter(ModuleFactory::isEnabled) | ||
.map(moduleFactory -> moduleFactory.buildModule(testHostIpAddress)).collect(Collectors.toList()); | ||
printInfoMessage(); | ||
} | ||
|
||
/** | ||
* Get JVM options required for this setup. | ||
* | ||
* @return array of JVM options | ||
*/ | ||
public String[] getJvmOptions() { | ||
return this.enabledModules.stream().flatMap(Module::getJvmOptions).toArray(String[]::new); | ||
} | ||
|
||
private void printInfoMessage() { | ||
if (LOGGER.isInfoEnabled()) { | ||
LOGGER.info(getInfoMessage()); | ||
} | ||
} | ||
|
||
private String getInfoMessage() { | ||
final StringBuilder messageBuilder = new StringBuilder("Udf Test Setup Configuration:\n"); | ||
AVAILABLE_MODULES.forEach(module -> { | ||
messageBuilder.append(module.getModulePropertyName()); | ||
messageBuilder.append(":\t"); | ||
messageBuilder.append(module.isEnabled() ? "enabled" : "disabled"); | ||
messageBuilder.append("\n"); | ||
}); | ||
return messageBuilder.toString(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/exasol/udfdebugging/modules/AbstractModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.exasol.udfdebugging.modules; | ||
|
||
import com.exasol.udfdebugging.Module; | ||
import com.exasol.udfdebugging.ModuleFactory; | ||
|
||
/** | ||
* Abstract basis for {@link Module}. | ||
*/ | ||
public abstract class AbstractModuleFactory implements ModuleFactory { | ||
private final String moduleProperty; | ||
|
||
/** | ||
* Create a new instance of {@link AbstractModuleFactory}. | ||
* | ||
* @param moduleName name of the module | ||
*/ | ||
protected AbstractModuleFactory(final String moduleName) { | ||
this.moduleProperty = "test." + moduleName; | ||
} | ||
|
||
@Override | ||
public final boolean isEnabled() { | ||
return System.getProperty(this.moduleProperty, "false").equals("true"); | ||
} | ||
|
||
@Override | ||
public String getModulePropertyName() { | ||
return this.moduleProperty; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/exasol/udfdebugging/modules/debugging/DebuggingModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.exasol.udfdebugging.modules.debugging; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import com.exasol.udfdebugging.Module; | ||
|
||
public class DebuggingModule implements Module { | ||
public static final String DEBUGGING_PORT = "8000"; | ||
private final String testHostIpAddress; | ||
|
||
/** | ||
* Create a new instance of {@link DebuggingModule}. | ||
* | ||
* @param testHostIpAddress IP address of the host running this UDF Test Setup under which UDFs can reach it | ||
*/ | ||
public DebuggingModule(final String testHostIpAddress) { | ||
this.testHostIpAddress = testHostIpAddress; | ||
} | ||
|
||
@Override | ||
public Stream<String> getJvmOptions() { | ||
return Stream.of("-agentlib:jdwp=transport=dt_socket,server=n,address=" + this.testHostIpAddress + ":" | ||
+ DEBUGGING_PORT + ",suspend=y"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/exasol/udfdebugging/modules/debugging/DebuggingModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.exasol.udfdebugging.modules.debugging; | ||
|
||
import com.exasol.udfdebugging.Module; | ||
import com.exasol.udfdebugging.modules.AbstractModuleFactory; | ||
|
||
public class DebuggingModuleFactory extends AbstractModuleFactory { | ||
/** | ||
* Create a new instance of {@link DebuggingModuleFactory}. | ||
*/ | ||
public DebuggingModuleFactory() { | ||
super("debug"); | ||
} | ||
|
||
@Override | ||
public Module buildModule(final String testHostIpAddress) { | ||
return new DebuggingModule(testHostIpAddress); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/exasol/udfdebugging/UdfTestSetupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.exasol.udfdebugging; | ||
|
||
import static org.hamcrest.CoreMatchers.hasItem; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class UdfTestSetupTest { | ||
public static final String DEBUG_PROPERTY = "test.debug"; | ||
private static final String EXPECTED_DEBUG_JVM_OPTION = "-agentlib:jdwp=transport=dt_socket,server=n,address=1.2.3.4:8000,suspend=y"; | ||
|
||
@Test | ||
void testDebuggingEnabled() { | ||
System.setProperty(DEBUG_PROPERTY, "true"); | ||
final UdfTestSetup udfTestSetup = new UdfTestSetup("1.2.3.4"); | ||
final List<String> jvmOptions = Arrays.asList(udfTestSetup.getJvmOptions()); | ||
assertThat(jvmOptions, hasItem(EXPECTED_DEBUG_JVM_OPTION)); | ||
} | ||
|
||
@Test | ||
void testDebuggingIsDisabledByDefault() { | ||
System.clearProperty(DEBUG_PROPERTY); | ||
final UdfTestSetup udfTestSetup = new UdfTestSetup("1.2.3.4"); | ||
final List<String> jvmOptions = Arrays.asList(udfTestSetup.getJvmOptions()); | ||
assertThat(jvmOptions, not(hasItem(EXPECTED_DEBUG_JVM_OPTION))); | ||
} | ||
|
||
@Test | ||
void testDebuggingDisabled() { | ||
System.setProperty(DEBUG_PROPERTY, "false"); | ||
final UdfTestSetup udfTestSetup = new UdfTestSetup("1.2.3.4"); | ||
final List<String> jvmOptions = Arrays.asList(udfTestSetup.getJvmOptions()); | ||
assertThat(jvmOptions, not(hasItem(EXPECTED_DEBUG_JVM_OPTION))); | ||
} | ||
} |