Skip to content

Commit

Permalink
require: fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Dec 10, 2024
1 parent da16017 commit 1a50af9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 73 deletions.
30 changes: 9 additions & 21 deletions tests/go/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ func TestRead(t *testing.T) {
time.Sleep(2 * time.Second)

c, err := opcua.NewClient("opc.tcp://localhost:4840", opcua.SecurityMode(ua.MessageSecurityModeNone))
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 All @@ -67,15 +65,9 @@ func testRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{},
},
TimestampsToReturn: ua.TimestampsToReturnBoth,
})
if err != nil {
t.Fatalf("Read failed: %s", err)
}
if resp.Results[0].Status != ua.StatusOK {
t.Fatalf("Status not OK: %v", resp.Results[0].Status)
}
if got, want := resp.Results[0].Value.Value(), v; !require.Equal(t, want, got) {
t.Fail()
}
require.NoError(t, err, "Read failed")
require.Equal(t, ua.StatusOK, resp.Results[0].Status, "Status not OK")
require.Equal(t, v, resp.Results[0].Value.Value(), "Results[0].Value not equal")
}

func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{}, id *ua.NodeID) {
Expand All @@ -84,9 +76,7 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
resp, err := c.RegisterNodes(ctx, &ua.RegisterNodesRequest{
NodesToRegister: []*ua.NodeID{id},
})
if err != nil {
t.Fatalf("RegisterNodes failed: %s", err)
}
require.NoError(t, err, "RegisterNodes failed")

testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
Expand All @@ -97,7 +87,5 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
_, err = c.UnregisterNodes(ctx, &ua.UnregisterNodesRequest{
NodesToUnregister: []*ua.NodeID{id},
})
if err != nil {
t.Fatalf("UnregisterNodes failed: %s", err)
}
require.NoError(t, err, "UnregisterNodes failed")
}
22 changes: 7 additions & 15 deletions tests/python/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,18 @@ func TestCallMethod(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 {
t.Run(tt.req.ObjectID.String(), func(t *testing.T) {
resp, err := c.Call(ctx, tt.req)
if err != nil {
t.Fatal(err)
}
if got, want := resp.StatusCode, ua.StatusOK; got != want {
t.Fatalf("got status %v want %v", got, want)
}
if got, want := resp.OutputArguments, tt.out; !require.Equal(t, want, got) {
t.Fail()
}
require.NoError(t, err, "Call failed")
require.Equal(t, ua.StatusOK, resp.StatusCode, "StatusCode not equal")
require.Equal(t, tt.out, resp.OutputArguments, "OuptutArgs not equal")
})
}
}
30 changes: 9 additions & 21 deletions tests/python/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ func TestRead(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 All @@ -62,15 +60,9 @@ func testRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{},
},
TimestampsToReturn: ua.TimestampsToReturnBoth,
})
if err != nil {
t.Fatalf("Read failed: %s", err)
}
if resp.Results[0].Status != ua.StatusOK {
t.Fatalf("Status not OK: %v", resp.Results[0].Status)
}
if got, want := resp.Results[0].Value.Value(), v; !require.Equal(t, want, got) {
t.Fail()
}
require.NoError(t, err, "Read failed")
require.Equal(t, ua.StatusOK, resp.Results[0].Status, "Status not OK")
require.Equal(t, v, resp.Results[0].Value.Value(), "Results[0].Value not equal")
}

func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{}, id *ua.NodeID) {
Expand All @@ -79,9 +71,7 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
resp, err := c.RegisterNodes(ctx, &ua.RegisterNodesRequest{
NodesToRegister: []*ua.NodeID{id},
})
if err != nil {
t.Fatalf("RegisterNodes failed: %s", err)
}
require.NoError(t, err, "RegisterNodes failed")

testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
Expand All @@ -92,7 +82,5 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
_, err = c.UnregisterNodes(ctx, &ua.UnregisterNodesRequest{
NodesToUnregister: []*ua.NodeID{id},
})
if err != nil {
t.Fatalf("UnregisterNodes failed: %s", err)
}
require.NoError(t, err, "UnregisterNodes failed")
}
24 changes: 8 additions & 16 deletions tests/python/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ func TestStats(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)
}
c.Close(ctx)
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
c.Close(ctx) // close immediately

want := map[string]*expvar.Int{
"Dial": newExpVarInt(1),
Expand All @@ -54,20 +52,14 @@ func TestStats(t *testing.T) {
got := map[string]expvar.Var{}
stats.Client().Do(func(kv expvar.KeyValue) { got[kv.Key] = kv.Value })
for k := range got {
if _, ok := want[k]; !ok {
t.Fatalf("got unexpected key %q", k)
}
require.Contains(t, want, k, "got unexpected key %q", k)
}
for k := range want {
if _, ok := got[k]; !ok {
t.Fatalf("missing expected key %q", k)
}
require.Contains(t, got, k, "missing expected key %q", k)
}

for k, ev := range want {
v := stats.Client().Get(k)
if !require.Equal(t, ev, v) {
t.Errorf("got %s for %q, want %s", v.String(), k, ev.String())
}
require.Equal(t, ev, v, "got %s for %q, want %s", v.String(), k, ev.String())
}
}

0 comments on commit 1a50af9

Please sign in to comment.