Skip to content

Commit

Permalink
fix: client ctx is not initialized (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj authored Sep 23, 2024
1 parent 88256b2 commit 287a74f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions opcua/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ func NewClient(config *ClientConfig) (*Client, error) {
config.BufferMax = 512 * 1024
}

ctx, cancel := context.WithCancel(context.Background())

client := &Client{
config: config,
logger: config.Logger,
Expand All @@ -185,6 +187,8 @@ func NewClient(config *ClientConfig) (*Client, error) {
eventsChan: make(chan *sendRequest, config.SendQueueSize),
pendingQueue: make(chan *sendRequest, config.PendingQueueSize),
buffer: buffer.NewBuffer(config.BufferMax),
ctx: ctx,
ctxCancel: cancel,
}
go func() {
client.read()
Expand Down
35 changes: 35 additions & 0 deletions opcua/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package opcua

import (
"github.com/shoothzj/gox/netx"
"github.com/stretchr/testify/require"
"log/slog"
"testing"
)

func TestClientConnect(t *testing.T) {
logger := slog.Default()

serverConfig := &ServerConfig{
Host: "localhost",
Port: 0,
Logger: logger,
}
server := NewServer(serverConfig)

port, err := server.Run()
require.NoError(t, err)
defer server.Close()

clientConfig := &ClientConfig{
Address: netx.Address{
Host: "127.0.0.1",
Port: port,
},
Logger: logger,
}
client, err := NewClient(clientConfig)
require.NoError(t, err)

client.close()
}

0 comments on commit 287a74f

Please sign in to comment.