You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2023/06/14 14:02:56 UTC
[plc4x] 02/02: fix(plc4go): Made the Tracer synchronized
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch chore/profinet-phase-3
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 62933b66767b1c93327f98b3ee4f2b9e4fc29a41
Author: Christofer Dutz <cd...@apache.org>
AuthorDate: Wed Jun 14 16:02:39 2023 +0200
fix(plc4go): Made the Tracer synchronized
---
plc4go/pkg/api/cache/PlcConnectionCache_test.go | 4 ++++
plc4go/spi/tracer/Tracer.go | 15 ++++++++++++++-
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/plc4go/pkg/api/cache/PlcConnectionCache_test.go b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
index b367d65e8e..fa49225a36 100644
--- a/plc4go/pkg/api/cache/PlcConnectionCache_test.go
+++ b/plc4go/pkg/api/cache/PlcConnectionCache_test.go
@@ -456,6 +456,8 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
t.Errorf("Expected %d connections in the cache but got %d", 0, len(cache.connections))
}
+ // In the connection string, we tell the driver to return an error with
+ // the given message on executing a ping operation.
connectionResultChan := cache.GetConnection("simulated://1.2.3.4:42?pingError=hurz&traceEnabled=true")
select {
case connectResult := <-connectionResultChan:
@@ -484,6 +486,8 @@ func TestPlcConnectionCache_ReturningConnectionWithPingError(t *testing.T) {
if traces[3].Operation+"-"+traces[3].Message != "ping-error: hurz" {
t.Errorf("Expected '%s' as fourth trace message, but got '%s'", "ping-error: hurz", traces[3])
}
+ } else {
+ t.Errorf("Expected a result, but got nil")
}
}
case <-time.After(20 * time.Second):
diff --git a/plc4go/spi/tracer/Tracer.go b/plc4go/spi/tracer/Tracer.go
index 29009c9a66..a2c026efd7 100644
--- a/plc4go/spi/tracer/Tracer.go
+++ b/plc4go/spi/tracer/Tracer.go
@@ -21,6 +21,7 @@ package tracer
import (
"fmt"
+ "sync"
"time"
"github.com/apache/plc4x/plc4go/spi/options"
@@ -64,6 +65,7 @@ func NewTracer(connectionId string, _options ...options.WithOption) Tracer {
type tracer struct {
connectionId string
traceEntries []TraceEntry
+ m sync.Mutex
log zerolog.Logger
}
@@ -77,14 +79,20 @@ func (t *tracer) SetConnectionId(connectionId string) {
}
func (t *tracer) ResetTraces() {
+ t.m.Lock()
t.traceEntries = []TraceEntry{}
+ t.m.Unlock()
}
func (t *tracer) GetTraces() []TraceEntry {
- return t.traceEntries
+ t.m.Lock()
+ entries := t.traceEntries
+ t.m.Unlock()
+ return entries
}
func (t *tracer) AddTrace(operation string, message string) {
+ t.m.Lock()
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
ConnectionId: t.connectionId,
@@ -92,9 +100,11 @@ func (t *tracer) AddTrace(operation string, message string) {
Operation: operation,
Message: message,
})
+ t.m.Unlock()
}
func (t *tracer) AddTransactionalStartTrace(operation string, message string) string {
+ t.m.Lock()
transactionId := utils.GenerateId(t.log, 4)
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
@@ -103,10 +113,12 @@ func (t *tracer) AddTransactionalStartTrace(operation string, message string) st
Operation: operation,
Message: message,
})
+ t.m.Unlock()
return transactionId
}
func (t *tracer) AddTransactionalTrace(transactionId string, operation string, message string) {
+ t.m.Lock()
t.traceEntries = append(t.traceEntries, TraceEntry{
Timestamp: time.Now(),
ConnectionId: t.connectionId,
@@ -114,6 +126,7 @@ func (t *tracer) AddTransactionalTrace(transactionId string, operation string, m
Operation: operation,
Message: message,
})
+ t.m.Unlock()
}
func (t *tracer) FilterTraces(traces []TraceEntry, connectionIdFilter string, transactionIdFilter string, operationFilter string, messageFilter string) []TraceEntry {