18
18
package modelprocessor_test
19
19
20
20
import (
21
+ "bytes"
21
22
"context"
22
- "crypto/md5"
23
23
"encoding/hex"
24
+ "hash"
25
+ "strings"
24
26
"testing"
25
27
26
28
"github.com/stretchr/testify/assert"
@@ -36,7 +38,7 @@ func TestSetGroupingKey(t *testing.T) {
36
38
}{
37
39
"empty" : {
38
40
input : & modelpb.Error {},
39
- groupingKey : hashStrings ( /*empty*/ ) ,
41
+ groupingKey : "" ,
40
42
},
41
43
"exception_type_log_parammessage" : {
42
44
input : & modelpb.Error {
@@ -47,7 +49,7 @@ func TestSetGroupingKey(t *testing.T) {
47
49
ParamMessage : "log_parammessage" ,
48
50
},
49
51
},
50
- groupingKey : hashStrings ("exception_type" , "log_parammessage" ),
52
+ groupingKey : hexifyStrings ("exception_type" , "log_parammessage" ),
51
53
},
52
54
"exception_stacktrace" : {
53
55
input : & modelpb.Error {
@@ -74,7 +76,7 @@ func TestSetGroupingKey(t *testing.T) {
74
76
},
75
77
Log : & modelpb.ErrorLog {Stacktrace : []* modelpb.StacktraceFrame {{Filename : "abc" }}}, // ignored
76
78
},
77
- groupingKey : hashStrings (
79
+ groupingKey : hexifyStrings (
78
80
"module" , "func_1" , "filename" , "func_2" , "classname" , "func_4" , "func_5" , "func_6" ,
79
81
),
80
82
},
@@ -84,7 +86,7 @@ func TestSetGroupingKey(t *testing.T) {
84
86
Stacktrace : []* modelpb.StacktraceFrame {{Function : "function" }},
85
87
},
86
88
},
87
- groupingKey : hashStrings ("function" ),
89
+ groupingKey : hexifyStrings ("function" ),
88
90
},
89
91
"exception_message" : {
90
92
input : & modelpb.Error {
@@ -101,13 +103,13 @@ func TestSetGroupingKey(t *testing.T) {
101
103
},
102
104
Log : & modelpb.ErrorLog {Message : "log_message" }, // ignored
103
105
},
104
- groupingKey : hashStrings ("message_1" , "message_2" , "message_3" , "message_4" ),
106
+ groupingKey : hexifyStrings ("message_1" , "message_2" , "message_3" , "message_4" ),
105
107
},
106
108
"log_message" : {
107
109
input : & modelpb.Error {
108
110
Log : & modelpb.ErrorLog {Message : "log_message" }, // ignored
109
111
},
110
- groupingKey : hashStrings ("log_message" ),
112
+ groupingKey : hexifyStrings ("log_message" ),
111
113
},
112
114
}
113
115
@@ -116,7 +118,7 @@ func TestSetGroupingKey(t *testing.T) {
116
118
batch := modelpb.Batch {{
117
119
Error : test .input ,
118
120
}}
119
- processor := modelprocessor.SetGroupingKey {}
121
+ processor := modelprocessor.SetGroupingKey {NewHash : func () hash. Hash { return & fakeHash {} } }
120
122
err := processor .ProcessBatch (context .Background (), & batch )
121
123
assert .NoError (t , err )
122
124
assert .Equal (t , test .groupingKey , batch [0 ].Error .GroupingKey )
@@ -125,10 +127,24 @@ func TestSetGroupingKey(t *testing.T) {
125
127
126
128
}
127
129
128
- func hashStrings (s ... string ) string {
129
- md5 := md5 .New ()
130
- for _ , s := range s {
131
- md5 .Write ([]byte (s ))
132
- }
133
- return hex .EncodeToString (md5 .Sum (nil ))
130
+ // fakeHash is a hash.Hash that just accumulates the written data
131
+ // to a buffer, and returns it exactly as written.
132
+ type fakeHash struct {
133
+ bytes.Buffer
134
+ }
135
+
136
+ func (f * fakeHash ) Sum (b []byte ) []byte {
137
+ return append (b , f .Buffer .Bytes ()... )
138
+ }
139
+
140
+ func (f * fakeHash ) Size () int {
141
+ return f .Buffer .Len ()
142
+ }
143
+
144
+ func (f * fakeHash ) BlockSize () int {
145
+ return 1
146
+ }
147
+
148
+ func hexifyStrings (s ... string ) string {
149
+ return hex .EncodeToString ([]byte (strings .Join (s , "" )))
134
150
}
0 commit comments