github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_dispatcher/mock.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 threading import RLock
    17  import time
    18  
    19  from sawtooth_validator.networking import dispatch
    20  from sawtooth_validator.protobuf import validator_pb2
    21  
    22  
    23  class MockHandler1(dispatch.Handler):
    24      def __init__(self):
    25          self._time_to_sleep = 2.0
    26  
    27      def handle(self, connection_id, message_content):
    28          if self._time_to_sleep > 0:
    29              time.sleep(self._time_to_sleep)
    30              self._time_to_sleep -= 0.1
    31          return dispatch.HandlerResult(
    32              dispatch.HandlerStatus.PASS)
    33  
    34  
    35  class MockHandler2(dispatch.Handler):
    36      def handle(self, connection_id, message_content):
    37          request = validator_pb2.Message()
    38          request.ParseFromString(message_content)
    39          return dispatch.HandlerResult(
    40              dispatch.HandlerStatus.RETURN,
    41              message_out=validator_pb2.Message(
    42                  correlation_id=request.correlation_id,
    43              ),
    44              message_type=validator_pb2.Message.DEFAULT)
    45  
    46  
    47  class MockSendMessage(object):
    48      def __init__(self, connections):
    49          self.message_ids = []
    50          self.identities = []
    51          self._lock = RLock()
    52          self.connections = connections
    53  
    54      def send_message(self, connection_id, msg):
    55          with self._lock:
    56              message = validator_pb2.Message()
    57              message.ParseFromString(msg.content)
    58              self.identities.append(self.connections[connection_id])
    59              self.message_ids.append(message.correlation_id)