From 5d2d3c64ce0ea6f2c99a4b7550fefaf8a9fa45f9 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 14 Jan 2024 12:12:22 +0700 Subject: [PATCH] :white_check_mark: test: added new function for testing #14 --- example/ami_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ pkg/ami/ami.go | 10 +++++++++ pkg/ami/ami_core.go | 2 +- pkg/ami/ami_model.go | 1 + 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/example/ami_test.go b/example/ami_test.go index e6252ee..3c8fbb4 100644 --- a/example/ami_test.go +++ b/example/ami_test.go @@ -1,6 +1,8 @@ package example import ( + "fmt" + "log" "testing" "time" @@ -44,3 +46,52 @@ func TestDialOut(t *testing.T) { func TestChanspy(t *testing.T) { } + +func TestGetSIPPeersStatus(t *testing.T) { + c, err := createConn() + if err != nil { + ami.D().Error(err.Error()) + return + } + c.Core().AddSession() + peers, err := c.Core().GetSIPPeersStatus(c.Context()) + if err != nil { + ami.D().Error(err.Error()) + return + } + log.Println(fmt.Sprintf("SIP peer status: %v", ami.JsonString(peers[1]))) +} + +func TestGetQueueStatuses(t *testing.T) { + c, err := createConn() + if err != nil { + ami.D().Error(err.Error()) + return + } + c.Core().AddSession() + c.Core().Dictionary.SetEnabledForceTranslate(true) + c.Core().Dictionary.AddKeyLinkTranslator("https://raw.githubusercontent.com/pnguyen215/gear-insights-free/master/ami.dictionaries.json") + peers, err := c.Core().GetQueueStatuses(c.Context(), "") + if err != nil { + ami.D().Error(err.Error()) + return + } + log.Println(fmt.Sprintf("SIP queues status: %v", ami.JsonString(peers))) +} + +func TestGetQueueSummary(t *testing.T) { + c, err := createConn() + if err != nil { + ami.D().Error(err.Error()) + return + } + c.Core().AddSession() + c.Core().Dictionary.SetEnabledForceTranslate(true) + c.Core().Dictionary.AddKeyLinkTranslator("https://raw.githubusercontent.com/pnguyen215/gear-insights-free/master/ami.dictionaries.json") + peers, err := c.Core().GetQueueSummary(c.Context(), "") + if err != nil { + ami.D().Error(err.Error()) + return + } + log.Println(fmt.Sprintf("SIP queues summary: %v", ami.JsonString(peers))) +} diff --git a/pkg/ami/ami.go b/pkg/ami/ami.go index abdeceb..958b904 100644 --- a/pkg/ami/ami.go +++ b/pkg/ami/ami.go @@ -28,6 +28,15 @@ func (c *AMI) Core() *AMICore { return c.c } +func (c *AMI) SetContext(value context.Context) *AMI { + c.ctx = value + return c +} + +func (c *AMI) Context() context.Context { + return c.ctx +} + // Action sends an AMI action message to the Asterisk server. // If the action message does not have an ActionID, it adds one automatically. // The method returns true if the action message is successfully sent, otherwise false. @@ -457,6 +466,7 @@ func serve(conn net.Conn, request AmiClient) (*AMI, error) { return ins, err } ins.SetCore(c) + ins.SetContext(ctx) ins.release(ctx) return ins, nil } diff --git a/pkg/ami/ami_core.go b/pkg/ami/ami_core.go index b127065..bf218d7 100644 --- a/pkg/ami/ami_core.go +++ b/pkg/ami/ami_core.go @@ -41,7 +41,7 @@ func (c *AMICore) SetDictionary(dictionary *AMIDictionary) *AMICore { return c } -func (c *AMICore) ResetUUID() *AMICore { +func (c *AMICore) AddSession() *AMICore { c.SetUUID(GenUUIDShorten()) c.socket.SetUUID(c.UUID) return c diff --git a/pkg/ami/ami_model.go b/pkg/ami/ami_model.go index f40a7c3..40c4a79 100644 --- a/pkg/ami/ami_model.go +++ b/pkg/ami/ami_model.go @@ -27,6 +27,7 @@ type AmiClient struct { } type AMI struct { + ctx context.Context err chan error mutex sync.RWMutex conn net.Conn