-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consumer and producer support unit mode
- Loading branch information
1 parent
896a8a3
commit cb8da2a
Showing
6 changed files
with
243 additions
and
9 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,71 @@ | ||
package consumer | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func getFieldString(obj interface{}, field string) string { | ||
v := reflect.Indirect(reflect.ValueOf(obj)) | ||
return v.FieldByNameFunc(func(n string) bool { | ||
return n == field | ||
}).String() | ||
} | ||
|
||
func TestWithUnitMode(t *testing.T) { | ||
opt := defaultPullConsumerOptions() | ||
WithUnitMode(true)(&opt) | ||
if !opt.UnitMode { | ||
t.Errorf("consumer option WithUnitMode. want:true, got=%v", opt.UnitMode) | ||
} | ||
} | ||
|
||
func TestWithUnitName(t *testing.T) { | ||
opt := defaultPullConsumerOptions() | ||
unitName := "unsh" | ||
WithUnitName(unitName)(&opt) | ||
if opt.UnitName != unitName { | ||
t.Errorf("consumer option WithUnitName. want:%s, got=%s", unitName, opt.UnitName) | ||
} | ||
} | ||
|
||
func TestWithNameServerDomain(t *testing.T) { | ||
opt := defaultPullConsumerOptions() | ||
nameServerAddr := "http://127.0.0.1:8080/nameserver/addr" | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if domainStr != nameServerAddr { | ||
t.Errorf("consumer option WithUnitName. want:%s, got=%s", nameServerAddr, domainStr) | ||
} | ||
} | ||
|
||
func TestWithNameServerDomainAndUnitName(t *testing.T) { | ||
nameServerAddr := "http://127.0.0.1:8080/nameserver/addr" | ||
unitName := "unsh" | ||
suffix := fmt.Sprintf("-%s?nofix=1", unitName) | ||
|
||
// test with two different orders | ||
t.Run("WithNameServerDomain & WithUnitName", func(t *testing.T) { | ||
opt := defaultPullConsumerOptions() | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
WithUnitName(unitName)(&opt) | ||
|
||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if !strings.Contains(domainStr, nameServerAddr) || !strings.Contains(domainStr, suffix) { | ||
t.Errorf("consumer option should contains %s and %s", nameServerAddr, suffix) | ||
} | ||
}) | ||
|
||
t.Run("WithUnitName & WithNameServerDomain", func(t *testing.T) { | ||
opt := defaultPullConsumerOptions() | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
WithUnitName(unitName)(&opt) | ||
|
||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if !strings.Contains(domainStr, nameServerAddr) || !strings.Contains(domainStr, suffix) { | ||
t.Errorf("consumer option should contains %s and %s", nameServerAddr, suffix) | ||
} | ||
}) | ||
} |
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
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,71 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func getFieldString(obj interface{}, field string) string { | ||
v := reflect.Indirect(reflect.ValueOf(obj)) | ||
return v.FieldByNameFunc(func(n string) bool { | ||
return n == field | ||
}).String() | ||
} | ||
|
||
func TestWithUnitMode(t *testing.T) { | ||
opt := defaultProducerOptions() | ||
WithUnitMode(true)(&opt) | ||
if !opt.UnitMode { | ||
t.Errorf("consumer option WithUnitMode. want:true, got=%v", opt.UnitMode) | ||
} | ||
} | ||
|
||
func TestWithUnitName(t *testing.T) { | ||
opt := defaultProducerOptions() | ||
unitName := "unsh" | ||
WithUnitName(unitName)(&opt) | ||
if opt.UnitName != unitName { | ||
t.Errorf("consumer option WithUnitName. want:%s, got=%s", unitName, opt.UnitName) | ||
} | ||
} | ||
|
||
func TestWithNameServerDomain(t *testing.T) { | ||
opt := defaultProducerOptions() | ||
nameServerAddr := "http://127.0.0.1:8080/nameserver/addr" | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if domainStr != nameServerAddr { | ||
t.Errorf("consumer option WithUnitName. want:%s, got=%s", nameServerAddr, domainStr) | ||
} | ||
} | ||
|
||
func TestWithNameServerDomainAndUnitName(t *testing.T) { | ||
nameServerAddr := "http://127.0.0.1:8080/nameserver/addr" | ||
unitName := "unsh" | ||
suffix := fmt.Sprintf("-%s?nofix=1", unitName) | ||
|
||
// test with two different orders | ||
t.Run("WithNameServerDomain & WithUnitName", func(t *testing.T) { | ||
opt := defaultProducerOptions() | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
WithUnitName(unitName)(&opt) | ||
|
||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if !strings.Contains(domainStr, nameServerAddr) || !strings.Contains(domainStr, suffix) { | ||
t.Errorf("consumer option should contains %s and %s", nameServerAddr, suffix) | ||
} | ||
}) | ||
|
||
t.Run("WithUnitName & WithNameServerDomain", func(t *testing.T) { | ||
opt := defaultProducerOptions() | ||
WithNameServerDomain(nameServerAddr)(&opt) | ||
WithUnitName(unitName)(&opt) | ||
|
||
domainStr := getFieldString(opt.Resolver, "domain") | ||
if !strings.Contains(domainStr, nameServerAddr) || !strings.Contains(domainStr, suffix) { | ||
t.Errorf("consumer option should contains %s and %s", nameServerAddr, suffix) | ||
} | ||
}) | ||
} |