Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get dialer name #198

Merged
merged 5 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/app/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"bytes"
"context"
"fmt"
"reflect"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -532,6 +534,30 @@ func (c *Client) SetClientFactory(cf suite.ClientFactory) {
c.clientFactory = cf
}

// GetDialerName returns the name of the dialer
func (c *Client) GetDialerName() (dName string, err error) {
defer func() {
err := recover()
if err != nil {
dName = "unknown"
}
}()

opt := c.GetOptions()
if opt == nil || opt.Dialer == nil {
return "", fmt.Errorf("abnormal process: there is no client options or dialer")
}

dName = reflect.TypeOf(opt.Dialer).String()
dSlice := strings.Split(dName, ".")
dName = dSlice[0]
if dName[0] == '*' {
dName = dName[1:]
}

return
}

// NewClient return a client with options
func NewClient(opts ...config.ClientOption) (*Client, error) {
opt := config.NewClientOptions(opts)
Expand Down
49 changes: 49 additions & 0 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,3 +1879,52 @@ func newMockDialerWithCustomFunc(network, address string, timeout time.Duration,
timeout: timeout,
}
}

func TestClientDialerName(t *testing.T) {
client, _ := NewClient()
dName, err := client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Depending on the operating system,
// the default dialer has a different network library, either "netpoll" or "standard"
if !(dName == "netpoll" || dName == "standard") {
t.Errorf("expected 'netpoll', but get %s", dName)
}

client, _ = NewClient(WithDialer(netpoll.NewDialer()))
dName, err = client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "netpoll" {
t.Errorf("expected 'standard', but get %s", dName)
}

client, _ = NewClient(WithDialer(standard.NewDialer()))
dName, err = client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "standard" {
t.Errorf("expected 'standard', but get %s", dName)
}

client, _ = NewClient(WithDialer(&mockDialer{}))
dName, err = client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "client" {
t.Errorf("expected 'client', but get %s", dName)
}

client.options.Dialer = nil
dName, err = client.GetDialerName()
if err == nil {
t.Errorf("expected an err for abnormal process")
}
if dName != "" {
t.Errorf("expected 'empty string', but get %s", dName)
}
}