github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/broker_mock_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kafka
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/Shopify/sarama"
    24  )
    25  
    26  type mockBrockerImpl struct {
    27  	brokerImpl
    28  
    29  	mockBroker *sarama.MockBroker
    30  	handlerMap map[string]sarama.MockResponse
    31  }
    32  
    33  func mockNewBroker(t *testing.T, cp ChainPartition) (Broker, error) {
    34  	mockBroker := sarama.NewMockBroker(t, testBrokerID)
    35  	handlerMap := make(map[string]sarama.MockResponse)
    36  	// The sarama mock package doesn't allow us to return an error
    37  	// for invalid offset requests, so we return an offset of -1.
    38  	// Note that the mock offset responses below imply a broker with
    39  	// newestOffset-1 blocks available. Therefore, if you are using this
    40  	// broker as part of a bigger test where you intend to consume blocks,
    41  	// make sure that the mockConsumer has been initialized accordingly
    42  	// (Set the 'offset' parameter to newestOffset-1.)
    43  	handlerMap["OffsetRequest"] = sarama.NewMockOffsetResponse(t).
    44  		SetOffset(cp.Topic(), cp.Partition(), sarama.OffsetOldest, testOldestOffset).
    45  		SetOffset(cp.Topic(), cp.Partition(), sarama.OffsetNewest, testNewestOffset)
    46  	mockBroker.SetHandlerByMap(handlerMap)
    47  
    48  	broker := sarama.NewBroker(mockBroker.Addr())
    49  	if err := broker.Open(nil); err != nil {
    50  		return nil, fmt.Errorf("Cannot connect to mock broker: %s", err)
    51  	}
    52  
    53  	return &mockBrockerImpl{
    54  		brokerImpl: brokerImpl{
    55  			broker: broker,
    56  		},
    57  		mockBroker: mockBroker,
    58  		handlerMap: handlerMap,
    59  	}, nil
    60  }
    61  
    62  func (mb *mockBrockerImpl) Close() error {
    63  	mb.mockBroker.Close()
    64  	return nil
    65  }