github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_dispatcher/tests.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  from concurrent.futures import ThreadPoolExecutor
    17  import unittest
    18  
    19  from sawtooth_validator.networking import dispatch
    20  from sawtooth_validator.protobuf import validator_pb2
    21  
    22  from test_dispatcher.mock import MockSendMessage
    23  from test_dispatcher.mock import MockHandler1
    24  from test_dispatcher.mock import MockHandler2
    25  
    26  
    27  class TestDispatcherIdentityMessageMatch(unittest.TestCase):
    28      def setUp(self):
    29          self._connection = "TestConnection"
    30          self._dispatcher = dispatch.Dispatcher()
    31          thread_pool = ThreadPoolExecutor()
    32  
    33          self._dispatcher.add_handler(
    34              validator_pb2.Message.DEFAULT,
    35              MockHandler1(),
    36              thread_pool)
    37          self._dispatcher.add_handler(
    38              validator_pb2.Message.DEFAULT,
    39              MockHandler2(),
    40              thread_pool)
    41  
    42          self._message_ids = [str(i) for i in range(10)]
    43          self._identities = [str(i) for i in range(10)]
    44          self._connections = {chr(int(x) + 65): x for x in self._identities}
    45  
    46          self.mock_send_message = MockSendMessage(self._connections)
    47          self._dispatcher.add_send_message(self._connection,
    48                                            self.mock_send_message.send_message)
    49  
    50          self._messages = [
    51              validator_pb2.Message(
    52                  content=validator_pb2.Message(
    53                      correlation_id=m_id).SerializeToString(),
    54                  message_type=validator_pb2.Message.DEFAULT)
    55              for m_id in self._message_ids
    56          ]
    57  
    58      def test_correct_identity(self):
    59          """Tests that if a message is dispatched with a particular identity --
    60          having dispatcher.dispatch(connection, message, connection_id)
    61          called -- that identity will be sent back to the zmq interface
    62          with the message.
    63  
    64          Each message gets an id 0-9 along with an identity 0-9. These will
    65          be returned to send_message together no matter the ordering of the
    66          returns of the handlers.
    67  
    68          """
    69          self._dispatcher.start()
    70          for connection_id, message in zip(self._connections, self._messages):
    71              self._dispatcher.dispatch(
    72                  self._connection, message, connection_id)
    73          self._dispatcher.block_until_complete()
    74          self.assertEqual(sorted(self.mock_send_message.message_ids),
    75                           sorted(self.mock_send_message.identities))
    76  
    77      def tearDown(self):
    78          self._dispatcher.stop()