def test02_pingExpectingPrefix(self): d = self.client_factory[0].established_deferred silence_all_messages() d.addCallback(self.setPrefix) d.addCallback(self.pingExpectingPrefix) d.addCallback(self.quit) return d
def test01_invalidProtocol(self): self.server_factory = FakeFactory(self) silence_all_messages() self.server_factory.buildProtocol('addr').dataReceived( "invalid protocol\n") if verbose < 0: # can only grep output when output is redirected self.assertEqual( search_output('client with protocol UNKNOWN rejected'), True)
def dummyClientError(self, client): silence_all_messages() clear_all_messages() client.error("stupid dummy error test since client never calls") self.assertEquals( get_messages(), ["ERROR stupid dummy error test since client never calls"]) return (client, )
def test11_mainTests_raiseForceRelease(self): import Queue import time class MockException(Exception): pass def raiseForceRelease(name, timeout): raise MockException() def succeeded(result): self.failIf(True) def failed(result): self.failUnless(issinstance(result, MockException)) # FIXME: this callback never happens; it should, however. I # am not completely sure why; I assume it's because the # reactor.callFromThread() errback call in the main() doesn't # get executed before the reactor dies. OTOH, I don't fully # understand the thread/reactor interaction issues . If # someone can figure out and make sure this callback happens, # I'd appreciate it. d = defer.Deferred() d.addErrback(failed) d.addCallback(succeeded) class MockQueue: def __init__(qSelf): qSelf.count = 1 def qsize(qSelf): return qSelf.count def get(qSelf, timeout = 1): if qSelf.count > 0: qSelf.count = 0 return ("Mocky", raiseForceRelease, 10, d) else: raise Queue.Empty def empty(qSelf): return qSelf.count <= 0 def put(qSelf, val): pass class MockLock: def release(lSelf): raise MockException("MOCKY NO LOCK RELEASE") silence_all_messages() clear_all_messages() anotherLock = pokerlock.PokerLock(self.parameters) anotherLock.q = MockQueue() anotherLock.lock = MockLock() anotherLock.verbose = 6 anotherLock.start() time.sleep(2) self.assertEquals(anotherLock.running, True) for string in [ 'exception in function Traceback', 'release because exception', 'raise MockException("MOCKY NO LOCK RELEASE")']: self.failUnless(search_output(string), "missing '%s' in output" % string)
def testConnectionLost(self): """Testing ConnectionLost""" silence_all_messages() clear_all_messages() self.u.established = 1 self.u.connectionLost("testing") self.assertEquals(search_output("connectionLost: reason = testing"), True) assert self.u.established == 0
def testConnectionLost(self): """Testing ConnectionLost""" silence_all_messages() clear_all_messages() self.u.established = 1 self.u.connectionLost("testing") self.assertEquals(search_output("connectionLost: reason = testing"), True) assert self.u.established == 0
def testConnectionLostWithProtocolOk(self): """Testing ConnectionLostWithProtocolOk""" silence_all_messages() clear_all_messages() self.u.established = 1 self.u._protocol_ok = True self.u.connectionLost("another") self.assertEquals(search_output("connectionLost: reason = another"), True) assert self.u.established == 0
def testConnectionLostWithProtocolOk(self): """Testing ConnectionLostWithProtocolOk""" silence_all_messages() clear_all_messages() self.u.established = 1 self.u._protocol_ok = True self.u.connectionLost("another") self.assertEquals(search_output("connectionLost: reason = another"), True) assert self.u.established == 0
def setUp(self, verbose = 6): pokerlock.PokerLock.acquire_sleep = 1 # verbose = int(os.environ.get('VERBOSE_T', '-1')) silence_all_messages() if verbose < 0: pokerlock.PokerLock.message = lambda self, string: True self.parameters = {'host': 'localhost', 'user': '******', 'password': ''} pokerlock.PokerLock.queue_timeout = 30 self.locker = pokerlock.PokerLock(self.parameters) if verbose >= 0: self.locker.verbose = verbose; self.locker.start()
def quit(self, args): client = args[0] client.sendPacket(PacketQuit()) client.transport.loseConnection() server = self.server_factory.instance def serverPingTimeout(val): self.assertEqual(search_output("ping: timeout Mr.Fakey/-1"), True) client.connection_lost_deferred.addCallback(serverPingTimeout) silence_all_messages() return client.connection_lost_deferred
def test10_mainTests_notRunningForCallback(self): import Queue import time global myLock def setNotRunning(name, timeout): global myLock myLock.running = False d = defer.Deferred() def succeeded(result): self.failIf(True) def failed(result): self.failIf(True) d.addErrback(failed) d.addCallback(succeeded) class MockQueue: def __init__(qSelf): qSelf.count = 1 def qsize(qSelf): return qSelf.count def get(qSelf, timeout = 1): if qSelf.count > 0: qSelf.count = 0 return ("Mocky", setNotRunning, 10, d) else: raise Queue.Empty def empty(qSelf): return qSelf.count <= 0 def put(qSelf, val): pass class MockLock: def __init__(lSelf): lSelf.calledReleaseCount = 0 def release(lSelf): lSelf.calledReleaseCount += 1 silence_all_messages() clear_all_messages() anotherLock = pokerlock.PokerLock(self.parameters) anotherLock.q = MockQueue() anotherLock.lock = MockLock() anotherLock.verbose = 6 myLock = anotherLock anotherLock.start() time.sleep(2) self.failUnless(search_output('release because not running'), "missing 'release because not running' in output") self.assertEquals(anotherLock.running, False) self.assertEquals(anotherLock.lock.calledReleaseCount, 1)
def Run(): silence_all_messages() loader = runner.TestLoader() # loader.methodPrefix = "test09" suite = loader.suiteFactory() suite.addTest(loader.loadClass(ClientServer)) suite.addTest(loader.loadClass(ClientServerBadClientProtocol)) suite.addTest(loader.loadClass(ClientServerQueuedServerPackets)) suite.addTest(loader.loadClass(ClientServerDeferredServerPackets)) suite.addTest(loader.loadClass(DummyServerTests)) suite.addTest(loader.loadClass(DummyClientTests)) return runner.TrialRunner( reporter.VerboseTextReporter, # tracebackFormat='verbose', tracebackFormat='default', ).run(suite)
def test01_badClientProtocol(self): silence_all_messages() d = self.client_factory[0].established_deferred def findError(myFailure): from pokernetwork.protocol import PROTOCOL_MAJOR, PROTOCOL_MINOR msg = myFailure.getErrorMessage() self.failIf( msg.find("'0.00\\n', '%s.%s')" % (PROTOCOL_MAJOR, PROTOCOL_MINOR)) < 0) self.failIf( msg.find( "(<pokernetwork.client.UGAMEClientProtocol instance at") > 0) d.addErrback(findError) return d
def test07_bufferizedClientPackets(self): silence_all_messages() d = self.client_factory[0].established_deferred def bufferPackets(client): def checkOutput(client): msgs = get_messages() self.assertEquals(msgs[0], 'sendPacket(0) type = ACK(4) ') return (client, ) client.bufferized_packets = [PacketAck()] clear_all_messages() ccd = client.connection_lost_deferred ccd.addCallback(checkOutput) return ccd d.addCallback(bufferPackets) return d
def clientConnectionLost(self, client): class ReasonMockUp: def __str__(self): return "you mock me" def check(self, foo): return False silence_all_messages() clear_all_messages() client.connectionLost(ReasonMockUp()) self.assertEquals(get_messages(), [ 'connectionLost: reason = you mock me', 'UGAMEClient.connectionLost you mock me' ]) self.assertEquals(client._ping_timer, None) self.assertEquals(self.client_factory[0].protocol_instance, None) return True
# Bradley M. Kuhn <*****@*****.**> # import sys, os SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, SCRIPT_DIR) sys.path.insert(0, "..") import unittest import os.path import types from tests.testmessages import silence_all_messages, clear_all_messages, get_messages, search_output silence_all_messages() from twisted.python.runtime import seconds from pokernetwork import pokerauth from pokernetwork.user import User from pokernetwork import pokernetworkconfig from pokernetwork.pokerdatabase import PokerDatabase import libxml2 settings_xml = """<?xml version="1.0" encoding="ISO-8859-1"?> <server auto_create_account="no" verbose="6" ping="300000" autodeal="yes" simultaneous="4" chat="yes" > <database name="pokernetworktest" host="localhost" user="******" password="******" root_user="******" root_password="" schema="%(script_dir)s/../database/schema.sql" command="/usr/bin/mysql" /> <delays autodeal="18" round="12" position="60" showdown="30" finish="18" />
def setUp(self): self.u = protocol.UGAMEProtocol() self.u.transport = FakeTransport() self.u.factory = FakeFactory(int(os.environ.get('VERBOSE_T', 6))) silence_all_messages()
def setUp(self): self.u = protocol.UGAMEProtocol() self.u.transport = FakeTransport() self.u.factory = FakeFactory(int(os.environ.get('VERBOSE_T', 6))) silence_all_messages()
# Loic Dachary <*****@*****.**> # Johan Euphrosine <*****@*****.**> # Bradley M. Kuhn <*****@*****.**> # import sys, os SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, SCRIPT_DIR) sys.path.insert(0, "..") import unittest import os.path import types from tests.testmessages import silence_all_messages, clear_all_messages, get_messages, search_output silence_all_messages() from twisted.python.runtime import seconds from pokernetwork import pokerauth from pokernetwork.user import User from pokernetwork import pokernetworkconfig from pokernetwork.pokerdatabase import PokerDatabase import libxml2 settings_xml = """<?xml version="1.0" encoding="ISO-8859-1"?> <server auto_create_account="no" verbose="6" ping="300000" autodeal="yes" simultaneous="4" chat="yes" > <database name="pokernetworktest" host="localhost" user="******" password="******" root_user="******" root_password="" schema="%(script_dir)s/../database/schema.sql" command="/usr/bin/mysql" /> <delays autodeal="18" round="12" position="60" showdown="30" finish="18" />
# You should have received a copy of the GNU Affero General Public License # along with this program in a file in the toplevel directory called # "AGPLv3". If not, see <http://www.gnu.org/licenses/>. # # Authors: # J.Jeannin <*****@*****.**> # Bradley M. Kuhn <*****@*****.**> import sys, os sys.path.insert(0, "./..") sys.path.insert(0, "..") from tests.testmessages import silence_all_messages, search_output, clear_all_messages, get_messages verbose = int(os.environ.get('VERBOSE_T', '-1')) verbose = 5 if verbose < 0: silence_all_messages() from pokernetwork import protocol from pokernetwork import protocol_number from pokernetwork.version import Version from twisted.trial import unittest, runner, reporter from twisted.internet import reactor, defer from time import time protocol_version = Version(protocol_number) #----------------- class FakeFactory:
import simplejson from twisted.trial import unittest, runner, reporter from twisted.internet import defer, reactor from twisted.application import internet from twisted.python import failure from twisted.python.runtime import seconds import twisted.internet.base twisted.internet.base.DelayedCall.debug = True from twisted.web import client, http from tests import testmessages verbose = int(os.environ.get('VERBOSE_T', '-1')) if verbose < 0: testmessages.silence_all_messages() from tests import testclock from pokernetwork import pokermemcache from pokernetwork import pokersite from pokernetwork import pokernetworkconfig from pokernetwork import pokerservice from pokernetwork import proxyfilter from pokernetwork.pokerpackets import * settings_xml_server = """<?xml version="1.0" encoding="ISO-8859-1"?> <server verbose="6" ping="300000" autodeal="yes" simultaneous="4" chat="yes" > <delays autodeal="20" round="0" position="0" showdown="0" autodeal_max="1" finish="0" messages="60" /> <table name="Table1" variant="holdem" betting_structure="100-200-no-limit" seats="10" player_timeout="60" currency_serial="1" />
from types import * from twisted.python import failure from twisted.trial import unittest, runner, reporter import twisted.internet.base from twisted.internet import reactor from twisted.internet import defer twisted.internet.base.DelayedCall.debug = True from urlparse import urlparse from tests.testmessages import silence_all_messages, clear_all_messages, search_output, get_messages, restore_all_messages verbose = int(os.environ.get('VERBOSE_T', '-1')) if verbose < 0: silence_all_messages() from pokernetwork import currencyclient class CurrencyClientTestCase(unittest.TestCase): def destroyDb(self): if len("") > 0: os.system( "/usr/bin/mysql -u root --password='' -h 'localhost' -e 'DROP DATABASE IF EXISTS currencytest'" ) else: os.system( "/usr/bin/mysql -u root -h 'localhost' -e 'DROP DATABASE IF EXISTS currencytest'" )
import simplejson from twisted.trial import unittest, runner, reporter from twisted.internet import defer, reactor from twisted.application import internet from twisted.python import failure from twisted.python.runtime import seconds import twisted.internet.base twisted.internet.base.DelayedCall.debug = True from twisted.web import client, http from tests import testmessages verbose = int(os.environ.get('VERBOSE_T', '-1')) if verbose < 0: testmessages.silence_all_messages() from tests import testclock from pokernetwork import pokermemcache from pokernetwork import pokersite from pokernetwork import pokernetworkconfig from pokernetwork import pokerservice from pokernetwork import proxyfilter from pokernetwork.pokerpackets import * settings_xml_server = """<?xml version="1.0" encoding="ISO-8859-1"?> <server verbose="6" ping="300000" autodeal="yes" simultaneous="4" chat="yes" > <delays autodeal="20" round="0" position="0" showdown="0" autodeal_max="1" finish="0" messages="60" /> <table name="Table1" variant="holdem" betting_structure="100-200-no-limit" seats="10" player_timeout="60" currency_serial="1" />
def buildProtocol(self, addr): proto = FakeFactory.buildProtocol(self, addr) silence_all_messages() clear_all_messages() proto.bufferized_packets.append(PacketAck()) return proto
def test01_ping(self): d = self.client_factory[0].established_deferred silence_all_messages() d.addCallback(self.ping) d.addCallback(self.quit) return d