Skip to content

Commit

Permalink
Yamltester on windows (OpenHFT#653)
Browse files Browse the repository at this point in the history
* GPT-4 generated example

* Fix for windows, and add coverage for DTOs

* Fix for windows, and add coverage for DTOs

---------

Co-authored-by: Peter Lawrey <[email protected]>
  • Loading branch information
peter-lawrey and peter-lawrey authored May 3, 2023
1 parent 8f8dee4 commit d83fc08
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 45 deletions.
22 changes: 8 additions & 14 deletions src/main/java/net/openhft/chronicle/wire/TextMethodTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,13 @@ private void updateOutput() throws IOException {
try {
output2 = BytesUtil.findFile(output);
} catch (FileNotFoundException fnfe) {
File out2 = new File(this.output);
File out = new File(out2.getParentFile(), "out.yaml");
try {
output2 = BytesUtil.findFile(replaceTargetWithSource(input
.replace("in.yaml", "out.yaml"))

);
} catch (FileNotFoundException e) {
File out2 = new File(this.output);
File out = new File(out2.getParentFile(), "out.yaml");
try {
String output2dir = BytesUtil.findFile(replaceTargetWithSource(out.getPath()));
output2 = new File(new File(output2dir).getParentFile(), out2.getName()).getPath();
} catch (FileNotFoundException e2) {
throw fnfe;
}
String output2dir = BytesUtil.findFile(replaceTargetWithSource(out.getPath()));
output2 = new File(new File(output2dir).getParentFile(), out2.getName()).getPath();
} catch (FileNotFoundException e2) {
throw fnfe;
}
}
String actual2 = actual.endsWith("\n") ? actual : (actual + "\n");
Expand Down Expand Up @@ -450,7 +443,8 @@ private static String classNameFor(Throwable t) {
}

private String replaceTargetWithSource(String replace) {
return replace.replace("\\target\\test-classes\\", "\\src\\test\\resources\\")
return replace
.replace('\\', '/')
.replace("/target/test-classes/", "/src/test/resources/");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ else if (Jvm.isDebugEnabled(VanillaMethodReader.class))
private static String errorMsg(CharSequence s, MessageHistory history, long sourceIndex) {

final String identifierType = s.length() != 0 && Character.isDigit(s.charAt(0)) ? "@MethodId" : "method-name";
return "Unknown " + identifierType + "='" + s + "' from " + history.lastSourceId() + " at " +
Long.toHexString(sourceIndex) + " ~ " + (int) sourceIndex;
String msg = "Unknown " + identifierType + "='" + s + "'";
if (history.lastSourceId() >= 0)
msg += " from " + history.lastSourceId() + " at " +
Long.toHexString(sourceIndex) + " ~ " + (int) sourceIndex;
return msg;
}

@Deprecated(/* Not used. To be removed in x.25 */)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.openhft.chronicle.wire.generated;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.MethodReader;
import net.openhft.chronicle.wire.Wire;
import net.openhft.chronicle.wire.WireType;

interface MyService {
void foo(int arg1, String arg2);

int bar(String arg1, long arg2);
}

class MyServiceImpl implements MyService {
@Override
public void foo(int arg1, String arg2) {
System.out.printf("MyServiceImpl.foo(%d, %s)\n", arg1, arg2);
}

@Override
public int bar(String arg1, long arg2) {
System.out.printf("MyServiceImpl.bar(%s, %d)\n", arg1, arg2);
return arg1.length() + (int) arg2;
}
}

public class ChronicleWireMethodExample {

// GPT-4 Generated example
public static void main(String[] args) {

MyService service = new MyServiceImpl();

// Create a MethodWriter
Bytes<?> bytes = Bytes.elasticByteBuffer();
Wire wire = WireType.BINARY.apply(bytes);
MyService writer = wire.methodWriter(MyService.class);

// Call methods on the MethodWriter
writer.foo(123, "hello world");
int result = writer.bar("hello", 12345L);

// Create a MethodReader
Wire wire2 = WireType.BINARY.apply(bytes);
int counter = 0;
MethodReader reader = wire2.methodReader(service, new MyServiceImpl());

// Read and process the method calls from the BytesStore
while (reader.readOne()) {
counter++;
}

// Check that the correct number of method calls were read
assert (counter == 2);

// Print the return value of the second method call
System.out.println("Result: " + result);

// Clean up
bytes.releaseLast();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void tearDown() {
@Test
public void runTester() {
ignoreException(" to the classpath");
expectException("Unknown method-name='unknownMethod' ");
expectException("Unknown method-name='unknownMethod'");
expectException((ExceptionKey ek) -> ek.throwable instanceof StackTrace, "StackTrace");
expectException((ExceptionKey ek) -> ek.throwable instanceof ErrorsImpl.MyAssertionError, "MyAssertionError");

Expand Down
12 changes: 0 additions & 12 deletions src/test/java/run/chronicle/account/AccountsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ public void accountStatus(AccountStatus accountStatus) throws InvalidMarshallabl
sendAccountStatusFailed(accountStatus, "target mismatch");
return;
}
if (accountStatus.name() == null || accountStatus.name().isEmpty()) {
sendAccountStatusFailed(accountStatus, "name not set");
return;
}
if (accountStatus.account() == 0) {
sendAccountStatusFailed(accountStatus, "account not set");
return;
}
if (accountStatus.currency() == 0) {
sendAccountStatusFailed(accountStatus, "currency not set");
return;
}
if (!(accountStatus.amount() >= 0)) {
sendAccountStatusFailed(accountStatus, "invalid amount");
return;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/run/chronicle/account/AccountsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static List<Object[]> parameters() {
.agitators(
YamlAgitator.messageMissing(),
YamlAgitator.duplicateMessage(),
YamlAgitator.overrideFields("currency: , amount: NaN, amount: -1".split(", *")),
YamlAgitator.overrideFields("currency: , amount: NaN, amount: -1, target: no-vault".split(", *")),
YamlAgitator.missingFields("name, account, sender, target, sendingTime, from, to, currency, amount, reference".split(", *")))
.exceptionHandlerFunction(out -> (log, msg, thrown) -> ((ErrorListener) out).jvmError(thrown == null ? msg : (msg + " " + thrown)))
.exceptionHandlerFunctionAndLog(true)
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/run/chronicle/account/dto/AbstractEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ public E target(long target) {
return (E) this;
}

public long sendingTime() {
return sendingTime;
}

public E sendingTime(long sendingTime) {
this.sendingTime = sendingTime;
return (E) this;
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/run/chronicle/account/dto/AccountStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public AccountStatus account(long account) {
return this;
}

public String name() {
return name;
}

public AccountStatus name(String name) {
this.name = name;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package run.chronicle.account.dto;

import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.converter.Base85;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.*;
import static run.chronicle.account.dto.AccountStatusTest.getAccountStatus;

public class AccountStatusFailedTest {
@Test
public void testFromString() {
AccountStatusFailed asf = Marshallable.fromString(""+
"!run.chronicle.account.dto.AccountStatusFailed {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001/02/03T04:05:06.007008009,\n" +
" accountStatus: {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001-02-03T04:05:06.007008009,\n" +
" name: name,\n" +
" account: 2,\n" +
" currency: CURR,\n" +
" amount: 1.0" +
" },\n" +
" reason: reasons\n" +
"}");
assertEquals("sender", Base85.INSTANCE.asString(asf.sender()));
assertEquals("target", Base85.INSTANCE.asString(asf.target()));
assertEquals("reasons", asf.reason());
assertEquals(getAccountStatus(), asf.accountStatus());
}

}
33 changes: 33 additions & 0 deletions src/test/java/run/chronicle/account/dto/AccountStatusOKTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package run.chronicle.account.dto;

import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.converter.Base85;
import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static run.chronicle.account.dto.AccountStatusTest.getAccountStatus;

public class AccountStatusOKTest {
@Test
public void testFromString() {
AccountStatusOK asf = Marshallable.fromString(""+
"!run.chronicle.account.dto.AccountStatusOK {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001/02/03T04:05:06.007008009,\n" +
" accountStatus: {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001-02-03T04:05:06.007008009,\n" +
" name: name,\n" +
" account: 2,\n" +
" currency: CURR,\n" +
" amount: 1.0" +
" }\n" +
"}");
assertEquals("sender", Base85.INSTANCE.asString(asf.sender()));
assertEquals("target", Base85.INSTANCE.asString(asf.target()));
assertEquals(getAccountStatus(), asf.accountStatus());
}

}
35 changes: 35 additions & 0 deletions src/test/java/run/chronicle/account/dto/AccountStatusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package run.chronicle.account.dto;

import net.openhft.chronicle.wire.converter.Base85;
import net.openhft.chronicle.wire.converter.NanoTime;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AccountStatusTest {
@Test
public void testToString() {
assertEquals("" +
"!run.chronicle.account.dto.AccountStatus {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001-02-03T04:05:06.007008009,\n" +
" name: name,\n" +
" account: 2,\n" +
" currency: CURR,\n" +
" amount: 1.0\n" +
"}\n",
getAccountStatus().toString());
}

static AccountStatus getAccountStatus() {
return new AccountStatus()
.sender(Base85.INSTANCE.parse("sender"))
.target(Base85.INSTANCE.parse("target"))
.sendingTime(NanoTime.INSTANCE.parse("2001/02/03T04:05:06.007008009"))
.amount(1)
.account(2)
.currency((int) Base85.INSTANCE.parse("CURR"))
.name("name");
}
}
4 changes: 0 additions & 4 deletions src/test/java/run/chronicle/account/dto/Transfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public Transfer amount(double amount) {
return this;
}

public Bytes reference() {
return reference;
}

public Transfer reference(Bytes reference) {
this.reference.clear().append(reference);
return this;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/run/chronicle/account/dto/TransferFailedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package run.chronicle.account.dto;

import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.converter.Base85;
import net.openhft.chronicle.wire.converter.NanoTime;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TransferFailedTest {

public static final String EXPECTED = "" +
"!run.chronicle.account.dto.TransferFailed {\n" +
" sender: target,\n" +
" target: sender,\n" +
" sendingTime: 2001-02-03T04:05:06.777888999,\n" +
" transfer: {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001-02-03T04:05:06.007008009,\n" +
" from: 12345,\n" +
" to: 67890,\n" +
" currency: CURR,\n" +
" amount: 1.0,\n" +
" reference: reference\n" +
" },\n" +
" reason: reasons\n" +
"}\n";

@Test
public void testToString() {
assertEquals(EXPECTED,
new TransferFailed()
.target(Base85.INSTANCE.parse("sender"))
.sender(Base85.INSTANCE.parse("target"))
.sendingTime(NanoTime.INSTANCE.parse("2001/02/03T04:05:06.777888999"))
.reason("reasons")
.transfer(TransferTest.getTransfer())
.toString());
}

@Test
public void testFromString() {
TransferFailed tf = Marshallable.fromString(EXPECTED);
assertEquals(TransferTest.getTransfer(), tf.transfer());
assertEquals("reasons", tf.reason());
}
}
47 changes: 47 additions & 0 deletions src/test/java/run/chronicle/account/dto/TransferOKTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package run.chronicle.account.dto;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.converter.Base85;
import net.openhft.chronicle.wire.converter.NanoTime;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static run.chronicle.account.dto.TransferTest.getTransfer;

public class TransferOKTest {

public static final String EXPECTED = "" +
"!run.chronicle.account.dto.TransferOK {\n" +
" sender: target,\n" +
" target: sender,\n" +
" sendingTime: 2001-02-03T04:05:06.777888999,\n" +
" transfer: {\n" +
" sender: sender,\n" +
" target: target,\n" +
" sendingTime: 2001-02-03T04:05:06.007008009,\n" +
" from: 12345,\n" +
" to: 67890,\n" +
" currency: CURR,\n" +
" amount: 1.0,\n" +
" reference: reference\n" +
" }\n" +
"}\n";

@Test
public void testToString() {
assertEquals(EXPECTED,
new TransferOK()
.target(Base85.INSTANCE.parse("sender"))
.sender(Base85.INSTANCE.parse("target"))
.sendingTime(NanoTime.INSTANCE.parse("2001/02/03T04:05:06.777888999"))
.transfer(getTransfer())
.toString());
}

@Test
public void testFromString() {
TransferOK tok = Marshallable.fromString(EXPECTED);
assertEquals(TransferTest.getTransfer(), tok.transfer());
}
}
Loading

0 comments on commit d83fc08

Please sign in to comment.