Skip to content

Commit

Permalink
✅ test: added new function for testing #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 14, 2024
1 parent 6b96227 commit 5d2d3c6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
51 changes: 51 additions & 0 deletions example/ami_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package example

import (
"fmt"
"log"
"testing"
"time"

Expand Down Expand Up @@ -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)))
}
10 changes: 10 additions & 0 deletions pkg/ami/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/ami/ami_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/ami/ami_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type AmiClient struct {
}

type AMI struct {
ctx context.Context
err chan error
mutex sync.RWMutex
conn net.Conn
Expand Down

0 comments on commit 5d2d3c6

Please sign in to comment.