-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for both IPv4 and IPv6 broker addresses. Signed-off-by: Ben Steadman <[email protected]>
- Loading branch information
1 parent
fd889cc
commit 37ecd21
Showing
2 changed files
with
149 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package kgo | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseBrokerAddr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addr string | ||
expected hostport | ||
}{ | ||
{ | ||
"IPv4", | ||
"127.0.0.1:1234", | ||
hostport{"127.0.0.1", 1234}, | ||
}, | ||
{ | ||
"IPv4 + default port", | ||
"127.0.0.1", | ||
hostport{"127.0.0.1", 9092}, | ||
}, | ||
{ | ||
"host", | ||
"localhost:1234", | ||
hostport{"localhost", 1234}, | ||
}, | ||
{ | ||
"host + default port", | ||
"localhost", | ||
hostport{"localhost", 9092}, | ||
}, | ||
{ | ||
"IPv6", | ||
"[2001:1000:2000::1]:1234", | ||
hostport{"2001:1000:2000::1", 1234}, | ||
}, | ||
{ | ||
"IPv6 + default port", | ||
"[2001:1000:2000::1]", | ||
hostport{"2001:1000:2000::1", 9092}, | ||
}, | ||
{ | ||
"IPv6 literal", | ||
"::1", | ||
hostport{"::1", 9092}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result, err := parseBrokerAddr(test.addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if result != test.expected { | ||
t.Fatalf("expected %v, got %v", test.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseBrokerAddrErrors(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addr string | ||
}{ | ||
{ | ||
"IPv4 invalid port", | ||
"127.0.0.1:foo", | ||
}, | ||
{ | ||
"host invalid port", | ||
"localhost:foo", | ||
}, | ||
|
||
{ | ||
"IPv6 invalid port", | ||
"[2001:1000:2000::1]:foo", | ||
}, | ||
{ | ||
"IPv6 missing closing bracket", | ||
"[2001:1000:2000::1:1234", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
_, err := parseBrokerAddr(test.addr) | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
}) | ||
} | ||
} |