Skip to content

Commit

Permalink
fix: integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Oct 16, 2023
1 parent 0c3b5fe commit 0df3d25
Showing 1 changed file with 34 additions and 59 deletions.
93 changes: 34 additions & 59 deletions server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing"

"github.com/emiago/sipgo/sip"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -83,8 +82,6 @@ func TestIntegrationClientServer(t *testing.T) {
return
}

ua, _ := NewUA()

serverTLS := testServerTlsConfig(t)
clientTLS := testClientTlsConfig(t)

Expand All @@ -100,36 +97,33 @@ func TestIntegrationClientServer(t *testing.T) {
{transport: "wss", serverAddr: "127.1.1.100:5063", encrypted: true},
}

srv, err := NewServer(ua)
if err != nil {
log.Fatal().Err(err).Msg("Fail to setup dialog server")
}

srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
t.Log("Invite received")
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
if err := tx.Respond(res); err != nil {
t.Fatal(err)
}
<-tx.Done()
})

ctx, shutdown := context.WithCancel(context.Background())
wg := sync.WaitGroup{}

t.Cleanup(func() {
shutdown()
wg.Wait()
})

for _, tc := range testCases {
wg.Add(1)
ua, _ := NewUA()
srv, err := NewServer(ua)
require.NoError(t, err)

srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
t.Log("Invite received")
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
if err := tx.Respond(res); err != nil {
t.Fatal(err)
}
<-tx.Done()
})

// Trick to make sure we are listening
serverReady := make(chan struct{})
ctx = context.WithValue(ctx, ListenReadyCtxKey, ListenReadyCtxValue(serverReady))

go func(transport string, serverAddr string, encrypted bool) {
wg.Add(1)
go func(srv *Server, transport string, serverAddr string, encrypted bool) {
defer wg.Done()

if encrypted {
Expand All @@ -144,32 +138,20 @@ func TestIntegrationClientServer(t *testing.T) {
if err != nil && !errors.Is(err, net.ErrClosed) {
t.Error("ListenAndServe error: ", err)
}
}(tc.transport, tc.serverAddr, tc.encrypted)
}(srv, tc.transport, tc.serverAddr, tc.encrypted)
<-serverReady
t.Log("Server ready")
}

t.Log("Server ready")

for _, tc := range testCases {
t.Run(tc.transport, func(t *testing.T) {
// Doing gracefull shutdown

// Build UAC
ua, _ = NewUA(WithUserAgenTLSConfig(clientTLS))
ua, _ := NewUA(WithUserAgenTLSConfig(clientTLS))
client, err := NewClient(ua)
require.NoError(t, err)

// csrv, err := NewServer(ua) // Create server handle
// require.Nil(t, err)

// wg.Add(1)
// go func() {
// defer wg.Done()
// err := csrv.ListenAndServe(ctx, tc.transport, "127.1.1.200:5066")
// if err != nil && !errors.Is(err, net.ErrClosed) {
// t.Error("ListenAndServe error: ", err)
// }
// }()
proto := "sip"
if tc.encrypted {
proto = "sips"
Expand All @@ -193,8 +175,6 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
return
}

ua, _ := NewUA()

serverTLS := testServerTlsConfig(t)
clientTLS := testClientTlsConfig(t)

Expand All @@ -210,35 +190,33 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
{transport: "wss", serverAddr: "127.1.1.100:5063", encrypted: true},
}

srv, err := NewServer(ua)
if err != nil {
log.Fatal().Err(err).Msg("Fail to setup dialog server")
}

srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
if err := tx.Respond(res); err != nil {
t.Fatal(err)
}
<-tx.Done()
})

ctx, shutdown := context.WithCancel(context.Background())
wg := sync.WaitGroup{}

t.Cleanup(func() {
shutdown()
wg.Wait()
})

for _, tc := range testCases {
wg.Add(1)
ua, _ := NewUA()
srv, err := NewServer(ua)
require.NoError(t, err)

srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
t.Log("Invite received")
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
if err := tx.Respond(res); err != nil {
t.Fatal(err)
}
<-tx.Done()
})

// Trick to make sure we are listening
serverReady := make(chan struct{})
ctx = context.WithValue(ctx, ListenReadyCtxKey, ListenReadyCtxValue(serverReady))

go func(transport string, serverAddr string, encrypted bool) {
wg.Add(1)
go func(srv *Server, transport string, serverAddr string, encrypted bool) {
defer wg.Done()

if encrypted {
Expand All @@ -253,18 +231,15 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
if err != nil && !errors.Is(err, net.ErrClosed) {
t.Error("ListenAndServe error: ", err)
}
}(tc.transport, tc.serverAddr, tc.encrypted)
}(srv, tc.transport, tc.serverAddr, tc.encrypted)
<-serverReady
t.Log("Server ready")
}

t.Log("Server ready")

for _, tc := range testCases {
t.Run(tc.transport, func(t *testing.B) {
// Doing gracefull shutdown

// Build UAC
ua, _ = NewUA(WithUserAgenTLSConfig(clientTLS))
ua, _ := NewUA(WithUserAgenTLSConfig(clientTLS))
client, err := NewClient(ua)
require.NoError(t, err)

Expand Down

0 comments on commit 0df3d25

Please sign in to comment.