github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/service/application_metadata_cache_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"context"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  	"github.com/pyroscope-io/pyroscope/pkg/model/appmetadata"
     9  	"github.com/pyroscope-io/pyroscope/pkg/service"
    10  	"github.com/pyroscope-io/pyroscope/pkg/storage/metadata"
    11  )
    12  
    13  type mockApplicationWriter struct {
    14  	onWrite func(application appmetadata.ApplicationMetadata) error
    15  }
    16  
    17  func (svc *mockApplicationWriter) CreateOrUpdate(ctx context.Context, application appmetadata.ApplicationMetadata) error {
    18  	return svc.onWrite(application)
    19  }
    20  
    21  var _ = Describe("ApplicationWriteCacheService", func() {
    22  	s := new(testSuite)
    23  	BeforeEach(s.BeforeEach)
    24  	AfterEach(s.AfterEach)
    25  
    26  	sampleApp := appmetadata.ApplicationMetadata{
    27  		FQName:          "myapp",
    28  		SampleRate:      100,
    29  		SpyName:         "gospy",
    30  		Units:           metadata.SamplesUnits,
    31  		AggregationType: metadata.AverageAggregationType,
    32  	}
    33  
    34  	var m mockApplicationWriter
    35  	var svc *service.ApplicationMetadataCacheService
    36  	var cfg service.ApplicationMetadataCacheServiceConfig
    37  
    38  	BeforeEach(func() {
    39  		svc = service.NewApplicationMetadataCacheService(cfg, &m)
    40  	})
    41  
    42  	When("cache is empty", func() {
    43  		BeforeEach(func() {
    44  			m = mockApplicationWriter{
    45  				onWrite: func(application appmetadata.ApplicationMetadata) error {
    46  					Expect(application).To(Equal(sampleApp))
    47  					return nil
    48  				},
    49  			}
    50  		})
    51  
    52  		It("writes to underlying svc", func() {
    53  			err := svc.CreateOrUpdate(context.TODO(), sampleApp)
    54  			Expect(err).ToNot(HaveOccurred())
    55  		})
    56  	})
    57  
    58  	When("cache is not empty", func() {
    59  		var n int
    60  
    61  		BeforeEach(func() {
    62  			n = 0
    63  			m = mockApplicationWriter{
    64  				onWrite: func(application appmetadata.ApplicationMetadata) error {
    65  					n = n + 1
    66  					return nil
    67  				},
    68  			}
    69  		})
    70  
    71  		When("data is the same", func() {
    72  			It("does not write to underlying svc", func() {
    73  				err := svc.CreateOrUpdate(context.TODO(), sampleApp)
    74  				Expect(err).ToNot(HaveOccurred())
    75  
    76  				err = svc.CreateOrUpdate(context.TODO(), sampleApp)
    77  				Expect(err).ToNot(HaveOccurred())
    78  				Expect(n).To(Equal(1))
    79  			})
    80  		})
    81  
    82  		When("data is different", func() {
    83  			It("writes to the underlying svc", func() {
    84  				err := svc.CreateOrUpdate(context.TODO(), sampleApp)
    85  				Expect(err).ToNot(HaveOccurred())
    86  
    87  				sampleApp2 := sampleApp
    88  				sampleApp2.SpyName = "myspy"
    89  
    90  				err = svc.CreateOrUpdate(context.TODO(), sampleApp2)
    91  				Expect(err).ToNot(HaveOccurred())
    92  				Expect(n).To(Equal(2))
    93  			})
    94  		})
    95  	})
    96  })