from plenum.test.helper import checkViewNoForNodes, sendRandomRequests
from stp_core.common.log import getlogger

logger = getlogger()


def chk_view_change(nodes, newViewNo, wallet, client):
    # verify all nodes have undergone an instance change
    if {n.viewNo for n in nodes} != {newViewNo}:
        tr = []
        for n in nodes:
            tr.append(n.monitor.isMasterThroughputTooLow())
        # If throughput of every node has gone down then check that
        # view has changed
        if all(tr):
            logger.debug('Throughput ratio gone down, its {}'.format(','.join(tr)))
            checkViewNoForNodes(nodes, newViewNo)
        else:
            logger.debug('Master instance has not degraded yet, '
                         'sending more requests')
            sendRandomRequests(wallet, client, 1)
            assert False
    else:
        assert True

from plenum.test.helper import checkViewNoForNodes, sendRandomRequests
from stp_core.common.log import getlogger
from stp_core.loop.eventually import eventually
from plenum.test import waits

logger = getlogger()


def provoke_and_check_view_change(nodes, newViewNo, wallet, client):

    if {n.viewNo for n in nodes} == {newViewNo}:
        return True

    # If throughput of every node has gone down then check that
    # view has changed
    tr = [n.monitor.isMasterThroughputTooLow() for n in nodes]
    if all(tr):
        logger.info('Throughput ratio gone down, its {}'.format(tr))
        checkViewNoForNodes(nodes, newViewNo)
    else:
        logger.info('Master instance has not degraded yet, '
                     'sending more requests')
        sendRandomRequests(wallet, client, 10)
        assert False


def provoke_and_wait_for_view_change(looper,
                                     nodeSet,
                                     expectedViewNo,
                                     wallet,
                                     client,
                                     customTimeout=None):
    timeout = customTimeout or waits.expectedViewChangeTime(len(nodeSet))
    timeout *= 30
    return looper.run(eventually(provoke_and_check_view_change,
                                 nodeSet,
                                 expectedViewNo,
                                 wallet,
                                 client,
                                 timeout=timeout))


update method for forcing view change cna checking it; intoduce wait method for it