github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrstan/nrstan_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrstan
     5  
     6  import (
     7  	"os"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/nats-io/nats-streaming-server/server"
    12  	"github.com/nats-io/stan.go"
    13  	newrelic "github.com/newrelic/go-agent"
    14  	"github.com/newrelic/go-agent/internal"
    15  	"github.com/newrelic/go-agent/internal/integrationsupport"
    16  )
    17  
    18  const (
    19  	clusterName = "my_test_cluster"
    20  	clientName  = "me"
    21  )
    22  
    23  func TestMain(m *testing.M) {
    24  	s, err := server.RunServer(clusterName)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	defer s.Shutdown()
    29  	os.Exit(m.Run())
    30  }
    31  
    32  func createTestApp() integrationsupport.ExpectApp {
    33  	return integrationsupport.NewTestApp(integrationsupport.SampleEverythingReplyFn, cfgFn)
    34  }
    35  
    36  var cfgFn = func(cfg *newrelic.Config) {
    37  	cfg.Enabled = false
    38  	cfg.DistributedTracer.Enabled = true
    39  	cfg.TransactionTracer.SegmentThreshold = 0
    40  	cfg.TransactionTracer.Threshold.IsApdexFailing = false
    41  	cfg.TransactionTracer.Threshold.Duration = 0
    42  	cfg.Attributes.Include = append(cfg.Attributes.Include,
    43  		newrelic.AttributeMessageRoutingKey,
    44  		newrelic.AttributeMessageQueueName,
    45  		newrelic.AttributeMessageExchangeType,
    46  		newrelic.AttributeMessageReplyTo,
    47  		newrelic.AttributeMessageCorrelationID,
    48  	)
    49  }
    50  
    51  func TestSubWrapperWithNilApp(t *testing.T) {
    52  	subject := "sample.subject1"
    53  	sc, err := stan.Connect(clusterName, clientName)
    54  	if err != nil {
    55  		t.Fatal("Couldn't connect to server", err)
    56  	}
    57  	defer sc.Close()
    58  
    59  	wg := sync.WaitGroup{}
    60  	sc.Subscribe(subject, StreamingSubWrapper(nil, func(msg *stan.Msg) {
    61  		defer wg.Done()
    62  	}))
    63  	wg.Add(1)
    64  	sc.Publish(subject, []byte("data"))
    65  	wg.Wait()
    66  }
    67  
    68  func TestSubWrapper(t *testing.T) {
    69  	subject := "sample.subject2"
    70  	sc, err := stan.Connect(clusterName, clientName)
    71  	if err != nil {
    72  		t.Fatal("Couldn't connect to server", err)
    73  	}
    74  	defer sc.Close()
    75  
    76  	wg := sync.WaitGroup{}
    77  	app := createTestApp()
    78  	sc.Subscribe(subject, WgWrapper(&wg, StreamingSubWrapper(app, func(msg *stan.Msg) {})))
    79  
    80  	wg.Add(1)
    81  	sc.Publish(subject, []byte("data"))
    82  	wg.Wait()
    83  
    84  	app.ExpectMetrics(t, []internal.WantMetric{
    85  		{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
    86  		{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
    87  		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/all", Scope: "", Forced: false, Data: nil},
    88  		{Name: "DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther", Scope: "", Forced: false, Data: nil},
    89  		{Name: "OtherTransaction/Go/Message/STAN/Topic/Named/sample.subject2", Scope: "", Forced: true, Data: nil},
    90  		{Name: "OtherTransactionTotalTime/Go/Message/STAN/Topic/Named/sample.subject2", Scope: "", Forced: false, Data: nil},
    91  	})
    92  	app.ExpectTxnEvents(t, []internal.WantEvent{
    93  		{
    94  			Intrinsics: map[string]interface{}{
    95  				"name":     "OtherTransaction/Go/Message/STAN/Topic/Named/sample.subject2",
    96  				"guid":     internal.MatchAnything,
    97  				"priority": internal.MatchAnything,
    98  				"sampled":  internal.MatchAnything,
    99  				"traceId":  internal.MatchAnything,
   100  			},
   101  			AgentAttributes: map[string]interface{}{
   102  				"message.routingKey": "sample.subject2",
   103  			},
   104  			UserAttributes: map[string]interface{}{},
   105  		},
   106  	})
   107  }
   108  
   109  // Wrapper function to ensure that the NR wrapper is done recording transaction data before wg.Done() is called
   110  func WgWrapper(wg *sync.WaitGroup, nrWrap func(msg *stan.Msg)) func(msg *stan.Msg) {
   111  	return func(msg *stan.Msg) {
   112  		nrWrap(msg)
   113  		wg.Done()
   114  	}
   115  }