Skip to content

Commit

Permalink
require: finish package tests/python
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Dec 17, 2024
1 parent e70172c commit dc150bb
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 88 deletions.
1 change: 0 additions & 1 deletion tests/python/method_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand Down
28 changes: 9 additions & 19 deletions tests/python/namespace_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand All @@ -18,19 +17,16 @@ func TestNamespace(t *testing.T) {
defer srv.Close()

c, err := opcua.NewClient(srv.Endpoint, srv.Opts...)
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

t.Run("NamespaceArray", func(t *testing.T) {
got, err := c.NamespaceArray(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NamespaceArray failed")

want := []string{
"http://opcfoundation.org/UA/",
"urn:freeopcua:python:server",
Expand All @@ -40,17 +36,11 @@ func TestNamespace(t *testing.T) {
})
t.Run("FindNamespace", func(t *testing.T) {
ns, err := c.FindNamespace(ctx, "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)
}
require.NoError(t, err, "FindNamespace failed")
require.Equal(t, uint16(2), ns, "namespace id not equal")
})
t.Run("UpdateNamespaces", func(t *testing.T) {
err := c.UpdateNamespaces(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "UpdateNamespaces failed")
})
}
1 change: 0 additions & 1 deletion tests/python/py_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand Down
1 change: 0 additions & 1 deletion tests/python/read_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand Down
24 changes: 8 additions & 16 deletions tests/python/read_unknow_node_id_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand All @@ -10,6 +9,7 @@ import (
"github.com/gopcua/opcua"
"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

// TestRead performs an integration test to read values
Expand All @@ -21,12 +21,10 @@ func TestReadUnknowNodeID(t *testing.T) {
defer srv.Close()

c, err := opcua.NewClient(srv.Endpoint, srv.Opts...)
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

// read node with unknown extension object
Expand All @@ -37,13 +35,9 @@ func TestReadUnknowNodeID(t *testing.T) {
{NodeID: nodeWithUnknownType},
},
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "Read failed")

if got, want := resp.Results[0].Status, ua.StatusBadDataTypeIDUnknown; got != want {
t.Errorf("got status %v want %v for a node with an unknown type", got, want)
}
require.Equal(t, ua.StatusBadDataTypeIDUnknown, resp.Results[0].Status, "got different status for a node with an unknown type")

// check that the connection is still usable by reading another node.
_, err = c.Read(ctx, &ua.ReadRequest{
Expand All @@ -53,7 +47,5 @@ func TestReadUnknowNodeID(t *testing.T) {
},
},
})
if err != nil {
t.Error(err)
}
require.NoError(t, err, "Read failed")
}
35 changes: 13 additions & 22 deletions tests/python/reconnection_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand All @@ -11,6 +10,7 @@ import (
"github.com/gopcua/opcua"
"github.com/gopcua/opcua/monitor"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -28,18 +28,14 @@ func TestAutoReconnection(t *testing.T) {
defer srv.Close()

c, err := opcua.NewClient(srv.Endpoint, srv.Opts...)
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

m, err := monitor.NewNodeMonitor(c)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewNodeMonitor failed")

ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down Expand Up @@ -92,17 +88,14 @@ func TestAutoReconnection(t *testing.T) {
ch,
currentTimeNodeID,
)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "ChanSubscribe failed")
defer sub.Unsubscribe(ctx)

for _, tt := range tests {
ok := t.Run(tt.name, func(t *testing.T) {

if msg := <-ch; msg.Error != nil {
t.Fatalf("No error expected for first value: %s", msg.Error)
}
msg := <-ch
require.NoError(t, msg.Error, "No error expected for first value")

downC := make(chan struct{}, 1)
dTimeout := time.NewTimer(disconnectTimeout)
Expand Down Expand Up @@ -132,7 +125,7 @@ func TestAutoReconnection(t *testing.T) {
select {
case <-dTimeout.C:
cancel()
t.Fatal("Timeout reached, the connection did not go down as expected")
require.Fail(t, "Timeout reached, the connection did not go down as expected")
case <-downC:
}

Expand All @@ -144,16 +137,14 @@ func TestAutoReconnection(t *testing.T) {
rTimeout := time.NewTimer(reconnectionTimeout)
select {
case <-rTimeout.C:
t.Fatal("Timeout reached, reconnection failed")
require.Fail(t, "Timeout reached, reconnection failed")
case msg := <-ch:
if err := msg.Error; err != nil {
t.Fatal(err)
}
require.NoError(t, msg.Error)
}
})

if !ok {
t.FailNow()
require.Fail(t)

Check failure on line 147 in tests/python/reconnection_test.go

View workflow job for this annotation

GitHub Actions / Test ubuntu-latest go1.22.x

not enough arguments in call to require.Fail

Check failure on line 147 in tests/python/reconnection_test.go

View workflow job for this annotation

GitHub Actions / Test ubuntu-latest go1.23.x

not enough arguments in call to require.Fail
}
}
}
1 change: 0 additions & 1 deletion tests/python/stats_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand Down
21 changes: 7 additions & 14 deletions tests/python/timeout_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand All @@ -11,6 +10,7 @@ import (

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/errors"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -22,18 +22,13 @@ const (

func TestClientTimeoutViaOptions(t *testing.T) {
c, err := opcua.NewClient(tcpNoRstTestServer, opcua.DialTimeout(forceTimeoutDuration))
if err != nil {
t.Fatal(err)
}

require.NoError(t, err, "NewClient failed")
connectAndValidate(t, c, context.Background(), forceTimeoutDuration)
}

func TestClientTimeoutViaContext(t *testing.T) {
c, err := opcua.NewClient(tcpNoRstTestServer)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

ctx, cancel := context.WithTimeout(context.Background(), forceTimeoutDuration)
defer cancel()
Expand All @@ -45,26 +40,24 @@ func connectAndValidate(t *testing.T, c *opcua.Client, ctx context.Context, d ti
start := time.Now()

err := c.Connect(ctx)
if err == nil {
t.Fatal("err should not be nil")
}
require.Error(t, err, "Connect should fail")

elapsed := time.Since(start)

var oe *net.OpError
switch {
case errors.As(err, &oe) && !oe.Timeout():
t.Fatalf("got %#v, wanted net.timeoutError", oe.Unwrap())
require.Fail(t, "got %#v, wanted net.timeoutError", oe.Unwrap())
case errors.As(err, &oe):
// ignore
default:
t.Fatalf("got %T, wanted %T", err, &net.OpError{})
require.Fail(t, "got %T, wanted %T", err, &net.OpError{})
}

pct := 0.05

if !within(elapsed, d, pct) {
t.Fatalf("took %s, expected %s +/- %v%%", elapsed, d, pct*100)
require.Fail(t, "took %s, expected %s +/- %v%%", elapsed, d, pct*100)
}
}

Expand Down
20 changes: 7 additions & 13 deletions tests/python/write_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

package uatest

Expand All @@ -9,6 +8,7 @@ import (

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

// TestWrite performs an integration test to first write
Expand All @@ -33,12 +33,10 @@ func TestWrite(t *testing.T) {
defer srv.Close()

c, err := opcua.NewClient(srv.Endpoint, srv.Opts...)
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

for _, tt := range tests {
Expand Down Expand Up @@ -70,10 +68,6 @@ func testWrite(t *testing.T, ctx context.Context, c *opcua.Client, status ua.Sta
t.Helper()

resp, err := c.Write(ctx, req)
if err != nil {
t.Fatalf("Write failed: %s", err)
}
if got, want := resp.Results[0], status; got != want {
t.Fatalf("got status %v want %v", got, want)
}
require.NoError(t, err, "Write failed")
require.Equal(t, status, resp.Results[0], "Write result not equal")
}

0 comments on commit dc150bb

Please sign in to comment.