github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/dimension/dimension_test.go (about)

     1  package dimension
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  )
     7  
     8  var _ = Describe("dimension", func() {
     9  	Context("Delete", func() {
    10  		It("works", func() {
    11  			d1 := New()
    12  			d1.Insert(Key("bar"))
    13  			d1.Insert(Key("baz"))
    14  			d1.Insert(Key("foo"))
    15  
    16  			d2 := New()
    17  			d2.Insert(Key("foo"))
    18  			d2.Insert(Key("baz"))
    19  			d2.Insert(Key("bar"))
    20  
    21  			d3 := New()
    22  			d3.Insert(Key("bar"))
    23  
    24  			d1.Delete(Key("baz"))
    25  			d2.Delete(Key("foo"))
    26  			d3.Delete(Key("bar"))
    27  
    28  			d1.Delete(Key("x"))
    29  			d2.Delete(Key("x"))
    30  			d3.Delete(Key("x"))
    31  
    32  			Expect(Intersection(d1)).To(ConsistOf([]Key{
    33  				Key("bar"),
    34  				Key("foo"),
    35  			}))
    36  			Expect(Intersection(d2)).To(ConsistOf([]Key{
    37  				Key("bar"),
    38  				Key("baz"),
    39  			}))
    40  			Expect(Intersection(d3)).To(Equal([]Key{}))
    41  		})
    42  	})
    43  
    44  	Context("Intersection", func() {
    45  		It("works", func() {
    46  			d1 := New()
    47  			d1.Insert(Key("bar"))
    48  			d1.Insert(Key("baz"))
    49  			d1.Insert(Key("foo"))
    50  
    51  			d2 := New()
    52  			d2.Insert(Key("foo"))
    53  			d2.Insert(Key("baz"))
    54  			d2.Insert(Key("bar"))
    55  
    56  			d3 := New()
    57  			d3.Insert(Key("bar"))
    58  
    59  			d4 := New()
    60  
    61  			Expect(Intersection(d1, d2, d3, d4)).To(BeNil())
    62  			Expect(Intersection(d1, d2, d3)).To(Equal([]Key{Key("bar")}))
    63  			Expect(Intersection(d1, d2)).To(ConsistOf([]Key{
    64  				Key("bar"),
    65  				Key("baz"),
    66  				Key("foo"),
    67  			}))
    68  		})
    69  
    70  		It("works correctly", func() {
    71  			d1 := New()
    72  			v := []string{
    73  				"ride-sharing-app.cpu{hostname=40dfbd6616c3,region=eu-north,vehicle=scooter}",
    74  				"ride-sharing-app.cpu{hostname=4ef76b35f112,region=us-east,vehicle=scooter}",
    75  				"ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south,vehicle=scooter}",
    76  				"ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south,vehicle=scooter}",
    77  				"ride-sharing-app.cpu{hostname=904af763ff84,region=us-east,vehicle=scooter}",
    78  				"ride-sharing-app.cpu{hostname=a985ede2759f,region=eu-north,vehicle=scooter}",
    79  			}
    80  			for _, k := range v {
    81  				d1.Insert(Key(k))
    82  			}
    83  
    84  			d2 := New()
    85  			v = []string{
    86  				"ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south,vehicle=bike}",
    87  				"ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south,vehicle=car}",
    88  				"ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south,vehicle=scooter}",
    89  				"ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south}",
    90  				"ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south,vehicle=bike}",
    91  				"ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south,vehicle=car}",
    92  				"ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south,vehicle=scooter}",
    93  				"ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south}",
    94  			}
    95  			for _, k := range v {
    96  				d2.Insert(Key(k))
    97  			}
    98  
    99  			Expect(Intersection(d1, d2)).To(ConsistOf([]Key{
   100  				Key("ride-sharing-app.cpu{hostname=5fec370dfb99,region=ap-south,vehicle=scooter}"),
   101  				Key("ride-sharing-app.cpu{hostname=680222ce937b,region=ap-south,vehicle=scooter}"),
   102  			}))
   103  		})
   104  	})
   105  
   106  	Context("Union", func() {
   107  		It("works", func() {
   108  			d1 := New()
   109  			d1.Insert(Key("bar"))
   110  			d1.Insert(Key("baz"))
   111  			d1.Insert(Key("foo"))
   112  
   113  			d2 := New()
   114  			d2.Insert(Key("foo"))
   115  			d2.Insert(Key("baz"))
   116  			d2.Insert(Key("bar"))
   117  
   118  			d3 := New()
   119  			d3.Insert(Key("bar"))
   120  
   121  			d4 := New()
   122  
   123  			Expect(Union(d1, d2, d3, d4)).To(ConsistOf([]Key{
   124  				Key("bar"),
   125  				Key("baz"),
   126  				Key("foo"),
   127  			}))
   128  			Expect(Union(d1, d2, d3)).To(ConsistOf([]Key{
   129  				Key("bar"),
   130  				Key("baz"),
   131  				Key("foo"),
   132  			}))
   133  			Expect(Union(d3, d4)).To(ConsistOf([]Key{
   134  				Key("bar"),
   135  			}))
   136  		})
   137  	})
   138  })