Skip to content

Commit

Permalink
Merge branch 'feat/dialer_name' of https://github.com/FGYFFFF/hertz i…
Browse files Browse the repository at this point in the history
…nto feat/dialer_name
  • Loading branch information
FGYFFFF committed Sep 19, 2022
2 parents c07e8d0 + 2e973df commit 1c33bf3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
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 @@ -540,6 +542,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() {
err1 := recover()
if err1 != 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
38 changes: 38 additions & 0 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,3 +1879,41 @@ 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.Fatal(fmt.Sprintf("unexpected error: %v", err))
}
if dName != "netpoll" {
t.Errorf("expected 'netpoll', but get %s", dName)
}

client, _ = NewClient(WithDialer(standard.NewDialer()))
dName, err = client.GetDialerName()
if err != nil {
t.Fatal(fmt.Sprintf("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.Fatal(fmt.Sprintf("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)
}
}

0 comments on commit 1c33bf3

Please sign in to comment.