Skip to content

Commit

Permalink
Debug closing of sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Aug 14, 2024
1 parent b5324df commit 07c1311
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 56 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Ports
run: |
sudo netstat -tulpn | grep LISTEN
- name: CPU Info
run: |
cat /proc/cpuinfo
Expand Down
26 changes: 7 additions & 19 deletions coap-gateway/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,14 @@ func SetUp(t require.TestingT) (tearDown func()) {
}

func checkForClosedSockets(t require.TestingT, cfg service.Config) {
protocolClosed := make([]bool, len(cfg.APIs.COAP.Protocols))
// wait for all sockets to be closed - max 3 minutes = 900*200
for j := 0; j < 900; j++ {
allClosed := true
for i, protocol := range cfg.APIs.COAP.Protocols {
if protocolClosed[i] {
continue
}
protocolClosed[i] = test.IsListenSocketClosed(t, string(protocol), cfg.APIs.COAP.Addr)
if protocolClosed[i] {
continue
}
allClosed = false
break
}
if allClosed {
break
}
time.Sleep(time.Millisecond * 200)
sockets := make(test.ListenSockets, 0, len(cfg.APIs.COAP.Protocols))
for _, protocol := range cfg.APIs.COAP.Protocols {
sockets = append(sockets, test.ListenSocket{
Network: string(protocol),
Address: cfg.APIs.COAP.Addr,
})
}
sockets.CheckForClosedSockets(t)
}

// New creates test coap-gateway.
Expand Down
34 changes: 15 additions & 19 deletions device-provisioning-service/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,20 @@ func New(t *testing.T, cfg service.Config, opts ...service.Option) func() {
}

func checkForClosedSockets(t require.TestingT, cfg service.Config) {
protocolClosed := make([]bool, len(cfg.APIs.COAP.Config.Protocols))
// wait for all sockets to be closed - max 3 minutes = 900*200
for j := 0; j < 900; j++ {
allClosed := true
for i, protocol := range cfg.APIs.COAP.Config.Protocols {
if protocolClosed[i] {
continue
}
protocolClosed[i] = hubTest.IsListenSocketClosed(t, string(protocol), cfg.APIs.COAP.Config.Addr)
if protocolClosed[i] {
continue
}
allClosed = false
break
}
if allClosed {
break
}
time.Sleep(time.Millisecond * 200)
sockets := make(hubTest.ListenSockets, 0, len(cfg.APIs.COAP.Protocols)+1)
for _, protocol := range cfg.APIs.COAP.Protocols {
sockets = append(sockets, hubTest.ListenSocket{
Network: string(protocol),
Address: cfg.APIs.COAP.Addr,
})
}
if cfg.APIs.HTTP.Enabled {
sockets = append(sockets, hubTest.ListenSocket{
Network: "tcp",
Address: cfg.APIs.HTTP.Config.Connection.Addr,
})
}
sockets.CheckForClosedSockets(t)
}

// New creates test dps-gateway.
Expand All @@ -309,6 +303,8 @@ func NewWithContext(ctx context.Context, t *testing.T, cfg service.Config, opts
require.NoError(t, err)

checkForClosedSockets(t, cfg)
// wait for all connections to be closed
time.Sleep(time.Millisecond * 500)
}
}

Expand Down
70 changes: 56 additions & 14 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,25 +999,67 @@ func GenerateDeviceIDbyIdx(deviceIndex int) string {
return GenerateIDbyIdx("d", deviceIndex)
}

func IsListenSocketClosed(t require.TestingT, target string, addStr string) bool {
if strings.Contains(target, "udp") {
addr, err := net.ResolveUDPAddr(target, addStr)
require.NoError(t, err)
c, err := net.ListenUDP(target, addr)
type ListenSocket struct {
Network string
Address string
}

func (ls *ListenSocket) IsClosed() (bool, error) {
if strings.Contains(ls.Network, "udp") {
addr, err := net.ResolveUDPAddr(ls.Network, ls.Address)
if err != nil {
return false
return false, err
}
c, err := net.ListenUDP(ls.Network, addr)
if err != nil {
return false, nil

Check failure on line 1015 in test/test.go

View workflow job for this annotation

GitHub Actions / lint

error is not nil (line 1013) but it returns nil (nilerr)
}
err = c.Close()
require.NoError(t, err)
return true
if err != nil {
return false, err
}
return true, nil
}
addr, err := net.ResolveTCPAddr(target, addStr)
require.NoError(t, err)
c, err := net.ListenTCP(target, addr)

addr, err := net.ResolveTCPAddr(ls.Network, ls.Address)
if err != nil {
return false
return false, err
}
c, err := net.ListenTCP(ls.Network, addr)
if err != nil {
return false, nil

Check failure on line 1030 in test/test.go

View workflow job for this annotation

GitHub Actions / lint

error is not nil (line 1028) but it returns nil (nilerr)
}
err = c.Close()
require.NoError(t, err)
return true
if err != nil {
return false, err
}
return true, nil
}

type ListenSockets []ListenSocket

func (ls ListenSockets) CheckForClosedSockets(t require.TestingT) {
// wait for all sockets to be closed - max 3 minutes = 900*200
socketClosed := make([]bool, len(ls))
for j := 0; j < 900; j++ {
allClosed := true
for i, socket := range ls {
if socketClosed[i] {
continue
}
fmt.Printf("Check socket: %v\n", socket)
closed, err := socket.IsClosed()
require.NoError(t, err)
socketClosed[i] = closed
if socketClosed[i] {
fmt.Printf("Socket closed: %v\n", socket)
continue
}
allClosed = false
}
if allClosed {
break
}
time.Sleep(time.Millisecond * 200)
}
}

0 comments on commit 07c1311

Please sign in to comment.