Skip to content

Commit

Permalink
opcua: add FindNamespace and tests to client
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Jan 14, 2022
1 parent 2486dac commit c36cc4c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,21 @@ func (c *Client) NamespaceArray() ([]string, error) {
return ns, nil
}

// FindNamespace returns the id of the namespace with the given name.
func (c *Client) FindNamespace(name string) (uint16, error) {
stats.Client().Add("FindNamespace", 1)
nsa, err := c.NamespaceArray()
if err != nil {
return 0, err
}
for i, ns := range nsa {
if ns == name {
return uint16(i), nil
}
}
return 0, errors.Errorf("namespace not found. name=%s", name)
}

// UpdateNamespaces updates the list of cached namespaces from the server.
func (c *Client) UpdateNamespaces() error {
stats.Client().Add("UpdateNamespaces", 1)
Expand Down
52 changes: 52 additions & 0 deletions uatest/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build integration
// +build integration

package uatest

import (
"context"
"testing"

"github.com/pascaldekloe/goe/verify"

"github.com/gopcua/opcua"
)

func TestNamespace(t *testing.T) {
srv := NewServer("rw_server.py")
defer srv.Close()

c := opcua.NewClient(srv.Endpoint, srv.Opts...)
if err := c.Connect(context.Background()); err != nil {
t.Fatal(err)
}
defer c.Close()

t.Run("NamespaceArray", func(t *testing.T) {
got, err := c.NamespaceArray()
if err != nil {
t.Fatal(err)
}
want := []string{
"http://opcfoundation.org/UA/",
"urn:freeopcua:python:server",
"http://gopcua.com/",
}
verify.Values(t, "", got, want)
})
t.Run("FindNamespace", func(t *testing.T) {
ns, err := c.FindNamespace("http://gopcua.com/")
if err != nil {
t.Fatal(err)
}
if got, want := ns, uint16(2); got != want {
t.Fatalf("got namespace id %d want %d", got, want)
}
})
t.Run("UpdateNamespaces", func(t *testing.T) {
err := c.UpdateNamespaces()
if err != nil {
t.Fatal(err)
}
})
}

0 comments on commit c36cc4c

Please sign in to comment.