async def test_records_failures():
    connection_tracker = MemoryConnectionTracker()

    node = NodeFactory()
    assert await connection_tracker.should_connect_to(node) is True

    connection_tracker.record_failure(node, HandshakeFailure())

    assert await connection_tracker.should_connect_to(node) is False
    assert connection_tracker._record_exists(node.uri())
async def test_records_failures():
    connection_tracker = MemoryConnectionTracker()

    node = NodeFactory()
    blacklisted_ids = await connection_tracker.get_blacklisted()
    assert node.id not in blacklisted_ids

    connection_tracker.record_failure(node, HandshakeFailure())

    blacklisted_ids = await connection_tracker.get_blacklisted()
    assert node.id in blacklisted_ids
    assert connection_tracker._record_exists(node.id)
async def test_memory_does_not_persist():
    node = NodeFactory()

    connection_tracker_a = MemoryConnectionTracker()
    assert await connection_tracker_a.should_connect_to(node) is True
    connection_tracker_a.record_failure(node, HandshakeFailure())
    assert await connection_tracker_a.should_connect_to(node) is False

    # open a second instance
    connection_tracker_b = MemoryConnectionTracker()

    # the second instance has no memory of the failure
    assert await connection_tracker_b.should_connect_to(node) is True
    assert await connection_tracker_a.should_connect_to(node) is False
async def test_timeout_works():
    node = NodeFactory()

    connection_tracker = MemoryConnectionTracker()
    assert await connection_tracker.should_connect_to(node) is True

    connection_tracker.record_failure(node, HandshakeFailure())
    assert await connection_tracker.should_connect_to(node) is False

    record = connection_tracker._get_record(node.uri())
    record.expires_at -= datetime.timedelta(seconds=120)
    connection_tracker.session.add(record)
    connection_tracker.session.commit()

    assert await connection_tracker.should_connect_to(node) is True
async def test_memory_does_not_persist():
    node = NodeFactory()

    connection_tracker_a = MemoryConnectionTracker()
    blacklisted_ids = await connection_tracker_a.get_blacklisted()
    assert node.id not in blacklisted_ids
    connection_tracker_a.record_failure(node, HandshakeFailure())
    blacklisted_ids = await connection_tracker_a.get_blacklisted()
    assert node.id in blacklisted_ids

    # open a second instance
    connection_tracker_b = MemoryConnectionTracker()

    # the second instance has no memory of the failure
    tracker_b_blacklisted_ids = await connection_tracker_b.get_blacklisted()
    assert node.id not in tracker_b_blacklisted_ids

    tracker_a_blacklisted_ids = await connection_tracker_a.get_blacklisted()
    assert node.id in tracker_a_blacklisted_ids