Skip to content

Commit

Permalink
Closing of sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Aug 15, 2024
1 parent 3b09c6b commit 34f74f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
7 changes: 4 additions & 3 deletions coap-gateway/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func SetUp(t require.TestingT) (tearDown func()) {
return New(t, MakeConfig(t))
}

func checkForClosedSockets(t require.TestingT, cfg service.Config) {
func checkForClosedSockets(cfg service.Config) error {
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)
return sockets.CheckForClosedSockets()
}

// New creates test coap-gateway.
Expand All @@ -106,6 +106,7 @@ func New(t require.TestingT, cfg service.Config) func() {
err = fileWatcher.Close()
require.NoError(t, err)

checkForClosedSockets(t, cfg)
err = checkForClosedSockets(cfg)
require.NoError(t, err)
}
}
62 changes: 36 additions & 26 deletions device-provisioning-service/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ func init() {
},
},
}

if err := checkForClosedSockets(MakeAPIsConfig()); err != nil {
panic(err)
}
}

func MakeAPIsConfig() service.APIsConfig {
var cfg service.APIsConfig
cfg.COAP.Addr = DPSHost
cfg.COAP.MaxMessageSize = 256 * 1024
cfg.COAP.MessagePoolSize = 1000
cfg.COAP.Protocols = []pkgCoapService.Protocol{pkgCoapService.TCP}
if config.DPS_UDP_ENABLED {
cfg.COAP.Protocols = append(cfg.COAP.Protocols, pkgCoapService.UDP)
}
cfg.COAP.InactivityMonitor = &pkgCoapService.InactivityMonitor{
Timeout: time.Second * 20,
}
cfg.COAP.BlockwiseTransfer.Enabled = config.DPS_UDP_ENABLED
cfg.COAP.BlockwiseTransfer.SZX = "1024"
cfg.HTTP = MakeHTTPConfig()
tlsServerCfg := config.MakeTLSServerConfig()
cfg.COAP.TLS.Embedded.CertFile = tlsServerCfg.CertFile
cfg.COAP.TLS.Embedded.KeyFile = tlsServerCfg.KeyFile
return cfg
}

func MakeConfig(t require.TestingT) service.Config {
Expand All @@ -194,27 +219,11 @@ func MakeConfig(t require.TestingT) service.Config {
cfg.Log = log.MakeDefaultConfig()
cfg.TaskQueue.GoPoolSize = 1600
cfg.TaskQueue.Size = 2 * 1024 * 1024
cfg.APIs.COAP.Addr = DPSHost
cfg.APIs.COAP.MaxMessageSize = 256 * 1024
cfg.APIs.COAP.MessagePoolSize = 1000
cfg.APIs.COAP.Protocols = []pkgCoapService.Protocol{pkgCoapService.TCP}
if config.DPS_UDP_ENABLED {
cfg.APIs.COAP.Protocols = append(cfg.APIs.COAP.Protocols, pkgCoapService.UDP)
}
cfg.APIs.COAP.InactivityMonitor = &pkgCoapService.InactivityMonitor{
Timeout: time.Second * 20,
}
cfg.APIs.COAP.BlockwiseTransfer.Enabled = config.DPS_UDP_ENABLED
cfg.APIs.COAP.BlockwiseTransfer.SZX = "1024"
cfg.APIs.HTTP = MakeHTTPConfig()
tlsServerCfg := config.MakeTLSServerConfig()
cfg.APIs.COAP.TLS.Embedded.CertFile = tlsServerCfg.CertFile
cfg.APIs.COAP.TLS.Embedded.KeyFile = tlsServerCfg.KeyFile
cfg.APIs = MakeAPIsConfig()
cfg.Clients.Storage = MakeStorageConfig()
cfg.Clients.OpenTelemetryCollector = pkgHttp.OpenTelemetryCollectorConfig{
Config: config.MakeOpenTelemetryCollectorClient(),
}

cfg.EnrollmentGroups = append(cfg.EnrollmentGroups, MakeEnrollmentGroup())
err := cfg.Validate()
require.NoError(t, err)
Expand Down Expand Up @@ -262,21 +271,21 @@ func New(t *testing.T, cfg service.Config, opts ...service.Option) func() {
return NewWithContext(context.Background(), t, cfg, opts...)
}

func checkForClosedSockets(t require.TestingT, cfg service.Config) {
sockets := make(hubTest.ListenSockets, 0, len(cfg.APIs.COAP.Protocols)+1)
for _, protocol := range cfg.APIs.COAP.Protocols {
func checkForClosedSockets(cfg service.APIsConfig) error {
sockets := make(hubTest.ListenSockets, 0, len(cfg.COAP.Protocols)+1)
for _, protocol := range cfg.COAP.Protocols {
sockets = append(sockets, hubTest.ListenSocket{
Network: string(protocol),
Address: cfg.APIs.COAP.Addr,
Address: cfg.COAP.Addr,
})
}
if cfg.APIs.HTTP.Enabled {
if cfg.HTTP.Enabled {
sockets = append(sockets, hubTest.ListenSocket{
Network: "tcp",
Address: cfg.APIs.HTTP.Config.Connection.Addr,
Address: cfg.HTTP.Config.Connection.Addr,
})
}
sockets.CheckForClosedSockets(t)
return sockets.CheckForClosedSockets()
}

// New creates test dps-gateway.
Expand All @@ -302,9 +311,10 @@ func NewWithContext(ctx context.Context, t *testing.T, cfg service.Config, opts
err = fileWatcher.Close()
require.NoError(t, err)

checkForClosedSockets(t, cfg)
err = checkForClosedSockets(cfg.APIs)
require.NoError(t, err)
// wait for all connections to be closed
time.Sleep(time.Millisecond * 500)
time.Sleep(time.Second)
}
}

Expand Down
11 changes: 6 additions & 5 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,6 @@ func (ls *ListenSocket) IsClosed() (bool, error) {
}
c, err := net.ListenUDP(ls.Network, addr)
if err != nil {
fmt.Printf("ListenUDP error: %v\n", err)
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()
Expand All @@ -1028,7 +1027,6 @@ func (ls *ListenSocket) IsClosed() (bool, error) {
}
c, err := net.ListenTCP(ls.Network, addr)
if err != nil {
fmt.Printf("ListenTCP error: %v\n", err)
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()
Expand All @@ -1040,7 +1038,7 @@ func (ls *ListenSocket) IsClosed() (bool, error) {

type ListenSockets []ListenSocket

func (ls ListenSockets) CheckForClosedSockets(t require.TestingT) {
func (ls ListenSockets) CheckForClosedSockets() error {
// wait for all sockets to be closed - max 3 minutes = 900*200
socketClosed := make([]bool, len(ls))
for j := 0; j < 900; j++ {
Expand All @@ -1050,16 +1048,19 @@ func (ls ListenSockets) CheckForClosedSockets(t require.TestingT) {
continue
}
closed, err := socket.IsClosed()
require.NoError(t, err)
if err != nil {
return err
}
socketClosed[i] = closed
if socketClosed[i] {
continue
}
allClosed = false
}
if allClosed {
break
return nil
}
time.Sleep(time.Millisecond * 200)
}
return errors.New("ports not closed")
}

0 comments on commit 34f74f3

Please sign in to comment.