def test_log(self): "Test the log function" with DummyFile() as f: log.log("foobar") #A single item has been written, and then flushed: self.assertEqual(len(f.data), 2) self.assertEqual(type(f.data[0]), str) self.assertEqual(f.data[1], f.flush) line = f.data[0] #The expected data is present, including a trailing newline: self.assertTrue(line.endswith("foobar\n"))
def test_log(self): "Test the log function" with DummyFile() as f: log.log("foobar") #A single item has been written, and then flushed: self.assertEqual(len(f.data), 2) self.assertEqual(type(f.data[0]), str) self.assertEqual(f.data[1], f.flush) line = f.data[0] #The expected data is present, including a trailing newline: self.assertTrue(line.endswith("foobar\n"))
def test_payerSide(self): 'Test behavior when no route is found on the payer side' log.log('\n\n\n\nSCENARIO TEST: largenetwork_haveNoRoute.py test_payerSide\n') #Make a copy, where the link between 2 and 4 is broken: linkDefinitions = copy.deepcopy(largenetwork_setup.linkDefinitions_global) linkDefinitions[2].remove(4) linkDefinitions[4].remove(2) for s in largenetwork_setup.makeNodes(linkDefinitions): newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_payerSide(self): 'Test behavior when no route is found on the payer side' log.log( '\n\n\n\nSCENARIO TEST: largenetwork_haveNoRoute.py test_payerSide\n' ) #Make a copy, where the link between 2 and 4 is broken: linkDefinitions = copy.deepcopy( largenetwork_setup.linkDefinitions_global) linkDefinitions[2].remove(4) linkDefinitions[4].remove(2) for s in largenetwork_setup.makeNodes(linkDefinitions): newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_payerSide(self): 'Test behavior when route times out on the payer side' log.log('\n\n\n\nSCENARIO TEST: largenetwork_routeTimeout.py test_payerSide\n') settings = largenetwork_setup.makeNodes() #Let node 2 ignore MakeRoute: with open(settings[2].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_IgnoreMakeRoute') with open(settings[2].stateFile, 'wb') as f: f.write(stateData) for s in settings: newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_lockDelay(self): 'Test behavior when the lock is delayed until after reservation time-out' log.log('\n\n\n\nSCENARIO TEST: largenetwork_lockDelay.py\n') settings = largenetwork_setup.makeNodes() #Let node 2 delay the Lock: with open(settings[2].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_LockDelay') with open(settings[2].stateFile, 'wb') as f: f.write(stateData) for s in settings: newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_noLock(self): 'Test behavior when no lock happens' log.log('\n\n\n\nSCENARIO TEST: largenetwork_noLock.py\n') settings = largenetwork_setup.makeNodes() #Let node 0 (the paying node) generate no lock: with open(settings[0].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_NoLock') with open(settings[0].stateFile, 'wb') as f: f.write(stateData) for s in settings: newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_noLock(self): 'Test behavior when no lock happens' log.log('\n\n\n\nSCENARIO TEST: largenetwork_noLock.py\n') settings = largenetwork_setup.makeNodes() #Let node 0 (the paying node) generate no lock: with open(settings[0].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_NoLock') with open(settings[0].stateFile, 'wb') as f: f.write(stateData) for s in settings: newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_lockDelay(self): 'Test behavior when the lock is delayed until after reservation time-out' log.log('\n\n\n\nSCENARIO TEST: largenetwork_lockDelay.py\n') settings = largenetwork_setup.makeNodes() #Let node 2 delay the Lock: with open(settings[2].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_LockDelay') with open(settings[2].stateFile, 'wb') as f: f.write(stateData) for s in settings: newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_noCommit(self): 'Test behavior when no commit happens' log.log('\n\n\n\nSCENARIO TEST: largenetwork_noCommit.py\n') settings = largenetwork_setup.makeNodes() #Let node 7 (the receiving node) generate no commit: with open(settings[7].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_NoCommit') with open(settings[7].stateFile, 'wb') as f: f.write(stateData) for s in settings: s.timeoutIncrement = 3 newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_noCommit(self): 'Test behavior when no commit happens' log.log('\n\n\n\nSCENARIO TEST: largenetwork_noCommit.py\n') settings = largenetwork_setup.makeNodes() #Let node 7 (the receiving node) generate no commit: with open(settings[7].stateFile, 'rb') as f: stateData = f.read() stateData = stateData.replace('NodeState', 'NodeState_NoCommit') with open(settings[7].stateFile, 'wb') as f: f.write(stateData) for s in settings: s.timeoutIncrement = 3 newNode = node.Node(s) newNode.start() self.nodes.append(newNode) #Allow links to connect time.sleep(3) self.checkCancelledState()
def test_success(self): 'Test successfully performing a transaction' log.log('\n\n\n\nSCENARIO TEST: largenetwork_success.py test_succes\n') data = [n.list() for n in self.nodes] if verbose: print 'Before payment:' for i, d in enumerate(data): self.printNodeInfo(i, d) beforeBalances = [self.getBalance(d) for d in data] t0 = time.time() #Pay from 0 to 7: URL = self.nodes[7].request(123, 'receipt') if verbose: print 'Payment URL:', URL amount, receipt = self.nodes[0].pay(URL) paymentState = self.nodes[0].confirmPayment(True) if verbose: print 'Payment is ', paymentState self.assertEqual(paymentState, 'committed') t1 = time.time() if verbose: print 'Payment took %f seconds' % (t1 - t0) #Allow paylink to disconnect time.sleep(0.5) data = [n.list() for n in self.nodes] if verbose: print 'After payment:' for i, d in enumerate(data): self.printNodeInfo(i, d) #Check balance changes: afterBalances = [self.getBalance(d) for d in data] balanceChanges = [ afterBalances[i] - beforeBalances[i] for i in range(len(self.nodes)) ] for i, change in enumerate(balanceChanges): if i == 0: expectedChange = -123 elif i == 7: expectedChange = 123 else: expectedChange = 0 self.assertEqual(change, expectedChange) #Check channel consistency between peers: for i, d in enumerate(data): for name, link_a in d['links'].iteritems(): p = len('link_to_') j = int(name[p:]) link_b = data[j]['links']['link_to_%d' % i] chn_a = link_a['channels'][0] chn_b = link_b['channels'][0] self.assertEqual(chn_a['amountLocal'], chn_b['amountRemote']) self.assertEqual(chn_a['amountRemote'], chn_b['amountLocal']) #Check whether state is cleaned up: for d in data: self.assertEqual(d['transactions'], []) self.assertEqual(d['timeoutMessages'], []) self.assertEqual(d['payeeLinks'], {}) self.assertEqual(d['payerLink'], None) for lnk in d['links'].values(): self.assertEqual(len(lnk['channels']), 1) chn = lnk['channels'][0] self.assertEqual(chn['transactionsIncomingReserved'], {}) self.assertEqual(chn['transactionsOutgoingReserved'], {}) self.assertEqual(chn['transactionsIncomingLocked'], {}) self.assertEqual(chn['transactionsOutgoingLocked'], {})
def test_success(self): 'Test successfully performing a transaction between neighboring nodes' log.log('\n\n\n\nSCENARIO TEST: twonodes.py test_succes\n') if verbose: print 'Node 1:' pprint.pprint(self.node1.list()) print 'Node 2:' pprint.pprint(self.node2.list()) URL = self.node2.request(123, 'receipt') if verbose: print 'Payment URL:', URL amount, receipt = self.node1.pay(URL) if verbose: print 'Amount: ', amount print 'Receipt: ', receipt self.assertEqual(amount, 123) self.assertEqual(receipt, 'receipt') paymentState = self.node1.confirmPayment(True) if verbose: print 'Payment is ', paymentState self.assertEqual(paymentState, 'committed') #Allow paylink to disconnect time.sleep(0.5) state1 = self.node1.list() state2 = self.node2.list() if verbose: print 'Node 1:' pprint.pprint(state1) print 'Node 2:' pprint.pprint(state2) del state1['connections']['node1']['connectMessage']['dice'] del state2['connections']['node2']['connectMessage']['dice'] self.assertEqual( state1, { '_class': 'NodeState', 'connections': { 'node1': { '_class': 'PersistentConnection', 'closing': False, 'connectMessage': { '_class': 'ConnectLink', 'ID': 'node2', 'callbackHost': 'localhost', 'callbackID': 'node1', 'callbackPort': 4322 }, 'host': 'localhost', 'lastIndex': 2, 'messages': [], 'notYetTransmitted': 0, 'port': 4323 } }, 'links': { 'node1': { '_class': 'Link', 'channels': [{ '_class': 'PlainChannel', 'amountLocal': 877, 'amountRemote': 123, 'state': 'open', 'transactionsIncomingLocked': {}, 'transactionsIncomingReserved': {}, 'transactionsOutgoingLocked': {}, 'transactionsOutgoingReserved': {} }], 'localID': 'node1', 'remoteID': 'node2' } }, 'meetingPoints': {}, 'payeeLinks': {}, 'payerLink': None, 'transactions': [], 'timeoutMessages': [] }) self.assertEqual( state2, { '_class': 'NodeState', 'connections': { 'node2': { '_class': 'PersistentConnection', 'closing': False, 'connectMessage': { '_class': 'ConnectLink', 'ID': 'node1', 'callbackHost': 'localhost', 'callbackID': 'node2', 'callbackPort': 4323, }, 'host': 'localhost', 'lastIndex': 1, 'messages': [], 'notYetTransmitted': 0, 'port': 4322 } }, 'links': { 'node2': { '_class': 'Link', 'channels': [{ '_class': 'PlainChannel', 'amountLocal': 123, 'amountRemote': 877, 'state': 'open', 'transactionsIncomingLocked': {}, 'transactionsIncomingReserved': {}, 'transactionsOutgoingLocked': {}, 'transactionsOutgoingReserved': {} }], 'localID': 'node2', 'remoteID': 'node1' } }, 'meetingPoints': { 'MeetingPoint2': { '_class': 'MeetingPoint', 'ID': 'MeetingPoint2', 'unmatchedRoutes': {} } }, 'payeeLinks': {}, 'payerLink': None, 'transactions': [], 'timeoutMessages': [] })