-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
- Loading branch information
Showing
5 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module opcua-go | ||
|
||
go 1.20 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,59 @@ | ||
package opcua | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type ServerConfig struct { | ||
Host string | ||
Port int | ||
} | ||
|
||
func (s *ServerConfig) addr() string { | ||
return fmt.Sprintf("%s:%d", s.Host, s.Port) | ||
} | ||
|
||
type Server struct { | ||
config *ServerConfig | ||
listener net.Listener | ||
} | ||
|
||
func NewServer(config *ServerConfig) *Server { | ||
server := &Server{} | ||
server := &Server{ | ||
config: config, | ||
} | ||
return server | ||
} | ||
|
||
func (s *Server) Run() (int, error) { | ||
listener, err := net.Listen("tcp", s.config.addr()) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to listen on %s: %w", s.config.addr(), err) | ||
} | ||
|
||
actualAddr, ok := listener.Addr().(*net.TCPAddr) | ||
if !ok { | ||
return 0, fmt.Errorf("failed to get TCP address from listener") | ||
} | ||
|
||
if s.config.Port == 0 { | ||
s.config.Port = actualAddr.Port | ||
} | ||
|
||
s.listener = listener | ||
|
||
return actualAddr.Port, nil | ||
} | ||
|
||
func (s *Server) Close() error { | ||
if s.listener == nil { | ||
return nil | ||
} | ||
err := s.listener.Close() | ||
s.listener = nil | ||
if err == nil { | ||
return nil | ||
} | ||
return fmt.Errorf("failed to close listener: %w", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package opcua | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestStartWithZeroPort(t *testing.T) { | ||
config := &ServerConfig{ | ||
Host: "localhost", | ||
Port: 0, | ||
} | ||
|
||
server := NewServer(config) | ||
|
||
port, err := server.Run() | ||
require.NoError(t, err, "Server should start without error") | ||
|
||
assert.Greater(t, port, 0, "Expected a valid port to be assigned, but got %d", port) | ||
|
||
err = server.Close() | ||
assert.NoError(t, err, "Server should close without error") | ||
} |