Skip to content

Commit

Permalink
release 2.5: update test
Browse files Browse the repository at this point in the history
guidfort committed Dec 3, 2024
1 parent 6104b05 commit 85eea74
Showing 4 changed files with 364 additions and 8 deletions.
275 changes: 275 additions & 0 deletions test/datalayer/provider_subscription_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/*
* SPDX-FileCopyrightText: Bosch Rexroth AG
*
* SPDX-License-Identifier: MIT
*/

package datalayer_test

import (
"fmt"
"testing"
"time"

"github.com/boschrexroth/ctrlx-datalayer-golang/v2/pkg/datalayer"
"github.com/stretchr/testify/suite"
)

const (
addressbasesub = "test-go-datalayer-provider-sub/folder/"
addressFolderSub = addressbasesub + "**"
addressSubInt1 = addressbasesub + "int1"
)

// helper for 'local' provider
type testProviderSub struct {
system *datalayer.System
provider *datalayer.Provider
node *newNodeDataFolderSub
}

// test suite client
type ProviderSubTestSuite struct {
suite.Suite
tp *testProviderSub
}

// test entry function
func TestProviderSubTestSuite(t *testing.T) {
suite.Run(t, new(ProviderSubTestSuite))
}

func (suite *ProviderSubTestSuite) TestProviderClientSubscription() {
client := suite.initClient()
defer datalayer.DeleteClient(client)

suite.True(client.IsConnected())

done := make(chan bool)

onSubscribe := func(result datalayer.Result, items map[string]datalayer.Variant) {
suite.Equal(datalayer.Result(0), result)
suite.Equal(len(items), 1)
done <- true
}

s, r := client.CreateSubscription("myProviderSub", datalayer.DefaultSubscriptionProperties(), onSubscribe)
suite.Equal(datalayer.Result(0), r)
suite.NotNil(s)

r = s.Subscribe(addressSubInt1)
suite.Equal(datalayer.Result(0), r)

suite.Equal(s.Id(), "myProviderSub")
suite.True(len(s.Addresses()) == 1)

select {
case <-done:
case <-time.After(5 * time.Second):
suite.Fail("callback timeout")
}

suite.NotPanics(func() { client.DeleteSubscription(s) })
close(done)

}

// create 'local' provider
func (suite *ProviderSubTestSuite) initProvider() {
suite.tp = &testProviderSub{}
suite.tp.create()
}

// close 'local' provider
func (suite *ProviderSubTestSuite) closeProvider() {
if suite.tp == nil {
return
}
suite.tp.close()
suite.tp = nil
}

// this function executes bevor all tests executed
func (suite *ProviderSubTestSuite) SetupSuite() {

suite.initProvider()
fmt.Println(">>> From SetupSuite")
}

// this function executes after all tests executed
func (suite *ProviderSubTestSuite) TearDownSuite() {
suite.closeProvider()
fmt.Println(">>> From TearDownSuite")
}

// create a 'local' client
func (suite *ProviderSubTestSuite) initClient() *datalayer.Client {
system := suite.tp.system
//system.Start(true)

client := system.Factory().CreateClient("ipc://")
client.SetTimeout(datalayer.TimeoutSettingPing, ctrlxClientTimeout())
return client
}

// create a 'local' provider with provider nodes
func (p *testProviderSub) create() {
p.system = datalayer.NewSystem("")
p.system.Start(true)
p.provider = p.system.Factory().CreateProvider("ipc://")
r := p.provider.Start()
fmt.Println("Start Provider: ", r)
if r != datalayer.ResultOk {
return
}
p.createNodes()
}

// create folder provider nodes and folder manage different subnodes
func (p *testProviderSub) createNodes() {
ndf := &newNodeDataFolderSub{
name: addressFolderSub,
node: datalayer.NewProviderNodeSub(),
Subscriptions: make(map[uint64]*datalayer.ProviderSubscription),
}
p.node = ndf
go startnodeDataSubHandler(*ndf)
r := p.provider.RegisterNode(ndf.Name(), ndf.Node())
if r != datalayer.ResultOk {
fmt.Println("ERROR Registering node ", ndf.Name(), " failed: ", r)
}
}

// close provider
func (p *testProviderSub) closeProvider() {
p.provider.UnregisterNode(addressFolderSub)
datalayer.DeleteProviderNode(p.node.node)
p.provider.Stop()
datalayer.DeleteProvider(p.provider)
}

// close runtime
func (p *testProviderSub) close() {
p.closeProvider()
p.system.Stop(true)
datalayer.DeleteSystem(p.system)
}

type newNodeDataFolderSub struct {
name string
node *datalayer.ProviderNode

Subscriptions map[uint64]*datalayer.ProviderSubscription
}

func (n *newNodeDataFolderSub) Node() *datalayer.ProviderNode {
return n.node
}

func (n *newNodeDataFolderSub) Name() string {
return n.name
}

// handler for provider node
func startnodeDataSubHandler(d newNodeDataFolderSub) {
for {
if d.Node() == nil {
return
}
select {
case event, ok := <-d.Node().Channels().OnCreate:
if !ok {
return
}
fmt.Println("event: oncreate: ", d.Name(), event.Address)
event.Callback(datalayer.ResultOk, nil)

case event, ok := <-d.Node().Channels().OnRemove:
if !ok {
return
}

fmt.Println("event: OnRemove: ", d.Name())
event.Callback(datalayer.ResultOk, nil)

case event, ok := <-d.Node().Channels().OnBrowse:
func() {
if !ok {
return
}

newData := datalayer.NewVariant()
defer datalayer.DeleteVariant(newData)
newData.SetArrayString([]string{d.Name()})
fmt.Println("event: OnBrowse: ", d.Name())
event.Callback(datalayer.Result(0), newData)
}()

case event, ok := <-d.Node().Channels().OnRead:
func() {
if !ok {
return
}

newData := datalayer.NewVariant()
defer datalayer.DeleteVariant(newData)
newData.SetInt32(123)
fmt.Println("event: OnRead: ", d.Name(), event.Address)
event.Callback(datalayer.ResultOk, newData)
}()

case event, ok := <-d.Node().Channels().OnWrite:
if !ok {
return
}

fmt.Println("event: OnWrite: ", d.Name())
event.Callback(datalayer.ResultOk, event.Data)

case event, ok := <-d.Node().Channels().OnMetadata:
func() {
if !ok {
return
}

fmt.Println("event: OnMetadata: ", d.Name())

event.Callback(datalayer.ResultOk, nil)
}()

case event, ok := <-d.Node().Channels().OnSubscribe:
func() {
if !ok {
return
}

fmt.Println("event: OnSubscribe: ", d.Name(), event.Address, event.Subsciption.GetUniqueId())
fmt.Println(" Props: PublishInterval:", event.Subsciption.GetProps().PublishInterval())
_, ok := d.Subscriptions[event.Subsciption.GetUniqueId()]
if ok {
return
}
d.Subscriptions[event.Subsciption.GetUniqueId()] = event.Subsciption
n := datalayer.NewNotifyItemPublish(event.Address)
l := []*datalayer.NotifyItemPublish{n}
event.Subsciption.Publish(datalayer.ResultOk, l)
datalayer.DeleteNotifyItemsPublish(l)
}()
case event, ok := <-d.Node().Channels().OnUnsubscribe:
func() {
if !ok {
return
}

fmt.Println("event: OnUnsubscribe: ", d.Name(), event.Address, event.Subsciption.GetUniqueId())
if len(event.Subsciption.GetNodes()) != 0 {
return
}
delete(d.Subscriptions, event.Subsciption.GetUniqueId())
}()
case <-d.Node().Channels().Done:
fmt.Println("event: Done: ", d.Name())
return
}
}
}
64 changes: 64 additions & 0 deletions test/datalayer/provider_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-FileCopyrightText: Bosch Rexroth AG
*
* SPDX-License-Identifier: MIT
*/

package datalayer_test

import (
"testing"

"github.com/boschrexroth/ctrlx-datalayer-golang/v2/pkg/datalayer"
"github.com/stretchr/testify/suite"
)

// test suite client
type ProviderTestSuite struct {
ClientTestSuite
}

// test entry function
func TestProviderTestSuite(t *testing.T) {
suite.Run(t, new(ProviderTestSuite))
}

func (suite *ProviderTestSuite) TestGetRegisteredType() {
r, d := suite.tp.provider.GetRegisteredType("identity") //see test "c_provider_test.cpp"

defer datalayer.DeleteVariant(d)
suite.Equal(datalayer.ResultOk, r)
suite.NotNil(d)
suite.Equal(d.GetType(), datalayer.VariantTypeArrayUint8)

d.SetBool8(true)
r = suite.tp.provider.RegisterTypeVariant("types/GetRegisteredType/test1", d)
suite.Equal(datalayer.ResultOk, r)

r, d1 := suite.tp.provider.GetRegisteredType("types/GetRegisteredType/test1") //see test "c_provider_test.cpp"
defer datalayer.DeleteVariant(d1)
suite.Equal(datalayer.ResultOk, r)
suite.NotNil(d1)
suite.Equal(d1.GetType(), datalayer.VariantTypeBool8)
}

func (suite *ProviderTestSuite) TestGetRegisteredNodePaths() {
r, d := suite.tp.provider.GetRegisteredNodePaths()
suite.Equal(datalayer.ResultOk, r)
suite.NotNil(d)
suite.True(len(d) >= 2)
}

func (suite *ProviderTestSuite) TestGetRejectedNodePaths() {
r, d := suite.tp.provider.GetRejectedNodePaths()
suite.Equal(datalayer.ResultOk, r)
suite.NotNil(d)
suite.True(len(d) >= 0)
}

func (suite *ProviderTestSuite) TestPublishEvent() {
data := datalayer.NewVariant()
defer datalayer.DeleteVariant(data)
r := suite.tp.provider.PublishEvent(data, data)
suite.Equal(datalayer.ResultTypeMismatch, r)
}
17 changes: 17 additions & 0 deletions test/datalayer/provider_test.go
Original file line number Diff line number Diff line change
@@ -135,6 +135,23 @@ func TestProviderRegisterType(t *testing.T) {
a.Equal(t, datalayer.Result(0), provider.UnregisterType("types/godata/test"))
}

func TestGetRegisteredType(t *testing.T) {
if providerAddress == "" {
t.Skip("ctrlX device does not exist")
}
system, provider := initProvider()
defer datalayer.DeleteSystem(system)
defer datalayer.DeleteProvider(provider)

a.Equal(t, datalayer.Result(0), provider.Start())
a.Equal(t, true, provider.IsConnected())

r, d := provider.GetRegisteredType("identity")
a.Equal(t, datalayer.ResultOk, r)
a.NotNil(t, d)
a.Equal(t, d.GetType(), datalayer.VariantTypeArrayUint8)
}

var goFloat float32 = 0.369

func startTestNode(node *datalayer.ProviderNode) {
16 changes: 8 additions & 8 deletions test/datalayer/utils_test.go
Original file line number Diff line number Diff line change
@@ -105,14 +105,14 @@ func asyncTestTimeout() time.Duration {
}

func TestClientAddressNoTests(t *testing.T) {
os.Setenv("CTRLX_ADDRESS", "-")
t.Setenv("CTRLX_ADDRESS", "-")
e := ctrlxAddress()
assert.Equal(t, e, "")
resetGlobalEnvs()
}

func TestClientAddressSetEnv(t *testing.T) {
os.Setenv("CTRLX_ADDRESS", "10.0.2.2")
t.Setenv("CTRLX_ADDRESS", "10.0.2.2")
e := ctrlxAddress()
assert.True(t, e == "tcp://boschrexroth:boschrexroth@10.0.2.2" ||
e == "tcp://boschrexroth:boschrexroth@10.0.2.2?sslport=443" ||
@@ -121,36 +121,36 @@ func TestClientAddressSetEnv(t *testing.T) {
}

func TestClientAddressSetEnv2(t *testing.T) {
os.Setenv("CTRLX_ADDRESS", "10.0.2.2")
os.Setenv("CTRLX_SSL_PORT", "8443")
t.Setenv("CTRLX_ADDRESS", "10.0.2.2")
t.Setenv("CTRLX_SSL_PORT", "8443")
e := ctrlxAddress()
assert.Equal(t, e, "tcp://boschrexroth:boschrexroth@10.0.2.2?sslport=8443")
resetGlobalEnvs()
}

func TestClientTimeout(t *testing.T) {
os.Setenv("CTRLX_TIMEOUT", "-")
t.Setenv("CTRLX_TIMEOUT", "-")
e := ctrlxClientTimeout()
assert.Equal(t, e, uint(2000))
resetGlobalEnvs()
}

func TestClientTimeoutSetEnv(t *testing.T) {
os.Setenv("CTRLX_TIMEOUT", "4000")
t.Setenv("CTRLX_TIMEOUT", "4000")
e := ctrlxClientTimeout()
assert.Equal(t, e, uint(4000))
resetGlobalEnvs()
}

func TestAsyncWaitTimeout(t *testing.T) {
os.Setenv("CTRLX_TIMEOUT", "-")
t.Setenv("CTRLX_TIMEOUT", "-")
e := asyncTestTimeout()
assert.Equal(t, e, 5*time.Second)
resetGlobalEnvs()
}

func TestAsyncWaitTimeoutSetEnv(t *testing.T) {
os.Setenv("CTRLX_TIMEOUT", "4000")
t.Setenv("CTRLX_TIMEOUT", "4000")
e := asyncTestTimeout()
assert.Equal(t, e, (4000/1000)*2*time.Second)
resetGlobalEnvs()

0 comments on commit 85eea74

Please sign in to comment.