MCPcopy
hub / github.com/IBM/sarama / TestMurmur2Partitioner

Function TestMurmur2Partitioner

partitioner_test.go:346–412  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

344}
345
346func TestMurmur2Partitioner(t *testing.T) {
347 partitioner := NewMurmur2Partitioner("mytopic")
348
349 t.Run("single partition always returns 0", func(t *testing.T) {
350 choice, err := partitioner.Partition(&ProducerMessage{Key: StringEncoder("foo")}, 1)
351 if err != nil {
352 t.Fatal(err)
353 }
354 if choice != 0 {
355 t.Errorf("expected partition 0, got %d", choice)
356 }
357 })
358
359 t.Run("nil key returns random partition in range", func(t *testing.T) {
360 for range 50 {
361 choice, err := partitioner.Partition(&ProducerMessage{}, 50)
362 if err != nil {
363 t.Fatal(err)
364 }
365 if choice < 0 || choice >= 50 {
366 t.Errorf("partition %d out of range [0, 50)", choice)
367 }
368 }
369 })
370
371 t.Run("consistent for same key", func(t *testing.T) {
372 assertPartitioningConsistent(t, partitioner, &ProducerMessage{Key: StringEncoder("foo")}, 100)
373 assertPartitioningConsistent(t, partitioner, &ProducerMessage{Key: StringEncoder("bar")}, 100)
374 })
375
376 t.Run("partition in range for various keys", func(t *testing.T) {
377 keys := []string{"foo", "bar", "baz", "kafka", "sarama", "1468509572224"}
378 for _, key := range keys {
379 choice, err := partitioner.Partition(&ProducerMessage{Key: StringEncoder(key)}, 100)
380 if err != nil {
381 t.Fatalf("key %q: %v", key, err)
382 }
383 if choice < 0 || choice >= 100 {
384 t.Errorf("key %q: partition %d out of range [0, 100)", key, choice)
385 }
386 }
387 })
388
389 t.Run("known Java-compatible partition assignments", func(t *testing.T) {
390 // these expected partitions were verified against the Apache Kafka Java
391 // client's DefaultPartitioner with 100 partitions
392 testCases := []struct {
393 key string
394 partition int32
395 }{
396 {"foo", 16},
397 {"bar", 45},
398 {"baz", 80},
399 {"kafka", 80},
400 {"sarama", 11},
401 }
402 for _, tc := range testCases {
403 choice, err := partitioner.Partition(&ProducerMessage{Key: StringEncoder(tc.key)}, 100)

Callers

nothing calls this directly

Calls 8

NewMurmur2PartitionerFunction · 0.85
StringEncoderTypeAlias · 0.85
RunMethod · 0.80
FatalMethod · 0.80
FatalfMethod · 0.80
PartitionMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected