Skip to content

Commit

Permalink
[common] Verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kendarorg committed Feb 21, 2024
1 parent be70528 commit 6c16af9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ usage: runner
that will be replaced with the current timestamp)
-xl <arg> Select remote login
-xw <arg> Select remote password
-v <arg> Log level (default ERROR)
</pre>

Inside the choosen directory you will find simple jsons containing all the data exchanged
Inside the chosen directory you will find simple jsons containing all the data exchanged
with the server AND you can modify it before replaying, to simulate special situations!

## The state machine
Expand Down
19 changes: 17 additions & 2 deletions protocol-runner/src/main/java/org/kendar/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.kendar;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.apache.commons.cli.*;
import org.kendar.amqp.v09.AmqpFileStorage;
import org.kendar.amqp.v09.AmqpProtocol;
Expand All @@ -13,6 +15,7 @@
import org.kendar.sql.jdbc.JdbcReplayProxy;
import org.kendar.sql.jdbc.storage.JdbcFileStorage;
import org.kendar.utils.Sleeper;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.Date;
Expand All @@ -33,6 +36,7 @@ public static void execute(String[] args, Supplier<Boolean> stopWhenFalse) {
options.addOption("xd", true, "Select remote log directory (you can set a {timestamp} value\n" +
"that will be replaced with the current timestamp)");
options.addOption("pl", false, "Replay from log directory");
options.addOption("v", true, "Log level (default ERROR)");

try {
CommandLineParser parser = new DefaultParser();
Expand All @@ -42,6 +46,16 @@ public static void execute(String[] args, Supplier<Boolean> stopWhenFalse) {
var portVal = cmd.getOptionValue("l");
var login = cmd.getOptionValue("xl");
var password = cmd.getOptionValue("xw");
var logLevel = cmd.getOptionValue("v");
if(logLevel==null || logLevel.isEmpty()){
logLevel="ERROR";
}

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

var logger = loggerContext.getLogger("org.kendar");
logger.setLevel(Level.toLevel(logLevel,Level.ERROR));

var connectionString = cmd.getOptionValue("xc");
var logsDir = cmd.getOptionValue("xd");
if (logsDir != null && !logsDir.isEmpty()) {
Expand Down Expand Up @@ -71,12 +85,14 @@ public static void execute(String[] args, Supplier<Boolean> stopWhenFalse) {
throw new Exception("missing protocol (p)");
}
while (stopWhenFalse.get()) {
Thread.yield();
Sleeper.sleep(100);
}
protocolServer.stop();
} catch (Exception ex) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("runner", options);
}
System.out.println("EXITED");
}

private static Boolean stopWhenQuitCommand() {
Expand All @@ -86,7 +102,6 @@ private static Boolean stopWhenQuitCommand() {
try {
if (line != null && line.trim().equalsIgnoreCase("q")) {
System.out.println("Exiting");
protocolServer.stop();
return false;
} else {
System.out.println("Command not recognized: " + line.trim());
Expand Down
16 changes: 16 additions & 0 deletions protocol-runner/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%level] %msg [%logger{0}] %n</pattern>
</encoder>
</appender>

<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
<logger name="com.github" level="ERROR"/>
<logger name="org.testcontainers" level="ERROR"/>

<root level="INFO">
<appender-ref ref="stdout"/>
</root>
</configuration>
19 changes: 15 additions & 4 deletions protocol-runner/src/test/java/org/kenndar/runner/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kenndar.runner;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kendar.Main;
Expand All @@ -22,16 +23,22 @@ public static void beforeClass() {

}


@AfterEach
public void afterEach() {
runTheServer.set(false);
Sleeper.sleep(100);
}
@AfterAll
public static void afterClass() throws Exception {
afterClassBase();
}


private AtomicBoolean runTheServer = new AtomicBoolean(true);
@Test
void testMain() throws Exception {
var runTheServer = new AtomicBoolean(true);

System.out.println("RECORDING ==============================================");
var timestampForThisRun = "" + new Date().getTime();
//RECORDING
var args = new String[]{
Expand All @@ -40,7 +47,8 @@ void testMain() throws Exception {
"-xl", postgresContainer.getUserId(),
"-xw", postgresContainer.getPassword(),
"-xc", postgresContainer.getJdbcUrl(),
"-xd", Path.of("target", "tests", timestampForThisRun).toString()
"-xd", Path.of("target", "tests", timestampForThisRun).toString(),
"-v","DEBUG"
};

var serverThread = new Thread(() -> {
Expand Down Expand Up @@ -81,6 +89,8 @@ void testMain() throws Exception {
Sleeper.sleep(1000);
assertTrue(verifyTestRun.get());

System.out.println("REPLAYING ==============================================");

//REPLAYING
runTheServer.set(true);
verifyTestRun.set(false);
Expand All @@ -91,7 +101,8 @@ void testMain() throws Exception {
"-xw", postgresContainer.getPassword(),
"-xc", postgresContainer.getJdbcUrl(),
"-xd", Path.of("target", "tests", timestampForThisRun).toString(),
"-pl"
"-pl",
"-v","DEBUG"
};

serverThread = new Thread(() -> {
Expand Down
16 changes: 16 additions & 0 deletions protocol-runner/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%level] %msg [%logger{0}] %n</pattern>
</encoder>
</appender>

<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
<logger name="com.github" level="ERROR"/>
<logger name="org.testcontainers" level="ERROR"/>

<root level="ERROR">
<appender-ref ref="stdout"/>
</root>
</configuration>

0 comments on commit 6c16af9

Please sign in to comment.