Skip to content

Commit

Permalink
feat: add tcp server run method
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj committed Sep 5, 2024
1 parent 6009623 commit 3eb0878
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/go_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: setup OpenGemini
uses: shoothzj/setup-opengemini-action@main
- name: Run coverage
run: go test ./... -coverpkg=./opengemini/... -race -coverprofile=coverage.out -covermode=atomic
run: go test ./... -coverpkg=./opcua/... -race -coverprofile=coverage.out -covermode=atomic
8 changes: 8 additions & 0 deletions go.mod
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
)
10 changes: 10 additions & 0 deletions go.sum
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=
49 changes: 48 additions & 1 deletion opcua/server.go
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)
}
24 changes: 24 additions & 0 deletions opcua/server_test.go
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")
}

0 comments on commit 3eb0878

Please sign in to comment.