Fix tests
This commit is contained in:
@@ -35,11 +35,11 @@ class BlockTransaction implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* End the current transaction
|
||||
* Commit the transaction to database
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
void end() {
|
||||
void commit() {
|
||||
completed = true
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ import com.devsoap.dbt.data.BlockTransaction
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import groovy.util.logging.Slf4j
|
||||
import ratpack.exec.Promise
|
||||
import ratpack.func.Pair
|
||||
import ratpack.handling.Context
|
||||
import ratpack.handling.Handler
|
||||
import ratpack.http.Status
|
||||
import ratpack.http.client.HttpClient
|
||||
import ratpack.http.client.ReceivedResponse
|
||||
import ratpack.jdbctx.Transaction
|
||||
|
||||
import javax.inject.Inject
|
||||
@@ -41,24 +43,24 @@ class ExecutorHandler implements Handler {
|
||||
return
|
||||
}
|
||||
|
||||
executeCommands(ds, transaction).then {
|
||||
transaction.executed = true
|
||||
|
||||
// Notify ledger of result
|
||||
executeCommands(ds, transaction).onError { e ->
|
||||
log.info("Sending rolled back transaction to ledger")
|
||||
println mapper.writeValueAsString(transaction)
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
ctx.error(e)
|
||||
}
|
||||
}.then {
|
||||
log.info("Updating ledger with execution result")
|
||||
client.post(config.ledger.remoteUrl.toURI(), { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
}).then {
|
||||
if(it.status != Status.OK) {
|
||||
log.error("Failed to update ledger with execution result for transaction $transaction.id")
|
||||
}
|
||||
}
|
||||
|
||||
// Return transaction with result
|
||||
ctx.response.send(mapper.writeValueAsString(transaction))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean validateChain(BlockTransaction transaction) {
|
||||
if(transaction.queries[0].parent != transaction.id) {
|
||||
@@ -81,17 +83,16 @@ class ExecutorHandler implements Handler {
|
||||
def txDs = Transaction.dataSource(ds)
|
||||
def tx = Transaction.create { ds.connection }
|
||||
tx.wrap {
|
||||
Promise.sync {
|
||||
try {
|
||||
transaction.queries.each { block ->
|
||||
try{
|
||||
log.info "Executing $block.query ..."
|
||||
if(block.query.toLowerCase().startsWith("select")){
|
||||
log.info('Saving result from Select query')
|
||||
def result = txDs.connection
|
||||
.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)
|
||||
.executeQuery(block.query)
|
||||
block.result = toMap(result)
|
||||
log.info "Executing $block.query ..."
|
||||
if(block.query.toLowerCase().startsWith("select")){
|
||||
log.info('Saving result from Select query')
|
||||
} else {
|
||||
txDs.connection.createStatement().execute(block.query)
|
||||
}
|
||||
@@ -100,12 +101,13 @@ class ExecutorHandler implements Handler {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
transaction.executed = true
|
||||
Promise.sync { transaction }
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to execute transaction $transaction.id, transaction rolled back", e)
|
||||
tx.rollback()
|
||||
transaction.rolledback = true
|
||||
}
|
||||
transaction
|
||||
Promise.error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.devsoap.dbt.data.BlockTransaction
|
||||
import com.devsoap.dbt.services.LedgerService
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import groovy.util.logging.Slf4j
|
||||
import ratpack.exec.Promise
|
||||
import ratpack.handling.Context
|
||||
import ratpack.handling.Handler
|
||||
import ratpack.http.HttpMethod
|
||||
@@ -24,45 +25,55 @@ class LedgerUpdateTransactionHandler implements Handler {
|
||||
|
||||
@Override
|
||||
void handle(Context ctx) throws Exception {
|
||||
ctx.with {
|
||||
if(ctx.request.method == HttpMethod.POST) {
|
||||
if(!config.executor.remoteUrl) {
|
||||
if (ctx.request.method != HttpMethod.POST) {
|
||||
ctx.next()
|
||||
return
|
||||
}
|
||||
|
||||
if (!config.executor.remoteUrl) {
|
||||
throw new RuntimeException("Executor URL is not set, cannot update transaction")
|
||||
}
|
||||
|
||||
def ledgerService = get(LedgerService)
|
||||
request.body.then { body ->
|
||||
def mapper = get(ObjectMapper)
|
||||
def transaction = mapper.readValue(body.text, BlockTransaction)
|
||||
def ledgerService = ctx.get(LedgerService)
|
||||
ctx.request.body.then { body ->
|
||||
def mapper = ctx.get(ObjectMapper)
|
||||
BlockTransaction transaction = mapper.readValue(body.text, BlockTransaction)
|
||||
log.info("Recieved transaction $transaction.id")
|
||||
ledgerService.fetchTransaction(transaction.id).then {
|
||||
if(it.present) {
|
||||
ledgerService.fetchTransaction(transaction.id).then { Optional<BlockTransaction> t ->
|
||||
t.present ? updateTransaction(ctx, transaction) : newTransaction(ctx, transaction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTransaction(Context ctx, BlockTransaction transaction) {
|
||||
def ledgerService = ctx.get(LedgerService)
|
||||
log.info "Transaction $transaction.id exists, updating transaction"
|
||||
ledgerService.updateTransaction(transaction).then {
|
||||
log.info("Transaction $it updated in ledger")
|
||||
if(transaction.completed && !transaction.executed){
|
||||
if (transaction.completed & !(transaction.executed || transaction.rolledback)) {
|
||||
log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl")
|
||||
redirect(config.executor.remoteUrl)
|
||||
ctx.redirect(config.executor.remoteUrl)
|
||||
} else {
|
||||
render(Jackson.json(transaction))
|
||||
ctx.render(Jackson.json(transaction))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
private void newTransaction(Context ctx, BlockTransaction transaction) {
|
||||
if(transaction.executed || transaction.rolledback) {
|
||||
log.error("Tried to create a already executed transaction $transaction.id")
|
||||
throw new IllegalArgumentException("Cannot create a transaction with executed or rolledback status")
|
||||
}
|
||||
|
||||
def ledgerService = ctx.get(LedgerService)
|
||||
log.info("Creating new transaction")
|
||||
ledgerService.newTransaction(transaction).then {
|
||||
log.info("Transaction $it added to ledger")
|
||||
if(transaction.completed && !transaction.executed){
|
||||
if(transaction.completed){
|
||||
log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl")
|
||||
redirect(config.executor.remoteUrl)
|
||||
ctx.redirect(config.executor.remoteUrl)
|
||||
} else {
|
||||
render(Jackson.json(transaction))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
ctx.render(Jackson.json(transaction))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ class TransactionManagerService implements Service {
|
||||
}
|
||||
|
||||
TransactionBuilder complete() {
|
||||
transaction.end()
|
||||
transaction.commit()
|
||||
this
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
apply plugin: 'org.flywaydb.flyway'
|
||||
|
||||
dependencies {
|
||||
testCompile project(':dbt-core')
|
||||
compile project(':dbt-core')
|
||||
|
||||
testCompile ratpack.dependency('h2')
|
||||
testCompile ratpack.dependency('jdbc-tx')
|
||||
runtime ratpack.dependency('handlebars')
|
||||
runtime ratpack.dependency('h2')
|
||||
runtime ratpack.dependency('jdbc-tx')
|
||||
|
||||
testCompile 'org.flywaydb:flyway-core:5.0.7'
|
||||
testRuntime 'org.slf4j:slf4j-simple:1.7.25'
|
||||
compile 'org.flywaydb:flyway-core:5.0.7'
|
||||
runtime 'org.slf4j:slf4j-simple:1.7.25'
|
||||
|
||||
testCompile ratpack.dependency('test')
|
||||
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
|
||||
|
||||
@@ -18,8 +18,6 @@ ratpack {
|
||||
|
||||
bindings {
|
||||
|
||||
module SessionModule
|
||||
|
||||
module (DBTModule) {
|
||||
it.ledger.remoteUrl = 'http://localhost:8888/ledger'
|
||||
it.executor.remoteUrl = 'http://localhost:8888/executor'
|
||||
|
||||
@@ -18,7 +18,7 @@ class ExecutorSpec extends Specification {
|
||||
setup:
|
||||
def transaction = new BlockTransaction()
|
||||
transaction.execute("SELECT * FROM LOGS")
|
||||
transaction.end()
|
||||
transaction.commit()
|
||||
when:
|
||||
String json = aut.httpClient.requestSpec{ spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
|
||||
@@ -3,15 +3,8 @@ package com.devsoap.dbt
|
||||
import com.devsoap.dbt.data.BlockTransaction
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import groovy.json.JsonSlurper
|
||||
import ratpack.groovy.test.GroovyRatpackMainApplicationUnderTest
|
||||
import ratpack.impose.Imposition
|
||||
import ratpack.impose.Impositions
|
||||
import ratpack.impose.ImpositionsSpec
|
||||
import ratpack.impose.ServerConfigImposition
|
||||
import ratpack.server.RatpackServer
|
||||
import spock.lang.AutoCleanup
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class LedgerSpec extends Specification {
|
||||
@@ -41,7 +34,7 @@ class LedgerSpec extends Specification {
|
||||
setup:
|
||||
def transaction = new BlockTransaction()
|
||||
transaction.execute("SELECT * FROM LOGS")
|
||||
transaction.end()
|
||||
transaction.commit()
|
||||
when:
|
||||
String json = aut.httpClient.requestSpec{ spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
@@ -56,7 +49,7 @@ class LedgerSpec extends Specification {
|
||||
setup:
|
||||
def transaction = new BlockTransaction()
|
||||
transaction.execute("SELECT * FROM LOGS")
|
||||
transaction.end()
|
||||
transaction.commit()
|
||||
when:
|
||||
def response = mapper.readValue(aut.httpClient.requestSpec { spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
@@ -73,7 +66,7 @@ class LedgerSpec extends Specification {
|
||||
def transaction = new BlockTransaction()
|
||||
transaction.execute("INSERT INTO LOGS(LOG_ID,LOG_VALUE) VALUES (1, 'HELLO')")
|
||||
transaction.execute("SELECT * FROM LOGS")
|
||||
transaction.end()
|
||||
transaction.commit()
|
||||
when:
|
||||
String json = aut.httpClient.requestSpec{ spec ->
|
||||
spec.body.text(mapper.writeValueAsString(transaction))
|
||||
|
||||
Reference in New Issue
Block a user