Skip to content

Commit 5eae5c3

Browse files
r-luyckxkonimarti
authored andcommitted
fixed a runtime error in fix(), added TestTags & TestAutomationItemsClose
1 parent 3a9d37d commit 5eae5c3

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

connection_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ func TestGetTags(t *testing.T) {
142142
}
143143
}
144144

145+
func TestTags(t *testing.T) {
146+
var want []string
147+
client := &opcConnectionImpl{}
148+
tags := client.Tags()
149+
if !reflect.DeepEqual(tags, want) {
150+
fmt.Printf("actual: %+v\n", tags)
151+
fmt.Printf("Want: %+v\n", want)
152+
t.Error("Tags() should return a empty array of strings")
153+
}
154+
}
155+
156+
func TestAutomationItemsClose(t *testing.T) {
157+
conn := &opcConnectionImpl{}
158+
conn.AutomationItems.Close()
159+
}
160+
145161
func TestOpcRead(t *testing.T) {
146162
client, _ := NewConnection(
147163
"Graybox.Simulator",

connection_windows.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,13 @@ func (ai *AutomationItems) writeToOpc(opcitem *ole.IDispatch, value interface{})
319319

320320
//Close closes the OLE objects in AutomationItems.
321321
func (ai *AutomationItems) Close() {
322-
for key, opcitem := range ai.items {
323-
opcitem.Release()
324-
delete(ai.items, key)
322+
if ai != nil {
323+
for key, opcitem := range ai.items {
324+
opcitem.Release()
325+
delete(ai.items, key)
326+
}
327+
ai.addItemObject.Release()
325328
}
326-
ai.addItemObject.Release()
327329
}
328330

329331
//NewAutomationItems returns a new AutomationItems instance.
@@ -393,10 +395,13 @@ func (conn *opcConnectionImpl) Read() map[string]Item {
393395
//Tags returns the currently active tags
394396
func (conn *opcConnectionImpl) Tags() []string {
395397
var tags []string
396-
for tag, _ := range conn.AutomationItems.items {
397-
tags = append(tags, tag)
398+
if conn.AutomationItems != nil {
399+
for tag, _ := range conn.AutomationItems.items {
400+
tags = append(tags, tag)
401+
}
398402
}
399403
return tags
404+
400405
}
401406

402407
//fix tries to reconnect if connection is lost by creating a new connection

0 commit comments

Comments
 (0)