Skip to content

Commit

Permalink
DigixGlobal#70 resend_transactions tests for unavailable ethereum API…
Browse files Browse the repository at this point in the history
…, fixed query class name
  • Loading branch information
bshevchenko committed Sep 11, 2019
1 parent b329457 commit 87c8687
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def resend_transactions
group_size = WatchingTransaction.group(:group_id).count
WatchingTransaction.order('created_at ASC').each do |tx|
ok_tx, data = EthereumApi.get_transaction_by_hash(tx.txhash)
unless ok_tx == :ok && data
if ok_tx == :error
Rails.logger.info "Failed to get transaction by hash. Killing job.."
break
end
unless data
if group_size[tx.group_id] == 1
ok_send, txhash = EthereumApi.send_raw_transaction(tx.signed_transaction)
if ok_send == :ok
Expand Down
2 changes: 1 addition & 1 deletion test/graphql/watched_transaction_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'test_helper'

class UserQueryTest < ActiveSupport::TestCase
class WatchedTransactionQueryTest < ActiveSupport::TestCase
QUERY = <<~EOS
query($txhash: String!) {
watchedTransaction(txhash: $txhash) {
Expand Down
51 changes: 51 additions & 0 deletions test/models/watching_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,55 @@ class WatchingTransactionTest < ActiveSupport::TestCase
assert_equal new_txhash, resent.txhash,
'should update txhash for resent transaction'
end

test 'resend transactions should keep all if API is unavailable' do
user = create(:user)
group_id = SecureRandom.uuid
size = SecureRandom.rand(1..6)
size.times do
FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
end

get_failed_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(status: [500, 'Internal Server Error'])

WatchingTransaction.resend_transactions

assert_requested(get_failed_stub, times: 1)
assert_equal size, WatchingTransaction.where(group_id: group_id).count,
'should work'
end

test 'resend transactions should destroy dropped and keep latest as it is if eth_sendRawTransaction has failed' do
user = create(:user)
group_id = SecureRandom.uuid
size = SecureRandom.rand(1..6)
size.times do
FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
end
sleep 1 # UUID is not sortable, so some delay is needed in order to properly sort via created_at
latest = FactoryBot.create(:watching_transaction, group_id: group_id, user: user)

dropped_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(
body: {
result: nil
}.to_json
)

send_transaction_failed_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_sendRawTransaction/)
.to_return(status: [500, 'Internal Server Error'])

WatchingTransaction.resend_transactions

assert_requested(dropped_stub, times: size + 1)
assert_requested(send_transaction_failed_stub, times: 1)

not_resent = WatchingTransaction.find_by(id: latest.id)
assert_equal latest.txhash, not_resent.txhash,
'should keep txhash'
end
end

0 comments on commit 87c8687

Please sign in to comment.