def test_connection_switch_workload():
    qname = 'swapworkloadq'
    first = disq.Disque(port=7711, record_job_origin=True)

    # add jobs on both nodes, but more on node 1
    for _ in range(90):
        first.addjob(qname, 'foobar')
    second = disq.Disque(port=7712, record_job_origin=True)
    for _ in range(10):
        second.addjob(qname, 'foobar')

    # Test that by default it uses the initially connected node (2)
    conn, node = second._get_connection('GETJOB')
    assert node == second.default_node
    second._release_connection(conn, node)

    # get a bunch of jobs
    for _ in range(100):
        second.getjob(qname)

    assert first.default_node in second._job_score.keys()

    # assert that the preferred read node is node 1
    conn, node = second._get_connection('GETJOB')
    assert node != second.default_node
    assert node == first.default_node
    second._release_connection(conn, node)

    # assert that the preferred write node is still node 2
    conn, node = second._get_connection('ADDJOB')
    assert node == second.default_node
    assert node != first.default_node
    second._release_connection(conn, node)
dataset/ETHPy150Open ryansb/disq/tests/test_connection.py/test_connection_switch_workload/original