Skip to content

Commit 9f90bff

Browse files
committed
protection logger fix
1 parent f6cfe2f commit 9f90bff

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

backend/pkg/logger/logger.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
)
1111

1212
const (
13-
Name = "loggerHandler"
14-
HandlerName = "logger"
13+
Name = "loggerHandler"
14+
HandlerName = "logger"
15+
TimestampFormat = "01-Jan-2006_15-04-05.000"
1516
)
1617

17-
const TimestampFormat = "01-Jan-2006_15-04-05.000"
18-
1918
// Logger is a struct that implements the abstraction.Logger interface
2019
type Logger struct {
2120
// An atomic boolean is used in order to use CompareAndSwap in the Start and Stop methods

backend/pkg/logger/logger_test.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger/data"
99
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger/order"
1010
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger/protection"
11+
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger/state"
1112
dataPacketer "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/data"
1213
protectionPacketer "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/protection"
1314
"github.com/rs/zerolog"
@@ -23,17 +24,21 @@ func TestLogger(t *testing.T) {
2324
protectionSublogger := protection.NewLogger(map[abstraction.BoardId]string{
2425
0: "test",
2526
})
27+
stateSublogger := state.NewLogger()
2628
loggerHandler := logger.NewLogger(map[abstraction.LoggerName]abstraction.Logger{
2729
data.Name: dataSublogger,
2830
order.Name: orderSublogger,
2931
protection.Name: protectionSublogger,
32+
state.Name: stateSublogger,
3033
}, zerolog.New(os.Stdout).With().Timestamp().Logger())
3134

3235
if err := loggerHandler.Start(); err != nil {
3336
t.Error(err)
3437
}
3538

36-
timestamp := time.Now()
39+
timestamp := logger.Timestamp
40+
41+
time.Sleep(100 * time.Millisecond)
3742

3843
// Data
3944
dataPacket := dataPacketer.NewPacketWithValues(
@@ -44,6 +49,7 @@ func TestLogger(t *testing.T) {
4449
map[dataPacketer.ValueName]bool{
4550
"test": true,
4651
})
52+
dataPacketTime := time.Now()
4753
dataRecord := &data.Record{
4854
Packet: dataPacket,
4955
From: "test",
@@ -57,7 +63,7 @@ func TestLogger(t *testing.T) {
5763

5864
filename := path.Join(
5965
"logger/data",
60-
fmt.Sprintf("data_%s", timestamp.Format("2006_01_02_15_04_05")),
66+
fmt.Sprintf("%s", timestamp.Format(logger.TimestampFormat)),
6167
fmt.Sprintf("%s.csv", "test"),
6268
)
6369
file, err := os.Open(filename)
@@ -71,12 +77,13 @@ func TestLogger(t *testing.T) {
7177
t.Error(err)
7278
}
7379

74-
if output[0] != fmt.Sprint(timestamp.UnixMilli()) || output[1] != "test" || output[2] != "test" || output[3] != "true" {
75-
t.Errorf("dataErr: expected [test true], got %v", output)
80+
if output[0] != fmt.Sprint(dataPacketTime.UnixMilli()) || output[1] != "test" || output[2] != "test" || output[3] != "true" {
81+
t.Errorf("dataErr: expected [%v test test true], got %v", timestamp.UnixMilli(), output)
7682
}
7783

7884
// Order
7985
orderPacket := dataPacketer.NewPacket(0)
86+
orderPacketTime := time.Now()
8087
orderRecord := &order.Record{
8188
Packet: orderPacket,
8289
From: "test",
@@ -90,7 +97,7 @@ func TestLogger(t *testing.T) {
9097

9198
filename = path.Join(
9299
"logger/order",
93-
fmt.Sprintf("order_%s", timestamp.Format("2006_01_02_15_04_05")),
100+
fmt.Sprintf("%s", timestamp.Format(logger.TimestampFormat)),
94101
"order.csv",
95102
)
96103
file, err = os.Open(filename)
@@ -104,27 +111,45 @@ func TestLogger(t *testing.T) {
104111
t.Error(err)
105112
}
106113

107-
if output[0] != fmt.Sprint(timestamp.UnixMilli()) || output[1] != "test" || output[2] != "test" {
114+
if output[0] != fmt.Sprint(orderPacketTime.UnixMilli()) || output[1] != "test" || output[2] != "test" {
108115
t.Errorf("orderErr: expected [test test], got %v", output)
109116
}
110117

111118
// Protection
112119
protectionPacket := protectionPacketer.NewPacket(0, protectionPacketer.Ok)
120+
protectionPacket.Timestamp = &protectionPacketer.Timestamp{
121+
Counter: 0,
122+
Second: 0,
123+
Minute: 0,
124+
Hour: 0,
125+
Day: 0,
126+
Month: 0,
127+
Year: 0,
128+
}
129+
protectionPacket.Name = "test"
130+
protectionPacket.Kind = protectionPacketer.EqualsKind
131+
protectionPacket.Data = &protectionPacketer.Equals[int8]{
132+
Target: 0,
133+
Value: 0,
134+
}
135+
protectionPacket.Type = protectionPacketer.Uint8Type
113136
protectionRecord := &protection.Record{
114137
Packet: protectionPacket,
115138
BoardId: abstraction.BoardId(0),
116139
From: "test",
117140
To: "test",
118141
Timestamp: timestamp,
119142
}
143+
protectionPacketTime := protectionPacket.Timestamp.ToTime()
144+
120145
err = loggerHandler.PushRecord(protectionRecord)
121146
if err != nil {
122147
t.Error(err)
123148
}
124149

125150
filename = path.Join(
126151
"logger/protections",
127-
fmt.Sprintf("protections_%s", timestamp.Format("2006_01_02_15_04_05")),
152+
fmt.Sprintf("%s", timestamp.Format(logger.TimestampFormat)),
128153
fmt.Sprintf("%s.csv", "test"),
129154
)
130155
file, err = os.Open(filename)
@@ -138,13 +163,13 @@ func TestLogger(t *testing.T) {
138163
t.Error(err)
139164
}
140165

141-
if output[0] != fmt.Sprint(timestamp.UnixMilli()) || output[1] != "test" || output[2] != "test" || output[3] != "0" {
166+
if output[0] != fmt.Sprint(timestamp.UnixMilli()) || output[1] != "test" || output[2] != "test" || output[3] != "0" || output[4] != "7" || output[5] != "3" || output[6] != "test" || output[7] != "&{0 0}" || output[8] != protectionPacketTime.Format(time.RFC3339) {
142167
t.Errorf("orderErr: expected [test test 0], got %v", output)
143168
}
144169

145170
if closeErr := loggerHandler.Stop(); closeErr != nil {
146171
t.Error(err)
147172
}
148173

149-
// os.RemoveAll("logger")
174+
os.RemoveAll("logger")
150175
}

0 commit comments

Comments
 (0)