From 13b809993bc9bf67eaa142b0a0e0f85c9b1fa94b Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 14 Jan 2024 01:31:02 +0700 Subject: [PATCH] :white_check_mark: test: added function testing #14 --- example/ami_test.go | 21 +++++++++++++++++---- pkg/ami/ami.go | 2 +- pkg/ami/ami_dictionary.go | 2 +- pkg/ami/ami_error.go | 2 +- pkg/ami/ami_network.go | 10 +++++----- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/example/ami_test.go b/example/ami_test.go index 4478707..4119c53 100644 --- a/example/ami_test.go +++ b/example/ami_test.go @@ -2,13 +2,26 @@ package example import ( "testing" + "time" "github.com/pnguyen215/voipkit/pkg/ami" ) -func TestAmiClient(t *testing.T) { - c := ami.GetAmiClientSample() - ami.D().Info("ami client request: %v", c.String()) +func createConn() (*ami.AMI, error) { + c := ami.GetAmiClientSample(). + SetEnabled(true). + SetPort(5038). + SetUsername("monast"). + SetPassword("T5Monast"). + SetTimeout(5 * time.Second) + return ami.NewClient(ami.NewTcp(), *c) +} - ami.NewClient(ami.NewTcp(), *c) +func TestAmiClient(t *testing.T) { + _, err := createConn() + if err != nil { + ami.D().Error(err.Error()) + return + } + ami.D().Info("Authenticated successfully") } diff --git a/pkg/ami/ami.go b/pkg/ami/ami.go index 7fbb8d3..88ef67a 100644 --- a/pkg/ami/ami.go +++ b/pkg/ami/ami.go @@ -393,7 +393,7 @@ func create(conn net.Conn) (*AMI, context.Context) { _socket, err := WithSocket(ctx, addr) if err == nil { c.Socket = _socket - D().Info("Ami cloning (addr: %v) socket connection succeeded", addr) + D().Info("Ami network cloning (addr: %v) socket connection succeeded", addr) } } return c, ctx diff --git a/pkg/ami/ami_dictionary.go b/pkg/ami/ami_dictionary.go index f44f45f..2708ab5 100644 --- a/pkg/ami/ami_dictionary.go +++ b/pkg/ami/ami_dictionary.go @@ -228,7 +228,7 @@ func (d *AMIDictionary) GetDictionaries() *[]AMIEventDictionary { overlapDictionaries = &dictionaries dictionary, _ := d.FindDictionaryByKey(config.AmiListenerEventCommon) - log.Printf("Dictionaries (common) was initialized, len = %d", len(dictionary.Dictionaries)) + D().Info("Dictionaries (common) was initialized, len: %d", len(dictionary.Dictionaries)) return overlapDictionaries } diff --git a/pkg/ami/ami_error.go b/pkg/ami/ami_error.go index 2b09ee1..8198f7b 100644 --- a/pkg/ami/ami_error.go +++ b/pkg/ami/ami_error.go @@ -37,5 +37,5 @@ func (e *AmiError) ErrorWrap(message string, args ...interface{}) *AmiError { } func (e *AmiError) Error() string { - return fmt.Sprintf("ami has error ocurred: %s%s", e.S, e.E) + return fmt.Sprintf("Ami has an error ocurred: %s%s", e.S, e.E) } diff --git a/pkg/ami/ami_network.go b/pkg/ami/ami_network.go index 345f04f..d8dc7ec 100644 --- a/pkg/ami/ami_network.go +++ b/pkg/ami/ami_network.go @@ -77,21 +77,21 @@ func OnUdpConn(ip string, port int) (net.Conn, error) { // defer conn.Close() func NewNetwork(network, ip string, port int) (net.Conn, error) { if !config.AmiNetworkKeys[network] { - return nil, AmiErrorWrap("AMI: Invalid network") + return nil, AmiErrorWrap("Ami: Invalid network") } if IsStringEmpty(ip) { - return nil, AmiErrorWrap("AMI: IP must be not empty") + return nil, AmiErrorWrap("Ami: IP must be not empty") } if port <= 0 { - return nil, AmiErrorWrap("AMI: Port must be positive number") + return nil, AmiErrorWrap("Ami: Port must be positive number") } host, _port, _ := DecodeIp(ip) if len(host) > 0 && len(_port) > 0 { form := net.JoinHostPort(host, _port) - D().Info("AMI: (IP decoded) dial connection: %v", form) + D().Info("Ami (IP decoded) dial connection: %v", form) return net.Dial(network, form) } form := RemoveProtocol(ip, port) - D().Info("AMI: dial connection: %v", form) + D().Info("Ami dial connection: %v", form) return net.Dial(network, form) }