Skip to content

Commit

Permalink
fix synchronization for background handling. also add logging for bac…
Browse files Browse the repository at this point in the history
…kground. Still work in progress.
  • Loading branch information
jussimalinen committed Oct 30, 2014
1 parent b7f88d7 commit b88be92
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 99 deletions.
8 changes: 8 additions & 0 deletions atest/asynchronous_messages/async_resources.txt
Expand Up @@ -2,6 +2,10 @@
Resource ../Protocols.txt
Library Rammbock
Library my_handler.py
Library OperatingSystem

*** Variables ***
${SIGNAL FILE}= ${TEMPDIR}${/}.rammbock_background_signal

*** Keywords ***
Define Templates
Expand Down Expand Up @@ -31,6 +35,10 @@ Handler should have been called with '${count}' another messages
Length should be ${rcvd_msgs} ${count}
${msg}= Set variable ${rcvd msgs[0]}
Should be equal ${msg._header.messageType.hex} 0x00ab
Handler should have been called with '${count}' times with logging
Log handler messages
Reset handler messages
Handler should have been called with '${count}' times
Handler should have been called with '${count}' times
${rcvd msgs} Get rcvd msg
Length should be ${rcvd_msgs} ${count}
Expand Down
13 changes: 8 additions & 5 deletions atest/asynchronous_messages/asynchronous_msg.txt
Expand Up @@ -53,9 +53,9 @@ Asynchronous messages on background
[Setup] Setup protocol, one client, background server, and define templates Loop on background
Load Template sample
Reset received messages
Set client handler my_handler.respond_to_sample header_filter=messageType interval=0.1
Set client handler my_handler.respond_to_sample header_filter=messageType interval=0.01
Repeat keyword 100 Send receive another
Wait until keyword succeeds 20s 1s Handler should have been called with '101' sample messages
Wait until keyword succeeds 20s 0.5s Handler should have been called with '101' sample messages
[Teardown] Get background results and reset
Register an auto reply to work on background
Load Template sample
Expand All @@ -77,15 +77,18 @@ Send receive another
Get background results and reset
${res}= Terminate Process
Log STDOUT:\n${res.stdout}\nSTDERR:\n${res.stderr}
log handler messages
Teardown rammbock and increment port numbers
Setup protocol, one client, background server, and define templates
[Arguments] ${background operation}
Define Example protocol
Define templates
Remove File ${SIGNAL FILE}
Start background process ${background operation}
Start TCP client 127.0.0.1 45555 name=client protocol=Example
Sleep 3 # Unfortunately if connect fails, the same socket can not be reused. So polling becomes difficult
Connect 127.0.0.1 44455
Wait Until Created ${SIGNAL FILE} timeout=10 seconds
sleep 0.1s # Just to make sure we dont get inbetween keywordcalls
Connect 127.0.0.1 ${SERVER PORT}
Setup protocol, server, two clients, and define templates
Define protocol, start tcp server and two clients protocol=Example
Define templates
Expand Down Expand Up @@ -115,6 +118,6 @@ Sample message should be in cache
Start background process
[Arguments] ${name}
${process}= Start process pybot --test ${name} --loglevel DEBUG
... --variable BACKGROUND:True --pythonpath ${SOURCEDIR}
... --variable BACKGROUND:True --variable PORT:${SERVER PORT} --pythonpath ${SOURCEDIR}
... --outputdir ${TEMPDIR} ${BACKGROUND FILE} shell=True
[Return] ${process}
11 changes: 7 additions & 4 deletions atest/asynchronous_messages/background_server.txt
@@ -1,8 +1,10 @@
*** Settings ***
Suite teardown Reset Rammbock
Suite teardown Run keywords log handler messages Reset Rammbock
Resource async_resources.txt
Force tags background



*** Variables ***
${BACKGROUND}= ${False}
${PORT}= 44455
Expand All @@ -20,10 +22,10 @@ Serve on loop
Setup connection
Send sample
Load Template another
Set server handler my_handler.server_respond_to_another_max_100 header_filter=messageType interval=0.1
Set server handler my_handler.server_respond_to_another_max_100 header_filter=messageType interval=0.01
Load template sample response
Set server handler my_handler.server_respond_to_sample_response_max_100 header_filter=messageType interval=0.1
Wait until keyword succeeds 50s 1s Handler should have been called with '200' times
Set server handler my_handler.server_respond_to_sample_response_max_100 header_filter=messageType interval=0.01
Wait until keyword succeeds 30s 0.5s Handler should have been called with '200' times with logging

Serve
Setup connection
Expand All @@ -40,6 +42,7 @@ Setup connection
Define example protocol
Define Templates
Start TCP server 127.0.0.1 ${PORT} name=ExampleServer protocol=Example
Touch ${SIGNAL FILE}
Accept connection

Send [arguments] ${message}
Expand Down
6 changes: 6 additions & 0 deletions atest/asynchronous_messages/my_handler.py
@@ -1,3 +1,5 @@
from Rammbock import logger


RECEIVED_MESSAGES = []
SERVER_SENT = {'sample': 0,
Expand Down Expand Up @@ -33,6 +35,8 @@ def server_respond_to_another_max_100(rammbock, msg):
rammbock.server_sends_message()
finally:
rammbock.load_template("__backup_template")
else:
logger.warn("Reached 100 in another")


def server_respond_to_sample_response_max_100(rammbock, msg):
Expand All @@ -45,6 +49,8 @@ def server_respond_to_sample_response_max_100(rammbock, msg):
rammbock.server_sends_message()
finally:
rammbock.load_template("__backup_template")
else:
logger.warn("Reached 100 in sample")


def get_rcvd_msg():
Expand Down
2 changes: 1 addition & 1 deletion atest/misc.txt
Expand Up @@ -11,5 +11,5 @@ Execute unit tests
Should Be Equal As Integers ${rc} 0 ${output}

Execute pep8 without line length check
${rc} ${output} Run And Return Rc And Output pep8 --ignore=E501 src/ atest/ utest/
${rc} ${output} Run And Return Rc And Output pep8 --exclude decorator.py --ignore=E501 src/ atest/ utest/
Should Be Equal As Integers ${rc} 0 ${output}
2 changes: 1 addition & 1 deletion execute_static_code_analysis.sh
Expand Up @@ -2,5 +2,5 @@
base=`dirname $0`
export PATH=$PATH:/usr/local/bin
pylint --rcfile=.pylintrc src/ > pylint.txt
pep8 src/ > pep8.txt
pep8 --exclude decorator.py src/ > pep8.txt
true
34 changes: 21 additions & 13 deletions src/Rammbock/core.py
Expand Up @@ -13,25 +13,28 @@
# limitations under the License.

from __future__ import with_statement
from contextlib import contextmanager
from Rammbock import logger
from Rammbock.templates.containers import BagTemplate, CaseTemplate
from message import _StructuredElement
from networking import (TCPServer, TCPClient, UDPServer, UDPClient, SCTPServer,
SCTPClient, _NamedCache)
from message_sequence import MessageSequence
from templates import (Protocol, UInt, Int, PDU, MessageTemplate, Char, Binary,
TBCD, StructTemplate, ListTemplate, UnionTemplate,
BinaryContainerTemplate, ConditionalTemplate,
TBCDContainerTemplate)
from binary_tools import to_0xhex, to_bin
import copy
from contextlib import contextmanager
from . import logger
from .synchronization import SynchronizedType
from .templates.containers import BagTemplate, CaseTemplate
from .message import _StructuredElement
from .networking import (TCPServer, TCPClient, UDPServer, UDPClient, SCTPServer,
SCTPClient, _NamedCache)
from .message_sequence import MessageSequence
from .templates import (Protocol, UInt, Int, PDU, MessageTemplate, Char, Binary,
TBCD, StructTemplate, ListTemplate, UnionTemplate,
BinaryContainerTemplate, ConditionalTemplate,
TBCDContainerTemplate)
from .binary_tools import to_0xhex, to_bin


class RammbockCore(object):

ROBOT_LIBRARY_SCOPE = 'GLOBAL'

__metaclass__ = SynchronizedType

def __init__(self):
self._init_caches()

Expand All @@ -45,12 +48,17 @@ def _init_caches(self):
self._field_values = {}
self._message_sequence = MessageSequence()
self._message_templates = {}
self.reset_handler_messages()

@property
def _current_container(self):
return self._message_stack[-1]

# TODO: Set Server Handler
def reset_handler_messages(self):
logger.reset_background_messages()

def log_handler_messages(self):
logger.log_background_messages()

def set_client_handler(self, handler_func, name=None, header_filter=None, interval=0.5):
"""Sets an automatic handler for the type of message template currently loaded.
Expand Down

0 comments on commit b88be92

Please sign in to comment.