github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/broker_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  	"testing"
    21  
    22  	"github.com/Shopify/sarama"
    23  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    24  )
    25  
    26  func TestBrokerGetOffset(t *testing.T) {
    27  	t.Run("oldest", testBrokerGetOffsetFunc(sarama.OffsetOldest, testOldestOffset))
    28  	t.Run("newest", testBrokerGetOffsetFunc(sarama.OffsetNewest, testNewestOffset))
    29  }
    30  
    31  func testBrokerGetOffsetFunc(given, expected int64) func(t *testing.T) {
    32  	cp := newChainPartition(provisional.TestChainID, rawPartition)
    33  	return func(t *testing.T) {
    34  		mb, _ := mockNewBroker(t, cp)
    35  		defer testClose(t, mb)
    36  
    37  		ofs, _ := mb.GetOffset(cp, newOffsetReq(cp, given))
    38  		if ofs != expected {
    39  			t.Fatalf("Expected offset %d, got %d instead", expected, ofs)
    40  		}
    41  	}
    42  }
    43  
    44  func TestNewBrokerReturnsPartitionLeader(t *testing.T) {
    45  	cp := newChainPartition(provisional.TestChainID, rawPartition)
    46  	broker1 := sarama.NewMockBroker(t, 1)
    47  	broker2 := sarama.NewMockBroker(t, 2)
    48  	broker3 := sarama.NewMockBroker(t, 3)
    49  	defer func() {
    50  		broker2.Close()
    51  		broker3.Close()
    52  	}()
    53  
    54  	// Use broker1 and broker2 as bootstrap brokers, but shutdown broker1 right away
    55  	broker1.Close()
    56  
    57  	// Add expectation that broker2 will return a metadata response
    58  	// that identifies broker3 as the topic partition leader
    59  	broker2.SetHandlerByMap(map[string]sarama.MockResponse{
    60  		"MetadataRequest": sarama.NewMockMetadataResponse(t).
    61  			SetBroker(broker1.Addr(), broker1.BrokerID()).
    62  			SetBroker(broker2.Addr(), broker2.BrokerID()).
    63  			SetBroker(broker3.Addr(), broker3.BrokerID()).
    64  			SetLeader(cp.Topic(), cp.Partition(), broker3.BrokerID()),
    65  	})
    66  
    67  	// Add expectation that broker3 responds to an offset request
    68  	broker3.SetHandlerByMap(map[string]sarama.MockResponse{
    69  		"OffsetRequest": sarama.NewMockOffsetResponse(t).
    70  			SetOffset(cp.Topic(), cp.Partition(), sarama.OffsetOldest, testOldestOffset).
    71  			SetOffset(cp.Topic(), cp.Partition(), sarama.OffsetNewest, testNewestOffset),
    72  	})
    73  
    74  	// Get leader for the test chain partition
    75  	leaderBroker, _ := newBroker([]string{broker1.Addr(), broker2.Addr()}, cp)
    76  
    77  	// Only broker3 will respond successfully to an offset request
    78  	offsetRequest := new(sarama.OffsetRequest)
    79  	offsetRequest.AddBlock(cp.Topic(), cp.Partition(), -1, 1)
    80  	if _, err := leaderBroker.GetOffset(cp, offsetRequest); err != nil {
    81  		t.Fatal("Expected leader broker to respond to request:", err)
    82  	}
    83  }