github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/config_test.go (about)

     1  /**
     2   * Copyright 2016 Confluent Inc.
     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  
    24  // A custom type with Stringer interface to be used to test config map APIs
    25  type HostPortType struct {
    26  	Host string
    27  	Port int
    28  }
    29  
    30  // implements String() interface
    31  func (hp HostPortType) String() string {
    32  	return fmt.Sprintf("%s:%d", hp.Host, hp.Port)
    33  }
    34  
    35  //Test config map APIs
    36  func TestConfigMapAPIs(t *testing.T) {
    37  	config := &ConfigMap{}
    38  
    39  	// set a good key via SetKey()
    40  	err := config.SetKey("bootstrap.servers", testconf.Brokers)
    41  	if err != nil {
    42  		t.Errorf("Failed to set key via SetKey(). Error: %s\n", err)
    43  	}
    44  
    45  	// test custom Stringer type
    46  	hostPort := HostPortType{Host: "localhost", Port: 9092}
    47  	err = config.SetKey("bootstrap.servers", hostPort)
    48  	if err != nil {
    49  		t.Errorf("Failed to set custom Stringer type via SetKey(). Error: %s\n", err)
    50  	}
    51  
    52  	// test boolean type
    53  	err = config.SetKey("{topic}.produce.offset.report", true)
    54  	if err != nil {
    55  		t.Errorf("Failed to set key via SetKey(). Error: %s\n", err)
    56  	}
    57  
    58  	// test offset literal string
    59  	err = config.SetKey("{topic}.auto.offset.reset", "earliest")
    60  	if err != nil {
    61  		t.Errorf("Failed to set key via SetKey(). Error: %s\n", err)
    62  	}
    63  
    64  	//test offset constant
    65  	err = config.SetKey("{topic}.auto.offset.reset", OffsetBeginning)
    66  	if err != nil {
    67  		t.Errorf("Failed to set key via SetKey(). Error: %s\n", err)
    68  	}
    69  
    70  	//test integer offset
    71  	err = config.SetKey("{topic}.message.timeout.ms", 10)
    72  	if err != nil {
    73  		t.Errorf("Failed to set integer value via SetKey(). Error: %s\n", err)
    74  	}
    75  
    76  	// set a good key-value pair via Set()
    77  	err = config.Set("group.id=test.id")
    78  	if err != nil {
    79  		t.Errorf("Failed to set key-value pair via Set(). Error: %s\n", err)
    80  	}
    81  
    82  	// negative test cases
    83  	// set a bad key-value pair via Set()
    84  	err = config.Set("group.id:test.id2")
    85  	if err == nil {
    86  		t.Errorf("Expected failure when setting invalid key-value pair via Set()\n")
    87  	}
    88  
    89  	// get string value
    90  	v, err := config.Get("group.id", nil)
    91  	if err != nil {
    92  		t.Errorf("Expected Get(group.id) to succeed: %s\n", err)
    93  	}
    94  	if v == nil {
    95  		t.Errorf("Expected Get(group.id) to return non-nil value\n")
    96  	}
    97  	if v.(string) != "test.id" {
    98  		t.Errorf("group.id mismatch: %s\n", v)
    99  	}
   100  
   101  	// get string value but request int
   102  	dummyInt := 12
   103  	_, err = config.Get("group.id", dummyInt)
   104  	if err == nil {
   105  		t.Errorf("Expected Get(group.id) to fail\n")
   106  	}
   107  
   108  	// get integer value
   109  	v, err = config.Get("{topic}.message.timeout.ms", dummyInt)
   110  	if err != nil {
   111  		t.Errorf("Expected Get(message.timeout.ms) to succeed: %s\n", err)
   112  	}
   113  	if v == nil {
   114  		t.Errorf("Expected Get(message.timeout.ms) to return non-nil value\n")
   115  	}
   116  	if v.(int) != 10 {
   117  		t.Errorf("message.timeout.ms mismatch: %d\n", v.(int))
   118  	}
   119  
   120  	// get unknown value
   121  	v, err = config.Get("dummy.value.not.found", nil)
   122  	if v != nil {
   123  		t.Errorf("Expected nil for dummy value, got %v\n", v)
   124  	}
   125  
   126  }
   127  
   128  // Test that plugins will always be configured before their config options
   129  func TestConfigPluginPaths(t *testing.T) {
   130  	config := &ConfigMap{
   131  		"plugin.library.paths": "monitoring-interceptor",
   132  	}
   133  	_, err := config.convert()
   134  	if err != nil {
   135  		t.Skipf("Missing monitoring-interceptor: %s", err)
   136  	}
   137  
   138  	config = &ConfigMap{
   139  		"plugin.library.paths":                     "monitoring-interceptor",
   140  		"confluent.monitoring.interceptor.icdebug": true,
   141  	}
   142  
   143  	// convert() would fail randomly due to random order of ConfigMap key iteration.
   144  	// running convert() once gave the test case a 50% failure chance,
   145  	// running it 100 times gives ~100%
   146  	for i := 1; i <= 100; i++ {
   147  		_, err := config.convert()
   148  		if err != nil {
   149  			t.Fatalf("Failed to convert. Error: %s\n", err)
   150  		}
   151  	}
   152  }