test_otx.py (6787B)
1 # standard imports 2 import os 3 import logging 4 import unittest 5 6 # external imports 7 from chainlib.chain import ChainSpec 8 9 # local imports 10 from chainqueue.db.models.otx import Otx 11 from chainqueue.db.enum import ( 12 is_alive, 13 is_error_status, 14 ) 15 from chainqueue.sql.state import * 16 17 # test imports 18 from tests.chainqueue_base import TestOtxBase 19 20 logging.basicConfig(level=logging.DEBUG) 21 logg = logging.getLogger() 22 23 24 class TestOtx(TestOtxBase): 25 26 def test_ideal_state_sequence(self): 27 set_ready(self.chain_spec, self.tx_hash, session=self.session) 28 otx = Otx.load(self.tx_hash, session=self.session) 29 self.assertEqual(otx.status, StatusBits.QUEUED) 30 31 set_reserved(self.chain_spec, self.tx_hash) 32 self.session.refresh(otx) 33 self.assertEqual(otx.status, StatusBits.RESERVED) 34 35 set_sent(self.chain_spec, self.tx_hash, session=self.session) 36 self.session.refresh(otx) 37 self.assertEqual(otx.status, StatusBits.IN_NETWORK) 38 39 set_final(self.chain_spec, self.tx_hash, block=1024, session=self.session) 40 self.session.refresh(otx) 41 self.assertFalse(is_alive(otx.status)) 42 self.assertFalse(is_error_status(otx.status)) 43 44 45 def test_send_fail_and_retry(self): 46 set_ready(self.chain_spec, self.tx_hash, session=self.session) 47 otx = Otx.load(self.tx_hash, session=self.session) 48 self.assertEqual(otx.status, StatusBits.QUEUED) 49 50 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 51 self.session.refresh(otx) 52 self.assertEqual(otx.status, StatusBits.RESERVED) 53 54 set_sent(self.chain_spec, self.tx_hash, fail=True, session=self.session) 55 self.session.refresh(otx) 56 self.assertTrue(is_error_status(otx.status)) 57 58 set_ready(self.chain_spec, self.tx_hash, session=self.session) 59 self.session.refresh(otx) 60 self.assertEqual(otx.status & StatusBits.QUEUED, StatusBits.QUEUED) 61 self.assertTrue(is_error_status(otx.status)) 62 63 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 64 self.session.refresh(otx) 65 self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED) 66 self.assertTrue(is_error_status(otx.status)) 67 68 set_sent(self.chain_spec, self.tx_hash, session=self.session) 69 self.session.refresh(otx) 70 self.assertEqual(otx.status, StatusBits.IN_NETWORK) 71 self.assertFalse(is_error_status(otx.status)) 72 73 set_final(self.chain_spec, self.tx_hash, block=1024, session=self.session) 74 self.session.refresh(otx) 75 self.assertFalse(is_alive(otx.status)) 76 self.assertFalse(is_error_status(otx.status)) 77 78 79 def test_fubar(self): 80 set_ready(self.chain_spec, self.tx_hash, session=self.session) 81 otx = Otx.load(self.tx_hash, session=self.session) 82 self.assertEqual(otx.status, StatusBits.QUEUED) 83 84 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 85 self.session.refresh(otx) 86 self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED) 87 88 set_fubar(self.chain_spec, self.tx_hash, session=self.session) 89 self.session.refresh(otx) 90 self.assertTrue(is_error_status(otx.status)) 91 self.assertEqual(otx.status & StatusBits.UNKNOWN_ERROR, StatusBits.UNKNOWN_ERROR) 92 93 94 def test_reject(self): 95 set_ready(self.chain_spec, self.tx_hash, session=self.session) 96 otx = Otx.load(self.tx_hash, session=self.session) 97 self.assertEqual(otx.status, StatusBits.QUEUED) 98 99 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 100 self.session.refresh(otx) 101 self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED) 102 103 set_rejected(self.chain_spec, self.tx_hash, session=self.session) 104 self.session.refresh(otx) 105 self.assertTrue(is_error_status(otx.status)) 106 self.assertEqual(otx.status & StatusBits.NODE_ERROR, StatusBits.NODE_ERROR) 107 108 109 def test_final_fail(self): 110 set_ready(self.chain_spec, self.tx_hash, session=self.session) 111 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 112 set_sent(self.chain_spec, self.tx_hash, session=self.session) 113 set_final(self.chain_spec, self.tx_hash, block=1042, fail=True, session=self.session) 114 otx = Otx.load(self.tx_hash, session=self.session) 115 self.assertFalse(is_alive(otx.status)) 116 self.assertTrue(is_error_status(otx.status)) 117 self.assertEqual(otx.status & StatusBits.NETWORK_ERROR, StatusBits.NETWORK_ERROR) 118 119 120 def test_final_protected(self): 121 set_ready(self.chain_spec, self.tx_hash, session=self.session) 122 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 123 set_sent(self.chain_spec, self.tx_hash, session=self.session) 124 set_final(self.chain_spec, self.tx_hash, block=1042, session=self.session) 125 126 otx = Otx.load(self.tx_hash, session=self.session) 127 self.assertEqual(otx.status & StatusBits.FINAL, StatusBits.FINAL) 128 129 with self.assertRaises(TxStateChangeError): 130 set_ready(self.chain_spec, self.tx_hash, session=self.session) 131 132 with self.assertRaises(TxStateChangeError): 133 set_fubar(self.chain_spec, self.tx_hash, session=self.session) 134 135 with self.assertRaises(TxStateChangeError): 136 set_rejected(self.chain_spec, self.tx_hash, session=self.session) 137 138 set_cancel(self.chain_spec, self.tx_hash, session=self.session) 139 self.session.refresh(otx) 140 self.assertEqual(otx.status & StatusBits.OBSOLETE, 0) 141 142 set_cancel(self.chain_spec, self.tx_hash, manual=True, session=self.session) 143 self.session.refresh(otx) 144 self.assertEqual(otx.status & StatusBits.OBSOLETE, 0) 145 146 with self.assertRaises(TxStateChangeError): 147 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 148 149 with self.assertRaises(TxStateChangeError): 150 set_waitforgas(self.chain_spec, self.tx_hash, session=self.session) 151 152 with self.assertRaises(TxStateChangeError): 153 set_manual(self.chain_spec, self.tx_hash, session=self.session) 154 155 156 def test_manual_persist(self): 157 set_manual(self.chain_spec, self.tx_hash, session=self.session) 158 set_ready(self.chain_spec, self.tx_hash, session=self.session) 159 set_reserved(self.chain_spec, self.tx_hash, session=self.session) 160 set_sent(self.chain_spec, self.tx_hash, session=self.session) 161 set_final(self.chain_spec, self.tx_hash, block=1042, session=self.session) 162 163 otx = Otx.load(self.tx_hash, session=self.session) 164 self.assertEqual(otx.status & StatusBits.MANUAL, StatusBits.MANUAL) 165 166 167 if __name__ == '__main__': 168 unittest.main()