|
| 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 | +) |
| 24 | + |
| 25 | +func TestStaticPartitioner(t *testing.T) { |
| 26 | + |
| 27 | + var partition int32 = 3 |
| 28 | + var numberOfPartitions int32 = 6 |
| 29 | + |
| 30 | + partitionerConstructor := newStaticPartitioner(partition) |
| 31 | + partitioner := partitionerConstructor(testConf.Kafka.Topic) |
| 32 | + |
| 33 | + for i := 0; i < 10; i++ { |
| 34 | + assignedPartition, err := partitioner.Partition(new(sarama.ProducerMessage), numberOfPartitions) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + if assignedPartition != partition { |
| 39 | + t.Fatalf("Expected: %v. Actual: %v", partition, assignedPartition) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestNewBrokerConfig(t *testing.T) { |
| 45 | + |
| 46 | + topic := testConf.Kafka.Topic |
| 47 | + |
| 48 | + // use a partition id that is not the 'default' 0 |
| 49 | + var partition int32 = 2 |
| 50 | + originalPartitionID := testConf.Kafka.PartitionID |
| 51 | + defer func() { |
| 52 | + testConf.Kafka.PartitionID = originalPartitionID |
| 53 | + }() |
| 54 | + testConf.Kafka.PartitionID = partition |
| 55 | + |
| 56 | + // setup a mock broker that reports that it has 3 partitions for the topic |
| 57 | + broker := sarama.NewMockBroker(t, 1000) |
| 58 | + broker.SetHandlerByMap(map[string]sarama.MockResponse{ |
| 59 | + "MetadataRequest": sarama.NewMockMetadataResponse(t). |
| 60 | + SetBroker(broker.Addr(), broker.BrokerID()). |
| 61 | + SetLeader(topic, 0, broker.BrokerID()). |
| 62 | + SetLeader(topic, 1, broker.BrokerID()). |
| 63 | + SetLeader(topic, 2, broker.BrokerID()), |
| 64 | + "ProduceRequest": sarama.NewMockProduceResponse(t), |
| 65 | + }) |
| 66 | + |
| 67 | + config := newBrokerConfig(testConf) |
| 68 | + producer, err := sarama.NewSyncProducer([]string{broker.Addr()}, config) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + for i := 0; i < 10; i++ { |
| 74 | + assignedPartition, _, err := producer.SendMessage(&sarama.ProducerMessage{Topic: topic}) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if assignedPartition != partition { |
| 79 | + t.Fatalf("Expected: %v. Actual: %v", partition, assignedPartition) |
| 80 | + } |
| 81 | + } |
| 82 | + producer.Close() |
| 83 | + broker.Close() |
| 84 | +} |
0 commit comments