5
5
"strconv"
6
6
"sync"
7
7
"testing"
8
+ "text/template"
8
9
"time"
9
10
10
11
"github.com/google/uuid"
@@ -44,7 +45,7 @@ func TestMetrics(t *testing.T) {
44
45
45
46
reg := prometheus .NewRegistry ()
46
47
metrics := notifications .NewMetrics (reg )
47
- template := notifications .TemplateWorkspaceDeleted
48
+ tmpl := notifications .TemplateWorkspaceDeleted
48
49
49
50
const (
50
51
method = database .NotificationMethodSmtp
@@ -76,7 +77,7 @@ func TestMetrics(t *testing.T) {
76
77
user := createSampleUser (t , store )
77
78
78
79
// Build fingerprints for the two different series we expect.
79
- methodTemplateFP := fingerprintLabels (notifications .LabelMethod , string (method ), notifications .LabelTemplateID , template .String ())
80
+ methodTemplateFP := fingerprintLabels (notifications .LabelMethod , string (method ), notifications .LabelTemplateID , tmpl .String ())
80
81
methodFP := fingerprintLabels (notifications .LabelMethod , string (method ))
81
82
82
83
expected := map [string ]func (metric * dto.Metric , series string ) bool {
@@ -90,7 +91,7 @@ func TestMetrics(t *testing.T) {
90
91
91
92
var match string
92
93
for result , val := range results {
93
- seriesFP := fingerprintLabels (notifications .LabelMethod , string (method ), notifications .LabelTemplateID , template .String (), notifications .LabelResult , result )
94
+ seriesFP := fingerprintLabels (notifications .LabelMethod , string (method ), notifications .LabelTemplateID , tmpl .String (), notifications .LabelResult , result )
94
95
if ! hasMatchingFingerprint (metric , seriesFP ) {
95
96
continue
96
97
}
@@ -165,9 +166,9 @@ func TestMetrics(t *testing.T) {
165
166
}
166
167
167
168
// WHEN: 2 notifications are enqueued, 1 of which will fail until its retries are exhausted, and another which will succeed
168
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" ) // this will succeed
169
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "success" }, "test" ) // this will succeed
169
170
require .NoError (t , err )
170
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "failure" }, "test2" ) // this will fail and retry (maxAttempts - 1) times
171
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "failure" }, "test2" ) // this will fail and retry (maxAttempts - 1) times
171
172
require .NoError (t , err )
172
173
173
174
mgr .Run (ctx )
@@ -218,7 +219,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
218
219
219
220
reg := prometheus .NewRegistry ()
220
221
metrics := notifications .NewMetrics (reg )
221
- template := notifications .TemplateWorkspaceDeleted
222
+ tmpl := notifications .TemplateWorkspaceDeleted
222
223
223
224
const method = database .NotificationMethodSmtp
224
225
@@ -252,9 +253,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
252
253
user := createSampleUser (t , store )
253
254
254
255
// WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
255
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" ) // this will succeed
256
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "success" }, "test" ) // this will succeed
256
257
require .NoError (t , err )
257
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "failure" }, "test2" ) // this will fail and retry (maxAttempts - 1) times
258
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "failure" }, "test2" ) // this will fail and retry (maxAttempts - 1) times
258
259
require .NoError (t , err )
259
260
260
261
mgr .Run (ctx )
@@ -309,7 +310,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
309
310
310
311
reg := prometheus .NewRegistry ()
311
312
metrics := notifications .NewMetrics (reg )
312
- template := notifications .TemplateWorkspaceDeleted
313
+ tmpl := notifications .TemplateWorkspaceDeleted
313
314
314
315
const method = database .NotificationMethodSmtp
315
316
@@ -342,7 +343,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
342
343
343
344
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
344
345
for i := 0 ; i < msgCount ; i ++ {
345
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" , "i" : strconv .Itoa (i )}, "test" )
346
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "success" , "i" : strconv .Itoa (i )}, "test" )
346
347
require .NoError (t , err )
347
348
}
348
349
@@ -351,7 +352,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
351
352
// THEN:
352
353
// Ensure we see the dispatches of the messages inflight.
353
354
require .Eventually (t , func () bool {
354
- return promtest .ToFloat64 (metrics .InflightDispatches .WithLabelValues (string (method ), template .String ())) == msgCount
355
+ return promtest .ToFloat64 (metrics .InflightDispatches .WithLabelValues (string (method ), tmpl .String ())) == msgCount
355
356
}, testutil .WaitShort , testutil .IntervalFast )
356
357
357
358
for i := 0 ; i < msgCount ; i ++ {
@@ -389,7 +390,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
389
390
var (
390
391
reg = prometheus .NewRegistry ()
391
392
metrics = notifications .NewMetrics (reg )
392
- template = notifications .TemplateWorkspaceDeleted
393
+ tmpl = notifications .TemplateWorkspaceDeleted
393
394
anotherTemplate = notifications .TemplateWorkspaceDormant
394
395
)
395
396
@@ -400,7 +401,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
400
401
401
402
// GIVEN: a template whose notification method differs from the default.
402
403
out , err := store .UpdateNotificationTemplateMethodByID (ctx , database.UpdateNotificationTemplateMethodByIDParams {
403
- ID : template ,
404
+ ID : tmpl ,
404
405
Method : database.NullNotificationMethod {NotificationMethod : customMethod , Valid : true },
405
406
})
406
407
require .NoError (t , err )
@@ -426,7 +427,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
426
427
427
428
user := createSampleUser (t , store )
428
429
429
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" )
430
+ _ , err = enq .Enqueue (ctx , user .ID , tmpl , map [string ]string {"type" : "success" }, "test" )
430
431
require .NoError (t , err )
431
432
_ , err = enq .Enqueue (ctx , user .ID , anotherTemplate , map [string ]string {"type" : "success" }, "test" )
432
433
require .NoError (t , err )
@@ -447,7 +448,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
447
448
// THEN: we should have metric series for both the default and custom notification methods.
448
449
require .Eventually (t , func () bool {
449
450
return promtest .ToFloat64 (metrics .DispatchAttempts .WithLabelValues (string (defaultMethod ), anotherTemplate .String (), notifications .ResultSuccess )) > 0 &&
450
- promtest .ToFloat64 (metrics .DispatchAttempts .WithLabelValues (string (customMethod ), template .String (), notifications .ResultSuccess )) > 0
451
+ promtest .ToFloat64 (metrics .DispatchAttempts .WithLabelValues (string (customMethod ), tmpl .String (), notifications .ResultSuccess )) > 0
451
452
}, testutil .WaitShort , testutil .IntervalFast )
452
453
}
453
454
@@ -525,8 +526,8 @@ func newBarrierHandler(total int, handler notifications.Handler) *barrierHandler
525
526
}
526
527
}
527
528
528
- func (bh * barrierHandler ) Dispatcher (payload types.MessagePayload , title , body string ) (dispatch.DeliveryFunc , error ) {
529
- deliverFn , err := bh .h .Dispatcher (payload , title , body )
529
+ func (bh * barrierHandler ) Dispatcher (payload types.MessagePayload , title , body string , helpers template. FuncMap ) (dispatch.DeliveryFunc , error ) {
530
+ deliverFn , err := bh .h .Dispatcher (payload , title , body , helpers )
530
531
if err != nil {
531
532
return nil , err
532
533
}
0 commit comments