class TestH2HServer(unittest.TestCase): def setUp(self): self.q_h2hcore = Queue.Queue() self.m_h2hcore = XMPPMessaging(config.H2HCORE, config.H2HCORE_PASS, config.MSG_SERVER, 'SERVER1', self.q_h2hcore) self.last_result = None self.h2hserver = '{0}@{1}'.format(config.H2HSERVER, config.MSG_SERVER) #---------------------- def runTest(self): test_function = [x for x in dir(self) if x[:6] == 'srtest'] map(lambda x: getattr(self, x)(), sorted(test_function)) #---------------------- def req(self, trxid, product, agentid, msisdn, signature, order=1): url = 'https://127.0.0.1:9911/' resp = urllib.urlopen(url, urllib.urlencode({ 'trxid': trxid, 'product': product, 'agentid': agentid, 'msisdn': msisdn, 'signature': signature, 'order': order, })) result = resp.read() self.last_result = json.loads(result) #---------------------- def srtest010(self): t = threading.Thread(target=self.req, args=(1, 'A10', '00005', '08161940700', 'SIGN')) t.start() x = self.q_h2hcore.get(1, 5) print 'x=', x x = json.loads(x.split(':', 1)[1]) self.assertEqual(x['product'], 'A10') self.assertEqual(x['msisdn'], '08161940700') self.assertEqual(x['order'], '1') self.m_h2hcore.send(self.h2hserver, json.dumps({ const.H2_REJECT: 0, const.H2_DEST: '08161940700', const.H2_PROD: 'A10', const.H2_ORDER: 1, const.H2_AGENT: '00005', const.H2_PAYLOAD: {'apalah': 'Yihaaa!!!'}, })) t.join() # print 'result=', self.last_result self.assertEqual(self.last_result, {'apalah': 'Yihaaa!!!'}) #---------------------- def tearDown(self): self.m_h2hcore.stop()
def setUp(self): self.q_h2hcore = Queue.Queue() self.m_h2hcore = XMPPMessaging(config.H2HCORE, config.H2HCORE_PASS, config.MSG_SERVER, 'SERVER1', self.q_h2hcore) self.last_result = None self.h2hserver = '{0}@{1}'.format(config.H2HSERVER, config.MSG_SERVER)
def _initXMPP2(self, username, password, resource="UNITTEST"): self.msg_queue2 = Queue() self.xmppconn2 = XMPPMessaging(username, password, config.MSG_SERVER, resource, self.msg_queue2, prtimeout=1)
class TestCoreComponent(SRTestCase): """Test case core components A test case that includes testing the core components should inherit this class""" def setUp(self): super(TestCoreComponent, self).setUp() self.xmppconn = None self.xmppconn2 = None self.process = [] self.pipes = [] def _initXMPP(self, username, password, resource="UNITTEST"): self.msg_queue = Queue() self.xmppconn = XMPPMessaging(username, password, config.MSG_SERVER, resource, self.msg_queue, prtimeout=1) def _initXMPP2(self, username, password, resource="UNITTEST"): self.msg_queue2 = Queue() self.xmppconn2 = XMPPMessaging(username, password, config.MSG_SERVER, resource, self.msg_queue2, prtimeout=1) def _sendCommand(self, to, command, param): self.xmppconn.send(to, "{0}={1}".format(command, param)) def _sendCommand2(self, to, command, param): self.xmppconn2.send(to, "{0}={1}".format(command, param)) def _waitForMessage(self, timeout=3): try: msg = self.msg_queue.get(True, timeout) except: return None return msg def _waitForMessage2(self, timeout=3): try: msg = self.msg_queue2.get(True, timeout) except: return None return msg def _waitForRow(self, sql, timeout=3): timeout = datetime.now() + timedelta(seconds=timeout) cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) while datetime.now() < timeout: cursor.execute(sql) tmp = cursor.fetchall() if not tmp: time.sleep(0.1) continue break cursor.close() if len(tmp) == 1: return tmp[0] return tmp def _createProcess(self, process): here, there = multiprocessing.Pipe() new_proc = multiprocessing.Process(target=process, args=(there,)) self.process.append(new_proc) self.pipes.append((here, there)) new_proc.start() def tearDown(self): if self.xmppconn: self.xmppconn.stop() if self.xmppconn2: self.xmppconn2.stop() for proc in self.process: proc.terminate() if self.xmppconn: self.xmppconn.join() if self.xmppconn2: self.xmppconn2.join() for proc in self.process: proc.join()