Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Mar 1, 2024
1 parent 5b4b13a commit d3c3046
Showing 1 changed file with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.exasol.udfdebugging.modules.udflogs;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.io.*;
import java.net.Socket;
Expand All @@ -9,49 +12,44 @@
import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class LogRecorderTest {

@Test
void testLogsAreWrittenAsFile() throws Exception {
final List<Path> logFiles = new ArrayList<>();
final LogRecorder logRecorder = new LogRecorder(logFiles::add);
final StreamToLogger connection = new StreamToLogger(logRecorder.getPort());
connection.write("test");
assertThat(logFiles, Matchers.hasSize(1));
assertThat(Files.readString(logFiles.get(0)), Matchers.equalTo("test"));
connection.close();
logRecorder.close();
try (final LogRecorder logRecorder = new LogRecorder(logFiles::add);
final StreamToLogger connection = new StreamToLogger(logRecorder.getPort());) {
connection.write("test");
assertAll(() -> assertThat(logFiles, hasSize(1)),
() -> assertThat(Files.readString(logFiles.get(0)), equalTo("test")));

Check failure on line 26 in src/test/java/com/exasol/udfdebugging/modules/udflogs/LogRecorderTest.java

View workflow job for this annotation

GitHub Actions / Test Report

LogRecorderTest.testLogsAreWrittenAsFile

Multiple Failures (2 failures) java.lang.AssertionError: Expected: a collection with size <1> but: collection size was <0> java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
Raw output
org.opentest4j.MultipleFailuresError: 
Multiple Failures (2 failures)
	java.lang.AssertionError: 
Expected: a collection with size <1>
     but: collection size was <0>
	java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:80)
	at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:44)
	at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:38)
	at org.junit.jupiter.api.Assertions.assertAll(Assertions.java:2944)
	at com.exasol.udfdebugging.modules.udflogs.LogRecorderTest.testLogsAreWrittenAsFile(LogRecorderTest.java:25)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	Suppressed: java.lang.AssertionError: 
Expected: a collection with size <1>
     but: collection size was <0>
		at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
		at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
		at com.exasol.udfdebugging.modules.udflogs.LogRecorderTest.lambda$testLogsAreWrittenAsFile$0(LogRecorderTest.java:25)
		at org.junit.jupiter.api.AssertAll.lambda$assertAll$0(AssertAll.java:68)
		at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
		at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
		at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
		at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
		at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
		at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
		at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
		at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:77)
		... 7 more
	Suppressed: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
		at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
		at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
		at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
		at java.base/java.util.Objects.checkIndex(Objects.java:374)
		at java.base/java.util.ArrayList.get(ArrayList.java:459)
		at com.exasol.udfdebugging.modules.udflogs.LogRecorderTest.lambda$testLogsAreWrittenAsFile$1(LogRecorderTest.java:26)
		at org.junit.jupiter.api.AssertAll.lambda$assertAll$0(AssertAll.java:68)
		at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
		at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
		at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
		at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
		at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
		at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
		at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
		at org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:77)
		... 7 more
}
}

@Test
void testParallelStreams() throws Exception {
final List<Path> logFiles = new ArrayList<>();
final LogRecorder logRecorder = new LogRecorder(logFiles::add);
final StreamToLogger connection1 = new StreamToLogger(logRecorder.getPort());
connection1.write("test");
assertThat(logFiles, Matchers.hasSize(1));
assertThat(Files.readString(logFiles.get(0)), Matchers.equalTo("test"));
final StreamToLogger connection2 = new StreamToLogger(logRecorder.getPort());
connection2.write("other");
assertThat(logFiles, Matchers.hasSize(2));
assertThat(Files.readString(logFiles.get(1)), Matchers.equalTo("other"));
connection1.close();
connection2.close();
logRecorder.close();
try (final LogRecorder logRecorder = new LogRecorder(logFiles::add);
final StreamToLogger connection1 = new StreamToLogger(logRecorder.getPort());) {
connection1.write("test");
assertAll(() -> assertThat(logFiles, hasSize(1)),
() -> assertThat(Files.readString(logFiles.get(0)), equalTo("test")));
try (final StreamToLogger connection2 = new StreamToLogger(logRecorder.getPort())) {
connection2.write("other");
assertAll(() -> assertThat(logFiles, hasSize(2)),
() -> assertThat(Files.readString(logFiles.get(1)), equalTo("other")));
}
}
}

private static class StreamToLogger implements Closeable {
private final Socket socket;
private final OutputStream outputStream;
private final PrintWriter writer;

public StreamToLogger(final int port) throws IOException {
this.socket = new Socket("localhost", port);
this.outputStream = this.socket.getOutputStream();
this.writer = new PrintWriter(this.outputStream);
this.writer = new PrintWriter(this.socket.getOutputStream());
}

@SuppressWarnings("java:S2925") // sleep is required
Expand All @@ -64,8 +62,7 @@ public void write(final String message) throws InterruptedException {
@Override
public void close() throws IOException {
this.writer.close();
this.outputStream.close();
this.socket.close();
}
}
}
}

0 comments on commit d3c3046

Please sign in to comment.