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

@@ -105,16 +105,94 @@
 etlogger
+%0Afrom stp_core.loop.eventually import eventually%0Afrom plenum.test import waits
 %0A%0Alogger
@@ -212,18 +212,32 @@
 )%0A%0A%0Adef 
+provoke_and_che
 c
-h
 k_view_c
@@ -281,64 +281,8 @@
 t):%0A
-    # verify all nodes have undergone an instance change
 %0A   
@@ -315,9 +315,9 @@
 es%7D 
-!
+=
 = %7Bn
@@ -339,104 +339,21 @@
     
-tr = %5B%5D%0A        for n in nodes:%0A            tr.append(n.monitor.isMasterThroughputTooLow())%0A    
+return True%0A%0A
     
@@ -412,20 +412,16 @@
 ck that%0A
-    
     # vi
@@ -435,24 +435,83 @@
 changed%0A    
+tr = %5Bn.monitor.isMasterThroughputTooLow() for n in nodes%5D%0A
     if all(t
@@ -522,32 +522,27 @@
         
-    
 logger.
-debug
+info
 ('Throug
@@ -582,27 +582,13 @@
 mat(
-','.join(tr)))%0A    
+tr))%0A
     
@@ -633,30 +633,22 @@
 No)%0A    
- 
-   
 else:%0A
-    
         
@@ -658,13 +658,12 @@
 ger.
-debug
+info
 ('Ma
@@ -721,20 +721,16 @@
         
-    
 'sending
@@ -746,20 +746,16 @@
 uests')%0A
-    
         
@@ -790,22 +790,19 @@
 lient, 1
+0
 )%0A
-    
         
@@ -818,34 +818,679 @@
 lse%0A
-    else:%0A        assert True
+%0A%0Adef provoke_and_wait_for_view_change(looper,%0A                                     nodeSet,%0A                                     expectedViewNo,%0A                                     wallet,%0A                                     client,%0A                                     customTimeout=None):%0A    timeout = customTimeout or waits.expectedViewChangeTime(len(nodeSet))%0A    timeout *= 30%0A    return looper.run(eventually(provoke_and_check_view_change,%0A                                 nodeSet,%0A                                 expectedViewNo,%0A                                 wallet,%0A                                 client,%0A                                 timeout=timeout))%0A
 %0A

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