diff --git a/config.go b/config.go index 74577b57a..21a3c8191 100644 --- a/config.go +++ b/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.") } diff --git a/config_test.go b/config_test.go index 243572985..08bcaa421 100644 --- a/config_test.go +++ b/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) + } +}