Skip to content

Commit

Permalink
Add udp start test (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Oct 17, 2024
1 parent 8ca8c55 commit be56986
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions pkg/backends/udp_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package backends

import (
"context"
"net"
"sync"
"testing"

"github.com/ansible/receptor/pkg/logger"
"github.com/ansible/receptor/pkg/netceptor"
)

func TestNewUDPListener(t *testing.T) {
Expand Down Expand Up @@ -53,3 +55,73 @@ func TestNewUDPListener(t *testing.T) {
})
}
}

func TestUDPListenerStart(t *testing.T) {
type fields struct {
laddr *net.UDPAddr
conn *net.UDPConn
sessChan chan *UDPListenerSession
sessionRegistry map[string]*UDPListenerSession
logger *logger.ReceptorLogger
}

goodLogger := logger.NewReceptorLogger("UDPtest")
type args struct {
ctx context.Context
wg *sync.WaitGroup
}
tests := []struct {
name string
fields fields
args args
want chan netceptor.BackendSession
wantErr bool
}{
{
name: "Positive",
fields: fields{
laddr: &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 9999,
Zone: "",
},
conn: &net.UDPConn{},
sessChan: make(chan *UDPListenerSession),
sessionRegistry: make(map[string]*UDPListenerSession),
logger: goodLogger,
},
args: args{
ctx: context.Background(),
wg: &sync.WaitGroup{},
},
want: make(chan netceptor.BackendSession),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uc, err := net.ListenUDP("udp", tt.fields.laddr)
if err != nil {
t.Errorf("ListenUDP error = %+v", err)
}

b := &UDPListener{
laddr: tt.fields.laddr,
conn: uc,
sessChan: tt.fields.sessChan,
sessRegLock: sync.RWMutex{},
sessionRegistry: tt.fields.sessionRegistry,
logger: tt.fields.logger,
}
got, err := b.Start(tt.args.ctx, tt.args.wg)
if (err != nil) != tt.wantErr {
t.Errorf("UDPListener.Start() error = %v, wantErr %v", err, tt.wantErr)

return
}
if got == nil {
t.Errorf("UDPListener.Start() returned nil")
}
})
}
}

0 comments on commit be56986

Please sign in to comment.