solution: integration test with two upstreams
This commit is contained in:
@@ -182,7 +182,7 @@ class FilteredApisSpec extends Specification {
|
|||||||
.verify(Duration.ofSeconds(1))
|
.verify(Duration.ofSeconds(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
def "Start with offset"() {
|
def "Start with offset - 5 items"() {
|
||||||
expect:
|
expect:
|
||||||
FilteredApis.startFrom([0, 1, 2, 3, 4], pos) == exp
|
FilteredApis.startFrom([0, 1, 2, 3, 4], pos) == exp
|
||||||
where:
|
where:
|
||||||
@@ -196,6 +196,20 @@ class FilteredApisSpec extends Specification {
|
|||||||
6 | [1, 2, 3, 4, 0]
|
6 | [1, 2, 3, 4, 0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "Start with offset - 2 items"() {
|
||||||
|
expect:
|
||||||
|
FilteredApis.startFrom([0, 1], pos) == exp
|
||||||
|
where:
|
||||||
|
pos | exp
|
||||||
|
0 | [0, 1]
|
||||||
|
1 | [1, 0]
|
||||||
|
2 | [0, 1]
|
||||||
|
3 | [1, 0]
|
||||||
|
4 | [0, 1]
|
||||||
|
5 | [1, 0]
|
||||||
|
6 | [0, 1]
|
||||||
|
}
|
||||||
|
|
||||||
def "Starts with standard"() {
|
def "Starts with standard"() {
|
||||||
setup:
|
setup:
|
||||||
List<Upstream> standard = (0..1).collect {
|
List<Upstream> standard = (0..1).collect {
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ NOTE: Run commands in project's root dir
|
|||||||
cd testing/simple-upstream && ./gradlew run
|
cd testing/simple-upstream && ./gradlew run
|
||||||
----
|
----
|
||||||
|
|
||||||
|
[source,bash]
|
||||||
|
----
|
||||||
|
cd testing/simple-upstream && DSHACKLE_TESTUP_PORT=18546 ./gradlew run
|
||||||
|
----
|
||||||
|
|
||||||
=== Run Dshackle
|
=== Run Dshackle
|
||||||
|
|
||||||
[source,bash]
|
[source,bash]
|
||||||
|
|||||||
@@ -18,6 +18,14 @@ cluster:
|
|||||||
ethereum:
|
ethereum:
|
||||||
rpc:
|
rpc:
|
||||||
url: "http://localhost:18545"
|
url: "http://localhost:18545"
|
||||||
|
- id: test-2
|
||||||
|
chain: ethereum
|
||||||
|
options:
|
||||||
|
disable-validation: true
|
||||||
|
connection:
|
||||||
|
ethereum:
|
||||||
|
rpc:
|
||||||
|
url: "http://localhost:18546"
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
redis:
|
redis:
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package testing
|
||||||
|
|
||||||
|
import java.time.Instant
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
|
||||||
|
class InternalHandler implements CallHandler {
|
||||||
|
|
||||||
|
private AtomicInteger ids = new AtomicInteger()
|
||||||
|
private List<Item> requests = new ArrayList()
|
||||||
|
|
||||||
|
def record(String json) {
|
||||||
|
requests << new Item(ids.getAndIncrement(), json)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Result handle(String method, List<Object> params) {
|
||||||
|
if (method == "eth_call"
|
||||||
|
&& params[0].to?.toLowerCase() == "0x0123456789abcdef0123456789abcdef00000002".toLowerCase()) {
|
||||||
|
return Result.ok(Collections.unmodifiableList(requests))
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
class Item {
|
||||||
|
Integer id
|
||||||
|
String timestamp
|
||||||
|
String json
|
||||||
|
|
||||||
|
Item(Integer id, String json) {
|
||||||
|
this.id = id
|
||||||
|
this.timestamp = Instant.now().toString()
|
||||||
|
this.json = json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package testing
|
||||||
|
|
||||||
|
class PingPongHandler implements CallHandler {
|
||||||
|
@Override
|
||||||
|
Result handle(String method, List<Object> params) {
|
||||||
|
if (method == "eth_call"
|
||||||
|
&& params[0].to?.toLowerCase() == "0x0123456789abcdef0123456789abcdef00000001".toLowerCase()) {
|
||||||
|
return Result.ok([data: params[0].data])
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,17 +5,23 @@ import spark.Spark
|
|||||||
|
|
||||||
class SimpleUpstream {
|
class SimpleUpstream {
|
||||||
|
|
||||||
private int port = 18545
|
private int port = System.getenv("DSHACKLE_TESTUP_PORT")?.toInteger() ?: 18545
|
||||||
private ObjectMapper objectMapper
|
private ObjectMapper objectMapper
|
||||||
|
|
||||||
private List<CallHandler> handlers = []
|
private List<CallHandler> handlers = []
|
||||||
|
private InternalHandler internalHandler
|
||||||
|
|
||||||
void prepare() {
|
void prepare() {
|
||||||
objectMapper = new ObjectMapper()
|
objectMapper = new ObjectMapper()
|
||||||
|
|
||||||
|
internalHandler = new InternalHandler()
|
||||||
|
|
||||||
handlers << new TestcaseHandler(objectMapper)
|
handlers << new TestcaseHandler(objectMapper)
|
||||||
handlers << new CommonHandlers()
|
handlers << new CommonHandlers()
|
||||||
handlers << new BlocksHandler(objectMapper)
|
handlers << new BlocksHandler(objectMapper)
|
||||||
|
handlers << new PingPongHandler()
|
||||||
|
handlers << internalHandler
|
||||||
|
|
||||||
handlers << new InvalidCallHandler()
|
handlers << new InvalidCallHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,9 +29,11 @@ class SimpleUpstream {
|
|||||||
println("Starting upstream on 0.0.0.0:$port")
|
println("Starting upstream on 0.0.0.0:$port")
|
||||||
Spark.port(port)
|
Spark.port(port)
|
||||||
Spark.post("/") { req, resp ->
|
Spark.post("/") { req, resp ->
|
||||||
println("request")
|
|
||||||
try {
|
try {
|
||||||
Map json = objectMapper.readValue(req.body(), Map)
|
def requestBody = req.body()
|
||||||
|
println("request: $requestBody")
|
||||||
|
internalHandler.record(requestBody)
|
||||||
|
Map json = objectMapper.readValue(requestBody, Map)
|
||||||
def id = json["id"]
|
def id = json["id"]
|
||||||
String method = json["method"].toString()
|
String method = json["method"].toString()
|
||||||
List<Object> params = json.containsKey("params") ? json["params"] as List<Object> : []
|
List<Object> params = json.containsKey("params") ? json["params"] as List<Object> : []
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ class ProxyClient {
|
|||||||
return new ProxyClient("http://127.0.0.1:18080/$prefix")
|
return new ProxyClient("http://127.0.0.1:18080/$prefix")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ProxyClient forOriginal(int port) {
|
||||||
|
return new ProxyClient("http://127.0.0.1:$port")
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, Object> execute(String method, List<Object> params) {
|
Map<String, Object> execute(String method, List<Object> params) {
|
||||||
return execute(sequence++, method, params)
|
return execute(sequence++, method, params)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package io.emeraldpay.dshackle.testing.trial.proxy
|
||||||
|
|
||||||
|
import io.emeraldpay.dshackle.testing.trial.ProxyClient
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class DispatchSpec extends Specification {
|
||||||
|
|
||||||
|
def client = ProxyClient.forPrefix("eth")
|
||||||
|
|
||||||
|
def "multiple calls routed roughly equal to upstreams"() {
|
||||||
|
when:
|
||||||
|
def calls1before = ProxyClient.forOriginal(18545).execute("eth_call", [[to: "0x0123456789abcdef0123456789abcdef00000002"]]).result as List
|
||||||
|
def calls2before = ProxyClient.forOriginal(18546).execute("eth_call", [[to: "0x0123456789abcdef0123456789abcdef00000002"]]).result as List
|
||||||
|
|
||||||
|
100.times {
|
||||||
|
client.execute("eth_call", [[to: "0x0123456789abcdef0123456789abcdef00000001", data: "0x00000000" + Integer.toString(it, 16)]])
|
||||||
|
}
|
||||||
|
|
||||||
|
def calls1after = ProxyClient.forOriginal(18545).execute("eth_call", [[to: "0x0123456789abcdef0123456789abcdef00000002"]]).result as List
|
||||||
|
def calls2after = ProxyClient.forOriginal(18546).execute("eth_call", [[to: "0x0123456789abcdef0123456789abcdef00000002"]]).result as List
|
||||||
|
|
||||||
|
def calls1 = onlyNew(calls1before, calls1after)
|
||||||
|
def calls2 = onlyNew(calls2before, calls2after)
|
||||||
|
|
||||||
|
then:
|
||||||
|
calls1.size() >= 48
|
||||||
|
calls2.size() >= 48
|
||||||
|
calls1.size() + calls2.size() == 100
|
||||||
|
}
|
||||||
|
|
||||||
|
private List onlyNew(List before, List after) {
|
||||||
|
return after.findAll { a ->
|
||||||
|
!before.any { b ->
|
||||||
|
b.id == a.id
|
||||||
|
}
|
||||||
|
}.findAll {
|
||||||
|
(it.json as String).contains("0x0123456789abcdef0123456789abcdef00000001")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user