8000 Tck refactor (#371) · chakra-coder/rsocket-java@89eee98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89eee98

Browse files
authored
Tck refactor (rsocket#371)
use local connection cleanup allow library usage
1 parent ec0d373 commit 89eee98

File tree

13 files changed

+322
-479
lines changed

13 files changed

+322
-479
lines changed

rsocket-tck-drivers/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jar {
3131
dependencies {
3232
compile project(':rsocket-core')
3333
compile project(':rsocket-transport-netty')
34+
compile project(':rsocket-transport-local')
3435
compile project(':rsocket-test')
3536
compile 'com.fasterxml.jackson.core:jackson-core:2.8.8'
3637
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.8'

rsocket-tck-drivers/run.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
22

3-
LATEST_VERSION=$(ls build/libs/reactivesocket-tck-drivers-*-SNAPSHOT.jar | sort -r | head -1)
4-
5-
echo "running latest version $LATEST_VERSION"
3+
LATEST_VERSION=$(ls rsocket-tck-drivers/build/libs/rsocket-tck-drivers-*-SNAPSHOT.jar | sort -r | head -1)
64

75
java -cp "$LATEST_VERSION" io.rsocket.tckdrivers.main.Main "$@"

rsocket-tck-drivers/src/main/java/io/rsocket/tckdrivers/client/JavaClientDriver.java

Lines changed: 37 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@
1515

1616
import static org.junit.Assert.*;
1717

18+
import com.google.common.base.Throwables;
19+
import com.google.common.cache.CacheBuilder;
20+
import com.google.common.cache.CacheLoader;
21+
import com.google.common.cache.LoadingCache;
1822
import io.rsocket.Payload;
1923
import io.rsocket.RSocket;
20-
import io.rsocket.RSocketFactory;
2124
import io.rsocket.tckdrivers.common.ConsoleUtils;
2225
import io.rsocket.tckdrivers.common.EchoSubscription;
2326
import io.rsocket.tckdrivers.common.MySubscriber;
2427
import io.rsocket.tckdrivers.common.ParseChannel;
2528
import io.rsocket.tckdrivers.common.ParseChannelThread;
2629
import io.rsocket.tckdrivers.common.ParseMarble;
27-
import io.rsocket.tckdrivers.common.TckIndividualTest;
30+
import io.rsocket.tckdrivers.common.TckClientTest;
2831
import io.rsocket.tckdrivers.common.Tuple;
29-
import io.rsocket.uri.UriTransportRegistry;
3032
import io.rsocket.util.PayloadImpl;
31-
import java.io.BufferedReader;
32-
import java.io.File;
33-
import java.io.FileNotFoundException;
34-
import java.io.FileReader;
3533
import java.util.ArrayList;
3634
import java.util.HashMap;
3735
import java.util.Iterator;
3836
import java.util.List;
3937
import java.util.Map;
4038
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.ExecutionException;
4140
import java.util.concurrent.atomic.AtomicReference;
4241
import org.reactivestreams.Publisher;
4342
import org.reactivestreams.Subscriber;
4443
import org.reactivestreams.Subscription;
44+
import reactor.core.publisher.Mono;
4545

4646
/**
4747
* This class is the driver for the Java RSocket client. To use with class with the current Java
@@ -51,49 +51,29 @@
5151
*/
5252
public class JavaClientDriver {
5353

54-
private final Map<String, MySubscriber<Payload>> payloadSubscribers;
55-
private final Map<String, MySubscriber<Void>> fnfSubscribers;
56-
private final Map<String, String> idToType;
57-
private final String uri;
58-
priv F438 ate final Map<String, RSocket> clientMap;
54+
private final Map<String, MySubscriber<Payload>> payloadSubscribers = new HashMap<>();
55+
private final Map<String, MySubscriber<Void>> fnfSubscribers = new HashMap<>();
56+
private final Map<String, String> idToType = new HashMap<>();
5957
private final String AGENT = "[CLIENT]";
58+
private final LoadingCache<String, RSocket> clientMap;
6059
private ConsoleUtils consoleUtils = new ConsoleUtils(AGENT);
6160

62-
public JavaClientDriver(String uri) throws FileNotFoundException {
63-
this.payloadSubscribers = new HashMap<>();
64-
this.fnfSubscribers = new HashMap<>();
65-
this.idToType = new HashMap<>();
66-
67-
this.clientMap = new HashMap<>();
68-
this.uri = uri;
69-
}
70-
71-
public enum TestResult {
72-
PASS,
73-
FAIL,
74-
CHANNEL
61+
public JavaClientDriver(Mono<RSocket> clientBuilder) {
62+
clientMap =
63+
CacheBuilder.newBuilder()
64+
.build(
65+
new CacheLoader<String, RSocket>() {
66+
@Override
67+
public RSocket load(String key) throws Exception {
68+
return clientBuilder.block();
69+
}
70+
});
7571
}
7672

77-
/**
78-
* A function that creates a RSocket on a new TCP connection.
79-
*
80-
* @return a RSocket
81-
*/
82-
public RSocket createClient() {
83-
return RSocketFactory.connect()
84-
.transport(UriTransportRegistry.clientForUri(uri))
85-
.start()
86-
.block();
87-
}
88-
/**
89-
* Parses through the commands for each test, and calls handlers that execute the commands.
90-
*
91-
* @param test the list of strings which makes up each test case
92-
* @param name the name of the test
93-
*/
94-
public void runTest(List<String> test, String name) throws Exception {
73+
/** Parses through the commands for each test, and calls handlers that execute the commands. */
74+
public void runTest(TckClientTest test) {
9575
List<String> id = new ArrayList<>();
96-
Iterator<String> iter = test.iterator();
76+
Iterator<String> iter = test.testLines().iterator();
9777
boolean channelTest = false; // tells whether this is a test for channel or not
9878
while (iter.hasNext()) {
9979
String line = iter.next();
@@ -109,7 +89,7 @@ public void runTest(List<String> test, String name) throws Exception {
10989
break;
11090
case "channel":
11191
channelTest = true;
112-
handleChannel(args, iter, name, true);
92+
handleChannel(args, iter, test.name, true);
11393
break;
11494
case "echochannel":
11595
handleEchoChannel(args);
@@ -180,25 +160,19 @@ public void runTest(List<String> test, String name) throws Exception {
180160
/**
181161
* A function that do a look up in the clientMap hashtable. If entry does not exist, it creates
182162
* one.
183-
*
184-
* @param id
185-
* @return a RSocket
186163
*/
187164
private RSocket getClient(String id) {
188-
RSocket client = clientMap.get(id);
189-
if (client == null) {
190-
client = createClient();
191-
clientMap.put(id, client);
165+
try {
166+
return clientMap.get(id);
167+
} catch (ExecutionException e) {
168+
throw Throwables.propagate(e);
192169
}
193-
return client;
194170
}
195171

196172
/**
197173
* This function takes in the arguments for the subscribe command, and subscribes an instance of
198174
* MySubscriber with an initial request of 0 (which means don't immediately make a request) to an
199-
* instance of the corresponding publisher
200-
*
201-
* @param args
175+
* instance of the corresponding publisher.
202176
*/
203177
private void handleSubscribe(String[] args) {
204178
switch (args[2]) {
@@ -238,10 +212,6 @@ private void handleSubscribe(String[] args) {
238212
* This function takes in an iterator that is parsing through the test, and collects all the parts
239213
* that make up the channel functionality. It then create a thread that runs the test, which we
240214
* wait to finish before proceeding with the other tests.
241-
*
242-
* @param args
243-
* @param iter
244-
* @param name
245215
*/
246216
private void handleChannel(String[] args, Iterator<String> iter, String name, boolean pass) {
247217
List<String> commands = new ArrayList<>();
@@ -289,8 +259,6 @@ public void subscribe(Subscriber<? super Payload> s) {
289259
/**
290260
* This handles echo tests. This sets up a channel connection with the EchoSubscription, which we
291261
* pass to the MySubscriber.
292-
*
293-
* @param args
294262
*/
295263
private void handleEchoChannel(String[] args) {
296264
Payload initPayload = new PayloadImpl(args[2], args[3]);
@@ -490,13 +458,13 @@ private void handleCancel(String[] args) {
490458

491459
private void handleEOF() {
492460
MySubscriber<Void> fnfsub = new MySubscriber<>(0L, AGENT);
493-
if (clientMap.size() > 0) {
494-
// Use any Client to send shutdown msg to the server
495-
RSocket fnfclient = clientMap.get(clientMap.keySet().toArray()[0]);
496-
Publisher<Void> fnfpub = fnfclient.fireAndForget(new PayloadImpl("shutdown", "shutdown"));
497-
fnfpub.subscribe(fnfsub);
498-
fnfsub.request(1);
499-
1241 }
461+
//if (clientMap.size() > 0) {
462+
// // Use any Client to send shutdown msg to the server
463+
// RSocket fnfclient = clientMap.get(clientMap.keySet().toArray()[0]);
464+
// Publisher<Void> fnfpub = fnfclient.fireAndForget(new PayloadImpl("shutdown", "shutdown"));
465+
// fnfpub.subscribe(fnfsub);
466+
// fnfsub.request(1);
467+
//}
500468
}
501469

502470
/** A subscription for channel, it handles request(n) by sort of faking an initial payload. */
@@ -529,48 +497,4 @@ public void request(long n) {
529497
if (m > 0) pm.request(m);
530498
}
531499
}
532-
533-
/**
534-
* A function that parses the file and extract the individual tests
535-
*
536-
* @param file The file to read as input.
537-
* @return a list of TckIndividualTest.
538-
*/
539-
public static List<TckIndividualTest> extractTests(File file) throws Exception {
540-
541-
BufferedReader reader = new BufferedReader(new FileReader(file));
542-
List<TckIndividualTest> tests = new ArrayList<>();
543-
List<String> test = new ArrayList<>();
544-
String line = reader.readLine();
545-
String testFile = file.getName().replaceFirst(TckIndividualTest.clientPrefix, "");
546-
547-
//Parsing the input client file to read all the tests
548-
while (line != null) {
549-
switch (line) {
550-
case "!":
551-
String name = "";
552-
if (test.size() > 1) {
553-
name = test.get(0).split("%%")[1];
554-
}
555-
556-
TckIndividualTest tckTest = new TckIndividualTest(name, test, testFile);
557-
tests.add(tckTest);
558-
test = new ArrayList<>();
559-
break;
560-
default:
561-
test.add(line);
562-
break;
563-
}
564-
line = reader.readLine();
565-
}
566-
567-
if (test.size() > 0) {
568-
String name = "";
569-
name = test.get(0).split("%%")[1];
570-
TckIndividualTest tckTest = new TckIndividualTest(name, test, testFile);
571-
tests.add(tckTest);
572-
tests = tests.subList(1, tests.size()); // remove the first list, which is empty
573-
}
574-
return tests;
575-
}
576500
}

rsocket-tck-drivers/src/main/java/io/rsocket/tckdrivers/common/ServerThread.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2016 Facebook, Inc.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.rsocket.tckdrivers.common;
15+
16+
import com.google.common.base.Throwables;
17+
import com.google.common.io.Files;
18+
import io.rsocket.tckdrivers.client.JavaClientDriver;
19+
import io.rsocket.tckdrivers.server.JavaServerDriver;
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
import static java.util.stream.Collectors.toList;
28+
29+
public class TckClientTest {
30+
public String name;
31+
public List<String> test;
32+
33+
public TckClientTest(String name, List<String> test) {
34+
this.name = name;
35+
this.test = test;
36+
}
37+
38+
public List<String> testLines() {
39+
return test;
40+
}
41+
42+
public static List<TckClientTest> extractTests(File file) {
43+
try {
44+
return split(Files.readLines(file, StandardCharsets.UTF_8))
45+
.stream()
46+
.map(testLines -> new TckClientTest(parseName(testLines.remove(0)), testLines))
47+
.collect(toList());
48+
} catch (IOException e) {
49+
throw Throwables.propagate(e);
50+
}
51+
}
52+
53+
private static List<List<String>> split(List<String> lines) {
54+
List<List<String>> testLines = new ArrayList<>();
55+
56+
List<String> test = new ArrayList<>();
57+
58+
for (String line : lines) {
59+
switch (line) {
60+
case "!":
61+
if (!test.isEmpty()) {
62+
testLines.add(test);
63+
test = new ArrayList<>();
64+
}
65+
break;
66+
default:
67+
test.add(line);
68+
}
69+
}
70+
71+
return testLines;
72+
}
73+
74+
private static String parseName(String nameLine) {
75+
return nameLine.split("%%")[1];
76+
}
77+
78+
@Override public String toString() {
79+
return name;
80+
}
81+
}

0 commit comments

Comments
 (0)
0