forked from open-strateos/opcua_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
99 lines (84 loc) · 2.41 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"testing"
"github.com/gopcua/opcua/monitor"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/assert"
)
/**
* mockHandler implements the MsgHandler interface
*/
type mockHandler struct {
called bool // set if the Handle() func has been called
}
func (th *mockHandler) Handle(val ua.Variant) error {
th.called = true
return nil
}
func (th *mockHandler) FloatValue(val ua.Variant) (float64, error) {
return 0.0, nil
}
func makeTestMessage(nodeID *ua.NodeID) monitor.DataChangeMessage {
return monitor.DataChangeMessage{
NodeID: nodeID,
DataValue: &ua.DataValue{
Value: ua.MustVariant(0.0),
},
}
}
// Excercise the handleMessage() function
// Ensure that it dispatches messages to handlers as expected
func TestHandleMessage(t *testing.T) {
nodeID1 := ua.NewStringNodeID(1, "foo")
nodeID2 := ua.NewStringNodeID(1, "bar")
nodeName1 := nodeID1.String()
nodeName2 := nodeID2.String()
handlerMap := make(HandlerMap)
for i := 0; i < 3; i++ {
mapRecord := handlerMapRecord{
config: NodeConfig{NodeName: nodeName1, MetricName: "whatever"},
handler: &mockHandler{},
}
handlerMap[nodeName1] = append(handlerMap[nodeName1], mapRecord)
}
mapRecord2 := handlerMapRecord{
config: NodeConfig{NodeName: nodeName2, MetricName: "whatever"},
handler: &mockHandler{},
}
handlerMap[nodeName2] = append(handlerMap[nodeName2], mapRecord2)
assert.Equal(t, len(handlerMap[nodeName1]), 3)
assert.Equal(t, len(handlerMap[nodeName2]), 1)
// Handle a fake message addressed to nodeID1
msg := makeTestMessage(nodeID1)
handleMessage(&msg, handlerMap)
// All three nodeName1 handlers should have been called
for _, record := range handlerMap[nodeName1] {
handler := record.handler.(*mockHandler)
assert.True(t, handler.called)
}
// But not the nodeName2 handler
handler := handlerMap[nodeName2][0].handler.(*mockHandler)
assert.False(t, handler.called)
}
// Exercise the createMetrics() function
// Ensure that it creats the right sort of HandlerMap
func TestCreateMetrics(t *testing.T) {
nodeconfigs := []NodeConfig{
{
NodeName: "foo",
MetricName: "foo_level_blorbs",
},
{
NodeName: "bar",
MetricName: "bar_level_blorbs",
},
{
NodeName: "foo",
MetricName: "foo_rate_blarbs",
},
}
handlerMap := createMetrics(&nodeconfigs)
assert.Equal(t, len(handlerMap), 2)
assert.Equal(t, len(handlerMap["foo"]), 2)
assert.Equal(t, len(handlerMap["bar"]), 1)
}