solution: integration test with two upstreams

This commit is contained in:
Igor Artamonov
2021-03-24 22:52:25 -04:00
parent be33508bb9
commit 17f14987aa
8 changed files with 130 additions and 4 deletions

View File

@@ -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
}
}
}

View File

@@ -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
}
}

View File

@@ -5,17 +5,23 @@ import spark.Spark
class SimpleUpstream {
private int port = 18545
private int port = System.getenv("DSHACKLE_TESTUP_PORT")?.toInteger() ?: 18545
private ObjectMapper objectMapper
private List<CallHandler> handlers = []
private InternalHandler internalHandler
void prepare() {
objectMapper = new ObjectMapper()
internalHandler = new InternalHandler()
handlers << new TestcaseHandler(objectMapper)
handlers << new CommonHandlers()
handlers << new BlocksHandler(objectMapper)
handlers << new PingPongHandler()
handlers << internalHandler
handlers << new InvalidCallHandler()
}
@@ -23,9 +29,11 @@ class SimpleUpstream {
println("Starting upstream on 0.0.0.0:$port")
Spark.port(port)
Spark.post("/") { req, resp ->
println("request")
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"]
String method = json["method"].toString()
List<Object> params = json.containsKey("params") ? json["params"] as List<Object> : []