Skip to content

Commit

Permalink
[FAB-4336] Switch partition UTs to the assert pkg
Browse files Browse the repository at this point in the history
Change-Id: I6bd7228a0541d1272eb5c4665bf059b9ac3d8cde
Signed-off-by: Kostas Christidis <kostas@christidis.io>
  • Loading branch information
kchristidis committed Jun 2, 2017
1 parent 56d06f7 commit f3eabe9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
32 changes: 11 additions & 21 deletions orderer/kafka/partitioner.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package kafka

import "github.com/Shopify/sarama"

type staticPartitioner struct {
partitionID int32
}

// newStaticPartitioner returns a PartitionerConstructor that
// returns a Partitioner that always chooses the specified partition.
func newStaticPartitioner(partition int32) sarama.PartitionerConstructor {
Expand All @@ -26,17 +20,13 @@ func newStaticPartitioner(partition int32) sarama.PartitionerConstructor {
}
}

type staticPartitioner struct {
partitionID int32
}

// Partition takes a message and partition count and chooses a partition.
func (p *staticPartitioner) Partition(message *sarama.ProducerMessage, numPartitions int32) (int32, error) {
return p.partitionID, nil
func (prt *staticPartitioner) Partition(message *sarama.ProducerMessage, numPartitions int32) (int32, error) {
return prt.partitionID, nil
}

// RequiresConsistency indicates to the user of the partitioner
// whether the mapping of key->partition is consistent or not.
func (p *staticPartitioner) RequiresConsistency() bool {
// RequiresConsistency indicates to the user of the partitioner whether the
// mapping of key->partition is consistent or not.
func (prt *staticPartitioner) RequiresConsistency() bool {
return true
}
26 changes: 6 additions & 20 deletions orderer/kafka/partitioner_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package kafka
Expand All @@ -20,23 +10,19 @@ import (
"testing"

"github.com/Shopify/sarama"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/stretchr/testify/assert"
)

func TestStaticPartitioner(t *testing.T) {
var partition int32 = 3
var numberOfPartitions int32 = 6

partitionerConstructor := newStaticPartitioner(partition)
partitioner := partitionerConstructor(provisional.TestChainID)
partitioner := partitionerConstructor("channelFoo")

for i := 0; i < 10; i++ {
assignedPartition, err := partitioner.Partition(new(sarama.ProducerMessage), numberOfPartitions)
if err != nil {
t.Fatal("Partitioner not functioning as expected:", err)
}
if assignedPartition != partition {
t.Fatalf("Partitioner not returning the expected partition - expected %d, got %v", partition, assignedPartition)
}
assert.NoError(t, err, "Partitioner not functioning as expected:", err)
assert.Equal(t, partition, assignedPartition, "Partitioner not returning the expected partition - expected %d, got %v", partition, assignedPartition)
}
}

0 comments on commit f3eabe9

Please sign in to comment.