55"strconv"
66"sync"
77"testing"
8+ "text/template"
89"time"
910
1011"github.com/google/uuid"
@@ -44,7 +45,7 @@ func TestMetrics(t *testing.T) {
4445
4546reg := prometheus .NewRegistry ()
4647metrics := notifications .NewMetrics (reg )
47- template := notifications .TemplateWorkspaceDeleted
48+ tmpl := notifications .TemplateWorkspaceDeleted
4849
4950const (
5051method = database .NotificationMethodSmtp
@@ -76,7 +77,7 @@ func TestMetrics(t *testing.T) {
7677user := createSampleUser (t ,store )
7778
7879// 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 ())
8081methodFP := fingerprintLabels (notifications .LabelMethod ,string (method ))
8182
8283expected := map [string ]func (metric * dto.Metric ,series string )bool {
@@ -90,7 +91,7 @@ func TestMetrics(t *testing.T) {
9091
9192var match string
9293for 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 )
9495if ! hasMatchingFingerprint (metric ,seriesFP ) {
9596continue
9697}
@@ -165,9 +166,9 @@ func TestMetrics(t *testing.T) {
165166}
166167
167168// 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
169170require .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
171172require .NoError (t ,err )
172173
173174mgr .Run (ctx )
@@ -218,7 +219,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
218219
219220reg := prometheus .NewRegistry ()
220221metrics := notifications .NewMetrics (reg )
221- template := notifications .TemplateWorkspaceDeleted
222+ tmpl := notifications .TemplateWorkspaceDeleted
222223
223224const method = database .NotificationMethodSmtp
224225
@@ -252,9 +253,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
252253user := createSampleUser (t ,store )
253254
254255// 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
256257require .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
258259require .NoError (t ,err )
259260
260261mgr .Run (ctx )
@@ -309,7 +310,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
309310
310311reg := prometheus .NewRegistry ()
311312metrics := notifications .NewMetrics (reg )
312- template := notifications .TemplateWorkspaceDeleted
313+ tmpl := notifications .TemplateWorkspaceDeleted
313314
314315const method = database .NotificationMethodSmtp
315316
@@ -342,7 +343,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
342343
343344// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
344345for 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" )
346347require .NoError (t ,err )
347348}
348349
@@ -351,7 +352,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
351352// THEN:
352353// Ensure we see the dispatches of the messages inflight.
353354require .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
355356},testutil .WaitShort ,testutil .IntervalFast )
356357
357358for i := 0 ;i < msgCount ;i ++ {
@@ -389,7 +390,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
389390var (
390391reg = prometheus .NewRegistry ()
391392metrics = notifications .NewMetrics (reg )
392- template = notifications .TemplateWorkspaceDeleted
393+ tmpl = notifications .TemplateWorkspaceDeleted
393394anotherTemplate = notifications .TemplateWorkspaceDormant
394395)
395396
@@ -400,7 +401,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
400401
401402// GIVEN: a template whose notification method differs from the default.
402403out ,err := store .UpdateNotificationTemplateMethodByID (ctx , database.UpdateNotificationTemplateMethodByIDParams {
403- ID :template ,
404+ ID :tmpl ,
404405Method : database.NullNotificationMethod {NotificationMethod :customMethod ,Valid :true },
405406})
406407require .NoError (t ,err )
@@ -426,7 +427,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
426427
427428user := createSampleUser (t ,store )
428429
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" )
430431require .NoError (t ,err )
431432_ ,err = enq .Enqueue (ctx ,user .ID ,anotherTemplate ,map [string ]string {"type" :"success" },"test" )
432433require .NoError (t ,err )
@@ -447,7 +448,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
447448// THEN: we should have metric series for both the default and custom notification methods.
448449require .Eventually (t ,func ()bool {
449450return 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
451452},testutil .WaitShort ,testutil .IntervalFast )
452453}
453454
@@ -525,8 +526,8 @@ func newBarrierHandler(total int, handler notifications.Handler) *barrierHandler
525526}
526527}
527528
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 )
530531if err != nil {
531532return nil ,err
532533}