polish up websocket testee

This commit is contained in:
Tobias Oberstein 2016-02-08 19:59:58 +01:00
parent 08475e22ee
commit 39cba40689
6 changed files with 213 additions and 70 deletions

View File

@ -24,8 +24,7 @@
#
###############################################################################
__version__ = u"0.13.0"
__version__ = u'0.13.0'
"""
AutobahnPython library version.
"""

View File

@ -27,6 +27,11 @@
from __future__ import absolute_import
import sys
import platform
import autobahn
# WebSocket protocol support
from autobahn.asyncio.websocket import \
WebSocketServerProtocol, \
@ -45,3 +50,8 @@ __all__ = (
'WebSocketClientFactory',
'ApplicationSession',
)
__ident__ = u'Autobahn/{}-asyncio-{}/{}'.format(autobahn.__version__, platform.python_implementation(), '.'.join([str(x) for x in list(sys.version_info[:3])]))
"""
AutobahnPython library implementation (eg. "Autobahn/0.13.0-asyncio-CPython/3.5.1")
"""

View File

@ -27,6 +27,13 @@
from __future__ import absolute_import
import sys
import platform
import twisted
import autobahn
# Twisted specific utilities (these should really be in Twisted, but
# they aren't, and we use these in example code, so it must be part of
# the public API)
@ -75,3 +82,8 @@ __all__ = (
# WAMP support
'ApplicationSession',
)
__ident__ = u'Autobahn/{}-Twisted/{}-{}/{}'.format(autobahn.__version__, twisted.__version__, platform.python_implementation(), '.'.join([str(x) for x in list(sys.version_info[:3])]))
"""
AutobahnPython library implementation (eg. "Autobahn/0.13.0-Twisted/15.5.0-CPython/3.5.1")
"""

View File

@ -24,21 +24,40 @@
#
###############################################################################
import txaio
txaio.use_asyncio()
try:
import asyncio
except ImportError:
# Trollius >= 0.3 was renamed
import trollius as asyncio
import autobahn
from autobahn.asyncio.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
from autobahn.websocket.compress import *
from autobahn.websocket.compress import PerMessageDeflateOffer, \
PerMessageDeflateOfferAccept
# FIXME: streaming mode API is currently incompatible with permessage-deflate!
USE_STREAMING_TESTEE = False
class TesteeServerProtocol(WebSocketServerProtocol):
"""
A message-based WebSocket echo server.
"""
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
class StreamingTesteeServerProtocol(WebSocketServerProtocol):
"""
A streaming WebSocket echo server.
"""
def onMessageBegin(self, isBinary):
WebSocketServerProtocol.onMessageBegin(self, isBinary)
@ -60,41 +79,37 @@ class StreamingTesteeServerProtocol(WebSocketServerProtocol):
class TesteeServerFactory(WebSocketServerFactory):
# protocol = TesteeServerProtocol
protocol = StreamingTesteeServerProtocol
if USE_STREAMING_TESTEE:
protocol = StreamingTesteeServerProtocol
else:
protocol = TesteeServerProtocol
def __init__(self, url):
WebSocketServerFactory.__init__(self, url, server="AutobahnPython-asyncio/{}".format(autobahn.__version__))
def __init__(self, url, ident=None):
if ident is not None:
server = ident
else:
server = "AutobahnPython-Asyncio/%s" % autobahn.version
WebSocketServerFactory.__init__(self, url, server=server)
self.setProtocolOptions(failByDrop=False) # spec conformance
self.setProtocolOptions(failByDrop=True) # needed for streaming mode
# self.setProtocolOptions(utf8validateIncoming = False)
# enable permessage-deflate
##
def accept(offers):
for offer in offers:
if isinstance(offer, PerMessageDeflateOffer):
return PerMessageDeflateOfferAccept(offer)
if USE_STREAMING_TESTEE:
self.setProtocolOptions(failByDrop=True) # needed for streaming mode
else:
# enable permessage-deflate WebSocket protocol extension
def accept(offers):
for offer in offers:
if isinstance(offer, PerMessageDeflateOffer):
return PerMessageDeflateOfferAccept(offer)
self.setProtocolOptions(perMessageCompressionAccept=accept)
self.setProtocolOptions(perMessageCompressionAccept=accept)
if __name__ == '__main__':
try:
import asyncio
except ImportError:
# Trollius >= 0.3 was renamed
import trollius as asyncio
txaio.start_logging(level='info')
factory = TesteeServerFactory(u"ws://127.0.0.1:9002")
factory = TesteeServerFactory(u"ws://127.0.0.1:9001")
loop = asyncio.get_event_loop()
coro = loop.create_server(factory, port=9002)
coro = loop.create_server(factory, port=9001)
server = loop.run_until_complete(coro)
try:

View File

@ -0,0 +1,111 @@
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Tavendo GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################
import txaio
txaio.use_twisted()
from twisted.internet import reactor
import autobahn
from autobahn.twisted.websocket import connectWS, WebSocketClientFactory, \
WebSocketClientProtocol
from autobahn.websocket.compress import PerMessageDeflateOffer, \
PerMessageDeflateResponse, PerMessageDeflateResponseAccept
class TesteeClientProtocol(WebSocketClientProtocol):
def onOpen(self):
if self.factory.endCaseId is None:
self.log.info("Getting case count ..")
elif self.factory.currentCaseId <= self.factory.endCaseId:
self.log.info("Running test case {case_id}/{last_case_id} as user agent {agent} on peer {peer}",
case_id=self.factory.currentCaseId,
last_case_id=self.factory.endCaseId,
agent=self.factory.agent,
peer=self.peer)
def onMessage(self, msg, binary):
if self.factory.endCaseId is None:
self.factory.endCaseId = int(msg)
self.log.info("Ok, will run {case_count} cases", case_count=self.factory.endCaseId)
else:
self.sendMessage(msg, binary)
class TesteeClientFactory(WebSocketClientFactory):
protocol = TesteeClientProtocol
def __init__(self, url):
self.agent = "AutobahnPython-Twisted/{}".format(autobahn.__version__)
WebSocketClientFactory.__init__(self, url, useragent=self.agent)
self.setProtocolOptions(failByDrop=False) # spec conformance
# enable permessage-deflate WebSocket protocol extension
offers = [PerMessageDeflateOffer()]
self.setProtocolOptions(perMessageCompressionOffers=offers)
def accept(response):
if isinstance(response, PerMessageDeflateResponse):
return PerMessageDeflateResponseAccept(response)
self.setProtocolOptions(perMessageCompressionAccept=accept)
# setup client testee stuff
self.endCaseId = None
self.currentCaseId = 0
self.updateReports = True
self.resource = "/getCaseCount"
def clientConnectionLost(self, connector, reason):
self.currentCaseId += 1
if self.currentCaseId <= self.endCaseId:
self.resource = "/runCase?case={}&agent={}".format(self.currentCaseId, self.agent)
connector.connect()
elif self.updateReports:
self.resource = "/updateReports?agent={}".format(self.agent)
self.updateReports = False
connector.connect()
else:
reactor.stop()
def clientConnectionFailed(self, connector, reason):
self.log.info("Connection to {url} failed: {error_message}", url=self.url, error_message=reason.getErrorMessage())
reactor.stop()
if __name__ == '__main__':
txaio.start_logging(level='info')
factory = TesteeClientFactory(u"ws://127.0.0.1:9001")
connectWS(factory)
reactor.run()

View File

@ -24,6 +24,14 @@
#
###############################################################################
import sys
import platform
import txaio
txaio.use_twisted()
from twisted.internet import reactor
import autobahn
from autobahn.twisted.websocket import WebSocketServerProtocol, \
@ -32,68 +40,63 @@ from autobahn.twisted.websocket import WebSocketServerProtocol, \
from autobahn.websocket.compress import PerMessageDeflateOffer, \
PerMessageDeflateOfferAccept
# FIXME: streaming mode API is currently incompatible with permessage-deflate!
USE_STREAMING_TESTEE = False
if USE_STREAMING_TESTEE:
class TesteeServerProtocol(WebSocketServerProtocol):
"""
A message-based WebSocket echo server.
"""
class StreamingTesteeServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
"""
A streaming WebSocket echo server.
"""
def onMessageBegin(self, isBinary):
WebSocketServerProtocol.onMessageBegin(self, isBinary)
self.beginMessage(isBinary)
class StreamingTesteeServerProtocol(WebSocketServerProtocol):
"""
A streaming WebSocket echo server.
"""
def onMessageFrameBegin(self, length):
WebSocketServerProtocol.onMessageFrameBegin(self, length)
self.beginMessageFrame(length)
def onMessageBegin(self, isBinary):
WebSocketServerProtocol.onMessageBegin(self, isBinary)
self.beginMessage(isBinary)
def onMessageFrameData(self, payload):
self.sendMessageFrameData(payload)
def onMessageFrameBegin(self, length):
WebSocketServerProtocol.onMessageFrameBegin(self, length)
self.beginMessageFrame(length)
def onMessageFrameEnd(self):
pass
def onMessageFrameData(self, payload):
self.sendMessageFrameData(payload)
def onMessageEnd(self):
self.endMessage()
def onMessageFrameEnd(self):
pass
else:
class TesteeServerProtocol(WebSocketServerProtocol):
"""
A message-based WebSocket echo server.
"""
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
def onMessageEnd(self):
self.endMessage()
class TesteeServerFactory(WebSocketServerFactory):
log = txaio.make_logger()
if USE_STREAMING_TESTEE:
protocol = StreamingTesteeServerProtocol
else:
protocol = TesteeServerProtocol
def __init__(self, url, ident=None):
if ident is not None:
server = ident
else:
server = "AutobahnPython-Twisted/%s" % autobahn.__version__
WebSocketServerFactory.__init__(self, url, server=server)
def __init__(self, url):
testee_ident = autobahn.twisted.__ident__
self.log.info("Testee identification: {testee_ident}", testee_ident=testee_ident)
WebSocketServerFactory.__init__(self, url, server=testee_ident)
self.setProtocolOptions(failByDrop=False) # spec conformance
# self.setProtocolOptions(utf8validateIncoming = False)
if USE_STREAMING_TESTEE:
self.setProtocolOptions(failByDrop=True) # needed for streaming mode
else:
# enable permessage-deflate (which is not working with streaming currently)
#
# enable permessage-deflate WebSocket protocol extension
def accept(offers):
for offer in offers:
if isinstance(offer, PerMessageDeflateOffer):
@ -101,17 +104,10 @@ class TesteeServerFactory(WebSocketServerFactory):
self.setProtocolOptions(perMessageCompressionAccept=accept)
# self.setProtocolOptions(utf8validateIncoming = False)
if __name__ == '__main__':
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
txaio.start_logging(level='info')
factory = TesteeServerFactory(u"ws://127.0.0.1:9001")