def worker_thread2(ftf, connection):
     tm = TransactionManager()
     tm.resolve_missing_transactions(tm2.get_top_hash(), sm.get_transactions(ftf, connection))
     self.assertEqual(tm.get_transaction(2)['data'], 'foo5')
     self.assertEqual(tm.get_transaction(3)['data'], 'foo2')
     self.assertEqual(tm.get_transaction(4)['data'], 'foo3')
     self.assertEqual(tm.get_transaction(5)['data'], 'foo4')
     passes.append(True)
    def test_transaction_resolution(self):
        self.tm.add_transaction("foo0", 1, "bubba", "gump", uuid="1")
        self.tm.add_transaction("foo1", 2, "bubba", "gump", uuid="2")
        self.tm.add_transaction("foo2", 3, "bubba", "gump", uuid="3")
        self.tm.add_transaction("foo3", 4, "bubba", "gump", uuid="4")
        self.tm.add_transaction("foo4", 5, "bubba", "gump", uuid="5")

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 5)
        for i in xrange(0, 5):
            self.assertEqual(tm2.get_transaction(i + 1)["data"], "foo%s" % i)

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.add_transaction("foo0", 1, "bubba", "gump", uuid="1")
        tm2.add_transaction("foo1", 2, "bubba", "gump", uuid="2")
        tm2.add_transaction("foo3", 4, "bubba", "gump", uuid="4")
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 5)
        for i in xrange(0, 5):
            self.assertEqual(tm2.get_transaction(i + 1)["data"], "foo%s" % i)
class TestTransactionManager(TestCase):
    def setUp(self):
        self.tm = TransactionManager()
        self.tm._clear()

    def test_init(self):
        tm = self.tm
        self.assertEqual(tm.current_id, 0)
        self.assertEqual(tm.top_hash, None)

    def test_bubble_up(self):
        self.tm.set_val(0, "hi")
        self.tm.set_val(1, "there")
        self.tm.set_val(2, "buddy")
        self.tm.current_id = 2
        self.tm._bubble_up(1)
        self.assertEqual(self.tm.get_val(3), "buddy")
        self.assertEqual(self.tm.get_val(2), "there")
        self.assertEqual(self.tm.get_val(0), "hi")
        self.tm.current_id = 3
        self.tm._bubble_up(3)
        self.assertEqual(self.tm.get_val(4), "buddy")

    def test_add_transaction(self):
        self.tm.add_transaction("foo", 12345, "bubba", "gump")
        self.assertEqual(self.tm.current_id, 1)
        item = self.tm.get_transaction(1)
        self.assertEqual(item["data"], "foo")
        self.assertEqual(item["ts"], 12345)
        self.assertEqual(item["user"], "bubba")
        self.assertEqual(item["signature"], "gump")
        self.assertIsNotNone(item["hash"])

        self.tm.add_transaction("fooz", 12349, "bubbas", "gumps")
        old_top_hash = self.tm.top_hash
        self.tm.add_transaction("middle", 12346, "blubba", "gumple")

        bottom = self.tm.get_transaction(1)
        self.assertEqual(bottom["data"], "foo")
        middle = self.tm.get_transaction(2)
        self.assertEqual(middle["data"], "middle")
        top = self.tm.get_transaction(3)
        self.assertEqual(top["data"], "fooz")
        self.assertNotEqual(self.tm.top_hash, old_top_hash)

    def test_transaction_resolution(self):
        self.tm.add_transaction("foo0", 1, "bubba", "gump", uuid="1")
        self.tm.add_transaction("foo1", 2, "bubba", "gump", uuid="2")
        self.tm.add_transaction("foo2", 3, "bubba", "gump", uuid="3")
        self.tm.add_transaction("foo3", 4, "bubba", "gump", uuid="4")
        self.tm.add_transaction("foo4", 5, "bubba", "gump", uuid="5")

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 5)
        for i in xrange(0, 5):
            self.assertEqual(tm2.get_transaction(i + 1)["data"], "foo%s" % i)

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.add_transaction("foo0", 1, "bubba", "gump", uuid="1")
        tm2.add_transaction("foo1", 2, "bubba", "gump", uuid="2")
        tm2.add_transaction("foo3", 4, "bubba", "gump", uuid="4")
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 5)
        for i in xrange(0, 5):
            self.assertEqual(tm2.get_transaction(i + 1)["data"], "foo%s" % i)

    def test_transaction_resolution_multiple_same_timestamp(self):
        self.tm.add_transaction("foo0", 1, "bubba", "gump", uuid="1")
        self.tm.add_transaction("foo1", 1, "bubba", "gump", uuid="2")
        self.tm.add_transaction("foo2", 1, "bubba", "gump", uuid="3")

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.add_transaction("foo1", 1, "bubba", "gump", uuid="2")
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 3)
        self.assertEqual(self.tm.top_hash, tm2.top_hash)

    def test_transaction_resolution_same_ts_diff_data(self):
        self.tm.add_transaction("foo0", 1, "bubba", "gump")

        tm2 = TransactionManager("test_tm.pickle")
        tm2._clear()
        tm2.add_transaction("foo1", 1, "bubba", "gump")
        tm2.resolve_missing_transactions(self.tm.top_hash, self.tm.get_transactions_recent_to_old())
        self.assertEqual(tm2.current_id, 2)

    def test_current_id_loads(self):
        self.tm.add_transaction("foo0", 1, "bubba", "gump", uuid="1")

        tm = TransactionManager()
        self.assertEqual(tm.current_id, 1)