diff --git a/opcua/client.go b/opcua/client.go index c4bf071..984b4ec 100644 --- a/opcua/client.go +++ b/opcua/client.go @@ -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, @@ -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() diff --git a/opcua/client_test.go b/opcua/client_test.go new file mode 100644 index 0000000..1a072be --- /dev/null +++ b/opcua/client_test.go @@ -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() +}