Skip to content

Commit 2311f78

Browse files
committed
refactor: enhance OPC UA server startup and cleanup in tests
1 parent cbe279c commit 2311f78

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

mods/tql/fm_script_test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package tql_test
33
import (
44
"context"
55
"fmt"
6-
"log"
6+
"net"
77
"testing"
88

99
"github.com/stretchr/testify/require"
@@ -646,13 +646,15 @@ func TestScriptException(t *testing.T) {
646646
}
647647

648648
func TestScriptOPCUA(t *testing.T) {
649-
svr := startOPCUAServer()
650-
defer svr.Close()
649+
svr, endpoint := startOPCUAServer(t)
650+
t.Cleanup(func() {
651+
require.NoError(t, svr.Close())
652+
})
651653

652654
tests := []TqlTestCase{
653655
{
654656
Name: "js-opcua-read",
655-
Script: `
657+
Script: fmt.Sprintf(`
656658
SCRIPT("js", {
657659
ua = require("opcua");
658660
nodes = [
@@ -661,15 +663,15 @@ func TestScriptOPCUA(t *testing.T) {
661663
"ns=1;s=ro_int32", // int32(5)
662664
"ns=1;s=rw_int32", // int32(5)
663665
];
664-
client = new ua.Client({ endpoint: "opc.tcp://localhost:4840" });
666+
client = new ua.Client({ endpoint: %q });
665667
vs = client.read({ nodes: nodes, timestampsToReturn: ua.TimestampsToReturn.Both});
666668
vs.forEach((v, idx) => {
667669
$.yield(nodes[idx], v.status, v.value, v.type);
668670
})
669671
client.close();
670672
})
671673
CSV(timeformat('default'), tz('UTC'))
672-
`,
674+
`, endpoint),
673675
ExpectCSV: []string{
674676
"ns=1;s=ro_bool,0,true,Boolean",
675677
"ns=1;s=rw_bool,0,true,Boolean",
@@ -679,7 +681,7 @@ func TestScriptOPCUA(t *testing.T) {
679681
},
680682
{
681683
Name: "js-opcua-read-perms",
682-
Script: `
684+
Script: fmt.Sprintf(`
683685
SCRIPT("js", {
684686
ua = require("opcua");
685687
nodes = [
@@ -688,15 +690,15 @@ func TestScriptOPCUA(t *testing.T) {
688690
"ns=1;s=ReadOnlyVariable", // ua.StatusOK, 9.87
689691
"ns=1;s=NoAccessVariable", // ua.StatusBadUserAccessDenied
690692
];
691-
client = new ua.Client({ endpoint: "opc.tcp://localhost:4840" });
693+
client = new ua.Client({ endpoint: %q });
692694
vs = client.read({ nodes: nodes});
693695
vs.forEach((v, idx) => {
694696
$.yield(nodes[idx], v.statusCode, v.value, v.type);
695697
})
696698
client.close();
697699
})
698700
CSV()
699-
`,
701+
`, endpoint),
700702
ExpectCSV: []string{
701703
"ns=1;s=NoPermVariable,StatusGood,742,Int32",
702704
"ns=1;s=ReadWriteVariable,StatusGood,12.34,Double",
@@ -712,9 +714,12 @@ func TestScriptOPCUA(t *testing.T) {
712714
}
713715
}
714716

715-
func startOPCUAServer() *opc_server.Server {
717+
func startOPCUAServer(t *testing.T) (*opc_server.Server, string) {
718+
t.Helper()
719+
716720
var opts []opc_server.Option
717-
port := 4840
721+
port := freeOPCUAPort(t)
722+
endpoint := fmt.Sprintf("opc.tcp://127.0.0.1:%d", port)
718723

719724
opts = append(opts,
720725
opc_server.EnableSecurity("None", ua.MessageSecurityModeNone),
@@ -738,7 +743,7 @@ func startOPCUAServer() *opc_server.Server {
738743
)
739744

740745
opts = append(opts,
741-
opc_server.EndPoint("localhost", port),
746+
opc_server.EndPoint("127.0.0.1", port),
742747
)
743748

744749
s := opc_server.New(opts...)
@@ -832,7 +837,16 @@ func startOPCUAServer() *opc_server.Server {
832837
// Create a new node namespace. You can add namespaces before or after starting the server.
833838
// Start the server
834839
if err := s.Start(context.Background()); err != nil {
835-
log.Fatalf("Error starting server, exiting: %s", err)
840+
t.Fatalf("failed to start OPC UA test server at %s: %s", endpoint, err)
836841
}
837-
return s
842+
return s, endpoint
843+
}
844+
845+
func freeOPCUAPort(t *testing.T) int {
846+
t.Helper()
847+
848+
listener, err := net.Listen("tcp", "127.0.0.1:0")
849+
require.NoError(t, err)
850+
defer listener.Close()
851+
return listener.Addr().(*net.TCPAddr).Port
838852
}

0 commit comments

Comments
 (0)