Skip to content

Commit a770064

Browse files
committed
Refactor Vert.x test context usage in compliance suites
1 parent f16de4f commit a770064

2 files changed

Lines changed: 76 additions & 26 deletions

File tree

src/test/java/com/inqwise/async/compliance/RFCComplianceTest.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
package com.inqwise.async.compliance;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
import java.util.concurrent.atomic.AtomicInteger;
14+
15+
import org.apache.logging.log4j.LogManager;
16+
import org.apache.logging.log4j.Logger;
17+
import org.junit.jupiter.api.AfterEach;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.junit.jupiter.api.extension.RegisterExtension;
22+
323
import com.inqwise.async.stream.AsyncInputStream;
4-
import com.inqwise.async.stream.AsyncOutputStream;
524
import com.inqwise.async.stream.AsyncReadStream;
625
import com.inqwise.async.stream.AsyncWriteStream;
26+
727
import io.vertx.core.Handler;
828
import io.vertx.core.Vertx;
929
import io.vertx.core.buffer.Buffer;
1030
import io.vertx.core.streams.ReadStream;
11-
import io.vertx.core.streams.WriteStream;
31+
import io.vertx.junit5.RunTestOnContext;
1232
import io.vertx.junit5.Timeout;
1333
import io.vertx.junit5.VertxExtension;
1434
import io.vertx.junit5.VertxTestContext;
15-
import org.junit.jupiter.api.Test;
16-
import org.junit.jupiter.api.extension.ExtendWith;
17-
18-
import java.io.*;
19-
import java.util.concurrent.CompletableFuture;
20-
import java.util.concurrent.TimeUnit;
21-
import java.util.concurrent.atomic.AtomicBoolean;
22-
import java.util.concurrent.atomic.AtomicInteger;
23-
24-
import static org.junit.jupiter.api.Assertions.*;
2535

2636
/**
2737
* RFC Compliance tests for Inqwise Async library.
@@ -31,12 +41,32 @@
3141
@ExtendWith(VertxExtension.class)
3242
@Timeout(10000)
3343
public class RFCComplianceTest {
34-
44+
private static final Logger logger = LogManager.getLogger(RFCComplianceTest.class);
45+
46+
@RegisterExtension
47+
RunTestOnContext rtoc = new RunTestOnContext();
48+
Vertx vertx;
49+
50+
@BeforeEach
51+
void prepare(VertxTestContext testContext) {
52+
vertx = rtoc.vertx();
53+
// Prepare something on a Vert.x event-loop thread
54+
// The thread changes with each test instance
55+
testContext.completeNow();
56+
}
57+
58+
@AfterEach
59+
void cleanUp(VertxTestContext testContext) {
60+
// Clean things up on the same Vert.x event-loop thread
61+
// that called prepare and foo
62+
testContext.completeNow();
63+
}
64+
3565
/**
3666
* RFC-001: Verify that blocking I/O operations do not block the Vert.x event loop
3767
*/
3868
@Test
39-
public void testNonBlockingEventLoopCompliance(Vertx vertx, VertxTestContext testContext) throws Exception {
69+
public void testNonBlockingEventLoopCompliance(VertxTestContext testContext) throws Exception {
4070
String testData = "Event loop should not be blocked";
4171
ByteArrayInputStream inputStream = new ByteArrayInputStream(testData.getBytes());
4272

@@ -45,7 +75,7 @@ public void testNonBlockingEventLoopCompliance(Vertx vertx, VertxTestContext tes
4575
AtomicInteger dataReceived = new AtomicInteger(0);
4676

4777
// Schedule a task to run on the event loop while reading
48-
vertx.setTimer(50, id -> {
78+
vertx.setTimer(100, id -> {
4979
if (Thread.currentThread().getName().contains("eventloop")) {
5080
eventLoopBlocked.set(false); // Event loop is responsive
5181
}
@@ -54,6 +84,7 @@ public void testNonBlockingEventLoopCompliance(Vertx vertx, VertxTestContext tes
5484
asyncReadStream
5585
.exceptionHandler(testContext::failNow)
5686
.handler(buffer -> {
87+
logger.debug("handler");
5788
dataReceived.addAndGet(buffer.length());
5889
// Verify we're on the event loop thread
5990
assertTrue(Thread.currentThread().getName().contains("eventloop"),
@@ -70,7 +101,7 @@ public void testNonBlockingEventLoopCompliance(Vertx vertx, VertxTestContext tes
70101
* RFC-002: Verify proper resource cleanup and lifecycle management
71102
*/
72103
@Test
73-
public void testResourceLifecycleCompliance(Vertx vertx, VertxTestContext testContext) throws Exception {
104+
public void testResourceLifecycleCompliance(VertxTestContext testContext) throws Exception {
74105
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
75106
AsyncWriteStream asyncWriteStream = new AsyncWriteStream(vertx, outputStream);
76107

@@ -100,7 +131,7 @@ public void testResourceLifecycleCompliance(Vertx vertx, VertxTestContext testCo
100131
* RFC-003: Verify back-pressure handling in AsyncInputStream
101132
*/
102133
@Test
103-
public void testBackPressureCompliance(Vertx vertx, VertxTestContext testContext) throws Exception {
134+
public void testBackPressureCompliance(VertxTestContext testContext) throws Exception {
104135
// Create a large buffer to trigger back-pressure
105136
byte[] largeData = new byte[64 * 1024]; // 64KB
106137
for (int i = 0; i < largeData.length; i++) {
@@ -195,7 +226,7 @@ private void end() {
195226
* RFC-004: Verify thread safety of stream operations
196227
*/
197228
@Test
198-
public void testThreadSafetyCompliance(Vertx vertx, VertxTestContext testContext) throws Exception {
229+
public void testThreadSafetyCompliance(VertxTestContext testContext) throws Exception {
199230
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
200231
AsyncWriteStream asyncWriteStream = new AsyncWriteStream(vertx, outputStream);
201232

@@ -240,7 +271,7 @@ public void testThreadSafetyCompliance(Vertx vertx, VertxTestContext testContext
240271
* RFC-005: Verify proper exception propagation and error handling
241272
*/
242273
@Test
243-
public void testExceptionPropagationCompliance(Vertx vertx, VertxTestContext testContext) {
274+
public void testExceptionPropagationCompliance(VertxTestContext testContext) {
244275
InputStream faultyStream = new InputStream() {
245276
private boolean firstCall = true;
246277

@@ -275,7 +306,7 @@ public int read() throws IOException {
275306
* RFC-006: Verify compliance with stream pause/resume semantics
276307
*/
277308
@Test
278-
public void testStreamControlCompliance(Vertx vertx, VertxTestContext testContext) throws Exception {
309+
public void testStreamControlCompliance(VertxTestContext testContext) throws Exception {
279310
String testData = "Pause and resume test data that is longer than usual";
280311
ByteArrayInputStream inputStream = new ByteArrayInputStream(testData.getBytes());
281312

src/test/java/com/inqwise/async/compliance/StreamCompatibilityTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
import io.vertx.core.buffer.Buffer;
1010
import io.vertx.core.streams.ReadStream;
1111
import io.vertx.core.streams.WriteStream;
12+
import io.vertx.junit5.RunTestOnContext;
1213
import io.vertx.junit5.Timeout;
1314
import io.vertx.junit5.VertxExtension;
1415
import io.vertx.junit5.VertxTestContext;
16+
17+
import org.apache.logging.log4j.LogManager;
18+
import org.apache.logging.log4j.Logger;
19+
import org.junit.jupiter.api.BeforeEach;
1520
import org.junit.jupiter.api.Test;
1621
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
1723

1824
import java.io.*;
1925
import java.nio.charset.StandardCharsets;
@@ -31,12 +37,25 @@
3137
@ExtendWith(VertxExtension.class)
3238
@Timeout(10000)
3339
public class StreamCompatibilityTest {
40+
private static final Logger logger = LogManager.getLogger(RFCComplianceTest.class);
41+
42+
@RegisterExtension
43+
RunTestOnContext rtoc = new RunTestOnContext();
44+
Vertx vertx;
45+
46+
@BeforeEach
47+
void prepare(VertxTestContext testContext) {
48+
vertx = rtoc.vertx();
49+
// Prepare something on a Vert.x event-loop thread
50+
// The thread changes with each test instance
51+
testContext.completeNow();
52+
}
3453

3554
/**
3655
* Test bidirectional conversion: InputStream -> ReadStream -> InputStream
3756
*/
3857
@Test
39-
public void testInputStreamBidirectionalCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
58+
public void testInputStreamBidirectionalCompatibility(VertxTestContext testContext) throws Exception {
4059
String originalData = "Test data";
4160
byte[] originalBytes = originalData.getBytes(StandardCharsets.UTF_8);
4261

@@ -62,7 +81,7 @@ public void testInputStreamBidirectionalCompatibility(Vertx vertx, VertxTestCont
6281
* Test bidirectional conversion: OutputStream -> WriteStream -> OutputStream
6382
*/
6483
@Test
65-
public void testOutputStreamBidirectionalCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
84+
public void testOutputStreamBidirectionalCompatibility(VertxTestContext testContext) throws Exception {
6685
String testData = "Bidirectional OutputStream test";
6786

6887
ByteArrayOutputStream originalOutputStream = new ByteArrayOutputStream();
@@ -93,7 +112,7 @@ public void testOutputStreamBidirectionalCompatibility(Vertx vertx, VertxTestCon
93112
* Test stream chaining and data integrity
94113
*/
95114
@Test
96-
public void testStreamChainingCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
115+
public void testStreamChainingCompatibility(VertxTestContext testContext) throws Exception {
97116
String testData = "Stream chaining test";
98117

99118
ByteArrayInputStream source = new ByteArrayInputStream(testData.getBytes());
@@ -124,7 +143,7 @@ public void testStreamChainingCompatibility(Vertx vertx, VertxTestContext testCo
124143
* Test concurrent stream operations compatibility
125144
*/
126145
@Test
127-
public void testConcurrentStreamCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
146+
public void testConcurrentStreamCompatibility(VertxTestContext testContext) throws Exception {
128147
String testData = "Concurrent test data";
129148

130149
ByteArrayInputStream input = new ByteArrayInputStream(testData.getBytes());
@@ -155,7 +174,7 @@ public void testConcurrentStreamCompatibility(Vertx vertx, VertxTestContext test
155174
* Test large data compatibility and memory efficiency
156175
*/
157176
@Test
158-
public void testLargeDataCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
177+
public void testLargeDataCompatibility(VertxTestContext testContext) throws Exception {
159178
// Create 1MB of test data
160179
int dataSize = 1024 * 1024;
161180
byte[] largeData = new byte[dataSize];
@@ -201,7 +220,7 @@ public void testLargeDataCompatibility(Vertx vertx, VertxTestContext testContext
201220
* Test stream compatibility with different character encodings
202221
*/
203222
@Test
204-
public void testEncodingCompatibility(Vertx vertx, VertxTestContext testContext) throws Exception {
223+
public void testEncodingCompatibility(VertxTestContext testContext) throws Exception {
205224
String testData = "Encoding test: Basic ASCII text";
206225
byte[] encodedData = testData.getBytes(StandardCharsets.UTF_8);
207226

0 commit comments

Comments
 (0)