diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 81164ac12..7d4db4047 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/coap-gateway/test/test.go b/coap-gateway/test/test.go index ad4884ff8..206d70111 100644 --- a/coap-gateway/test/test.go +++ b/coap-gateway/test/test.go @@ -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. diff --git a/device-provisioning-service/test/test.go b/device-provisioning-service/test/test.go index e09cf481d..ab6fcc25e 100644 --- a/device-provisioning-service/test/test.go +++ b/device-provisioning-service/test/test.go @@ -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. diff --git a/test/test.go b/test/test.go index fad511dee..ae1b6604b 100644 --- a/test/test.go +++ b/test/test.go @@ -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 } 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 } 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) + } }