Quellcode durchsuchen

Merge pull request #664 from slaunay/bugfix/empty-client-id

Forbid empty ClientID and use 'sarama' as default
Evan Huus vor 9 Jahren
Ursprung
Commit
ee044df3c7
2 geänderte Dateien mit 15 neuen und 3 gelöschten Zeilen
  1. 6 2
      config.go
  2. 9 1
      config_test.go

+ 6 - 2
config.go

@@ -6,7 +6,9 @@ import (
 	"time"
 )
 
-var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]*\z`)
+const defaultClientID = "sarama"
+
+var validID *regexp.Regexp = regexp.MustCompile(`\A[A-Za-z0-9._-]+\z`)
 
 // Config is used to pass multiple configuration options to Sarama's constructors.
 type Config struct {
@@ -258,6 +260,8 @@ func NewConfig() *Config {
 
 	c.ChannelBufferSize = 256
 
+	c.ClientID = defaultClientID
+
 	return c
 }
 
@@ -297,7 +301,7 @@ func (c *Config) Validate() error {
 	if c.Consumer.Offsets.Retention%time.Millisecond != 0 {
 		Logger.Println("Consumer.Offsets.Retention only supports millisecond precision; nanoseconds will be truncated.")
 	}
-	if c.ClientID == "sarama" {
+	if c.ClientID == defaultClientID {
 		Logger.Println("ClientID is the default of 'sarama', you should consider setting it to something application-specific.")
 	}
 

+ 9 - 1
config_test.go

@@ -9,10 +9,18 @@ func TestDefaultConfigValidates(t *testing.T) {
 	}
 }
 
-func TestClientIDValidates(t *testing.T) {
+func TestInvalidClientIDConfigValidates(t *testing.T) {
 	config := NewConfig()
 	config.ClientID = "foo:bar"
 	if err := config.Validate(); string(err.(ConfigurationError)) != "ClientID is invalid" {
 		t.Error("Expected invalid ClientID, got ", err)
 	}
 }
+
+func TestEmptyClientIDConfigValidates(t *testing.T) {
+	config := NewConfig()
+	config.ClientID = ""
+	if err := config.Validate(); string(err.(ConfigurationError)) != "ClientID is invalid" {
+		t.Error("Expected invalid ClientID, got ", err)
+	}
+}