github.com/braveheart12/insolar-09-08-19@v0.8.7/network/servicenetwork/communicator_mock.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package servicenetwork
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/insolar/insolar/consensus/packets"
    24  	"github.com/insolar/insolar/consensus/phases"
    25  	"github.com/insolar/insolar/core"
    26  	"github.com/insolar/insolar/network"
    27  )
    28  
    29  type CommunicatorTestOpt int
    30  
    31  const (
    32  	PartialPositive1Phase = CommunicatorTestOpt(iota + 1)
    33  	PartialNegative1Phase
    34  	PartialPositive2Phase
    35  	PartialNegative2Phase
    36  	PartialPositive3Phase
    37  	PartialNegative3Phase
    38  	PartialPositive23Phase
    39  	PartialNegative23Phase
    40  )
    41  
    42  type CommunicatorMock struct {
    43  	communicator phases.Communicator
    44  	ignoreFrom   core.RecordRef
    45  	testOpt      CommunicatorTestOpt
    46  }
    47  
    48  func (cm *CommunicatorMock) ExchangePhase1(
    49  	ctx context.Context,
    50  	originClaim *packets.NodeAnnounceClaim,
    51  	participants []core.Node,
    52  	packet *packets.Phase1Packet,
    53  ) (map[core.RecordRef]*packets.Phase1Packet, error) {
    54  	pckts, err := cm.communicator.ExchangePhase1(ctx, originClaim, participants, packet)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	switch cm.testOpt {
    59  	case PartialNegative1Phase, PartialPositive1Phase:
    60  		delete(pckts, cm.ignoreFrom)
    61  	}
    62  	return pckts, nil
    63  }
    64  
    65  func (cm *CommunicatorMock) ExchangePhase2(ctx context.Context, list network.UnsyncList, participants []core.Node, packet *packets.Phase2Packet) (map[core.RecordRef]*packets.Phase2Packet, error) {
    66  	pckts, err := cm.communicator.ExchangePhase2(ctx, list, participants, packet)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	switch cm.testOpt {
    71  	case PartialPositive2Phase, PartialNegative2Phase, PartialPositive23Phase, PartialNegative23Phase:
    72  		delete(pckts, cm.ignoreFrom)
    73  	}
    74  	return pckts, nil
    75  }
    76  
    77  func (cm *CommunicatorMock) ExchangePhase21(ctx context.Context, list network.UnsyncList, packet *packets.Phase2Packet, additionalRequests []*phases.AdditionalRequest) ([]packets.ReferendumVote, error) {
    78  	return cm.communicator.ExchangePhase21(ctx, list, packet, additionalRequests)
    79  }
    80  
    81  func (cm *CommunicatorMock) ExchangePhase3(ctx context.Context, participants []core.Node, packet *packets.Phase3Packet) (map[core.RecordRef]*packets.Phase3Packet, error) {
    82  	pckts, err := cm.communicator.ExchangePhase3(ctx, participants, packet)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	switch cm.testOpt {
    87  	case PartialPositive3Phase, PartialNegative3Phase, PartialPositive23Phase, PartialNegative23Phase:
    88  		delete(pckts, cm.ignoreFrom)
    89  	}
    90  	return pckts, nil
    91  }
    92  
    93  func (cm *CommunicatorMock) Init(ctx context.Context) error {
    94  	return cm.communicator.Init(ctx)
    95  }