Best Gauge code snippet using gauge.Len
specItemFilter_test.go
Source:specItemFilter_test.go
1/*----------------------------------------------------------------2 * Copyright (c) ThoughtWorks, Inc.3 * Licensed under the Apache License, Version 2.04 * See LICENSE in the project root for license information.5 *----------------------------------------------------------------*/6package filter7import (8 "os"9 "testing"10 "github.com/getgauge/gauge/gauge"11 . "gopkg.in/check.v1"12)13func Test(t *testing.T) { TestingT(t) }14type MySuite struct{}15var _ = Suite(&MySuite{})16func before() {17 os.Clearenv()18 os.Setenv("allow_case_sensitive_tags", "false")19}20func after() {21 os.Clearenv()22}23func (s *MySuite) TestToEvaluateTagExpressionWithTwoTags(c *C) {24 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag1 & tag3"}25 c.Assert(filter.filterTags([]string{"tag1", "tag2"}), Equals, false)26}27func (s *MySuite) TestToEvaluateTagExpressionWithComplexTagExpression(c *C) {28 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag1 & ((tag3 | tag2) & (tag5 | tag4 | tag3) & tag7) | tag6"}29 c.Assert(filter.filterTags([]string{"tag1", "tag2", "tag7", "tag4"}), Equals, true)30}31func (s *MySuite) TestToEvaluateTagExpressionWithFailingTagExpression(c *C) {32 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag1 & ((tag3 | tag2) & (tag5 | tag4 | tag3) & tag7) & tag6"}33 c.Assert(filter.filterTags([]string{"tag1", "tag2", "tag7", "tag4"}), Equals, false)34}35func (s *MySuite) TestToEvaluateTagExpressionWithWrongTagExpression(c *C) {36 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag1 & ((((tag3 | tag2) & (tag5 | tag4 | tag3) & tag7) & tag6"}37 c.Assert(filter.filterTags([]string{"tag1", "tag2", "tag7", "tag4"}), Equals, false)38}39func (s *MySuite) TestToEvaluateTagExpressionConsistingOfSpaces(c *C) {40 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag 1 & tag3"}41 c.Assert(filter.filterTags([]string{"tag 1", "tag3"}), Equals, true)42}43func (s *MySuite) TestToEvaluateTagExpressionConsistingLogicalNotOperator(c *C) {44 filter := &ScenarioFilterBasedOnTags{tagExpression: "!tag 1 & tag3"}45 c.Assert(filter.filterTags([]string{"tag2", "tag3"}), Equals, true)46}47func (s *MySuite) TestToEvaluateTagExpressionConsistingManyLogicalNotOperator(c *C) {48 filter := &ScenarioFilterBasedOnTags{tagExpression: "!(!(tag 1 | !(tag6 | !(tag5))) & tag2)"}49 value := filter.filterTags([]string{"tag2", "tag4"})50 c.Assert(value, Equals, true)51}52func (s *MySuite) TestToEvaluateTagExpressionConsistingParallelLogicalNotOperator(c *C) {53 filter := &ScenarioFilterBasedOnTags{tagExpression: "!(tag1) & ! (tag3 & ! (tag3))"}54 value := filter.filterTags([]string{"tag2", "tag4"})55 c.Assert(value, Equals, true)56}57func (s *MySuite) TestToEvaluateTagExpressionConsistingComma(c *C) {58 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag 1 , tag3"}59 c.Assert(filter.filterTags([]string{"tag2", "tag3"}), Equals, false)60}61func (s *MySuite) TestToEvaluateTagExpressionConsistingCommaGivesTrue(c *C) {62 filter := &ScenarioFilterBasedOnTags{tagExpression: "tag 1 , tag3"}63 c.Assert(filter.filterTags([]string{"tag1", "tag3"}), Equals, true)64}65func (s *MySuite) TestToEvaluateTagExpressionConsistingTrueAndFalseAsTagNames(c *C) {66 filter := &ScenarioFilterBasedOnTags{tagExpression: "true , false"}67 c.Assert(filter.filterTags([]string{"true", "false"}), Equals, true)68}69func (s *MySuite) TestToEvaluateTagExpressionConsistingTrueAndFalseAsTagNamesWithNegation(c *C) {70 filter := &ScenarioFilterBasedOnTags{tagExpression: "!true"}71 c.Assert(filter.filterTags(nil), Equals, true)72}73func (s *MySuite) TestToEvaluateTagExpressionConsistingSpecialCharacters(c *C) {74 filter := &ScenarioFilterBasedOnTags{tagExpression: "a && b || c | b & b"}75 c.Assert(filter.filterTags([]string{"a", "b"}), Equals, true)76}77func (s *MySuite) TestToEvaluateTagExpressionWhenTagIsSubsetOfTrueOrFalse(c *C) {78 // https://github.com/getgauge/gauge/issues/66779 filter := &ScenarioFilterBasedOnTags{tagExpression: "b || c | b & b && a"}80 c.Assert(filter.filterTags([]string{"a", "b"}), Equals, true)81}82func (s *MySuite) TestParseTagExpression(c *C) {83 filter := &ScenarioFilterBasedOnTags{tagExpression: "b || c | b & b && a"}84 txps, tags := filter.parseTagExpression()85 expectedTxps := []string{"b", "|", "|", "c", "|", "b", "&", "b", "&", "&", "a"}86 expectedTags := []string{"b", "c", "b", "b", "a"}87 for i, t := range txps {88 c.Assert(expectedTxps[i], Equals, t)89 }90 for i, t := range tags {91 c.Assert(expectedTags[i], Equals, t)92 }93}94func (s *MySuite) TestScenarioSpanFilter(c *C) {95 scenario1 := &gauge.Scenario{96 Heading: &gauge.Heading{Value: "First Scenario"},97 Span: &gauge.Span{Start: 1, End: 3},98 }99 scenario2 := &gauge.Scenario{100 Heading: &gauge.Heading{Value: "Second Scenario"},101 Span: &gauge.Span{Start: 4, End: 6},102 }103 scenario3 := &gauge.Scenario{104 Heading: &gauge.Heading{Value: "Third Scenario"},105 Span: &gauge.Span{Start: 7, End: 10},106 }107 scenario4 := &gauge.Scenario{108 Heading: &gauge.Heading{Value: "Fourth Scenario"},109 Span: &gauge.Span{Start: 11, End: 15},110 }111 spec := &gauge.Specification{112 Items: []gauge.Item{scenario1, scenario2, scenario3, scenario4},113 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3, scenario4},114 }115 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{8}))116 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 1)117 c.Assert(specWithFilteredItems.Scenarios[0], Equals, scenario3)118 c.Assert(len(specWithOtherItems.Scenarios), Equals, 3)119 c.Assert(specWithOtherItems.Scenarios[0], Equals, scenario1)120 c.Assert(specWithOtherItems.Scenarios[1], Equals, scenario2)121 c.Assert(specWithOtherItems.Scenarios[2], Equals, scenario4)122}123func (s *MySuite) TestScenarioSpanFilterLastScenario(c *C) {124 scenario1 := &gauge.Scenario{125 Heading: &gauge.Heading{Value: "First Scenario"},126 Span: &gauge.Span{Start: 1, End: 3},127 }128 scenario2 := &gauge.Scenario{129 Heading: &gauge.Heading{Value: "Second Scenario"},130 Span: &gauge.Span{Start: 4, End: 6},131 }132 scenario3 := &gauge.Scenario{133 Heading: &gauge.Heading{Value: "Third Scenario"},134 Span: &gauge.Span{Start: 7, End: 10},135 }136 scenario4 := &gauge.Scenario{137 Heading: &gauge.Heading{Value: "Fourth Scenario"},138 Span: &gauge.Span{Start: 11, End: 15},139 }140 spec := &gauge.Specification{141 Items: []gauge.Item{scenario1, scenario2, scenario3, scenario4},142 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3, scenario4},143 }144 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{13}))145 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 1)146 c.Assert(specWithFilteredItems.Scenarios[0], Equals, scenario4)147 c.Assert(len(specWithOtherItems.Scenarios), Equals, 3)148 c.Assert(specWithOtherItems.Scenarios[0], Equals, scenario1)149 c.Assert(specWithOtherItems.Scenarios[1], Equals, scenario2)150 c.Assert(specWithOtherItems.Scenarios[2], Equals, scenario3)151}152func (s *MySuite) TestScenarioSpanFilterFirstScenario(c *C) {153 scenario1 := &gauge.Scenario{154 Heading: &gauge.Heading{Value: "First Scenario"},155 Span: &gauge.Span{Start: 1, End: 3},156 }157 scenario2 := &gauge.Scenario{158 Heading: &gauge.Heading{Value: "Second Scenario"},159 Span: &gauge.Span{Start: 4, End: 6},160 }161 scenario3 := &gauge.Scenario{162 Heading: &gauge.Heading{Value: "Third Scenario"},163 Span: &gauge.Span{Start: 7, End: 10},164 }165 scenario4 := &gauge.Scenario{166 Heading: &gauge.Heading{Value: "Fourth Scenario"},167 Span: &gauge.Span{Start: 11, End: 15},168 }169 spec := &gauge.Specification{170 Items: []gauge.Item{scenario1, scenario2, scenario3, scenario4},171 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3, scenario4},172 }173 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{2}))174 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 1)175 c.Assert(specWithFilteredItems.Scenarios[0], Equals, scenario1)176 c.Assert(len(specWithOtherItems.Scenarios), Equals, 3)177 c.Assert(specWithOtherItems.Scenarios[0], Equals, scenario2)178 c.Assert(specWithOtherItems.Scenarios[1], Equals, scenario3)179 c.Assert(specWithOtherItems.Scenarios[2], Equals, scenario4)180}181func (s *MySuite) TestScenarioSpanFilterForSingleScenarioSpec(c *C) {182 scenario1 := &gauge.Scenario{183 Heading: &gauge.Heading{Value: "First Scenario"},184 Span: &gauge.Span{Start: 1, End: 3},185 }186 spec := &gauge.Specification{187 Items: []gauge.Item{scenario1},188 Scenarios: []*gauge.Scenario{scenario1},189 }190 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{3}))191 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 1)192 c.Assert(specWithFilteredItems.Scenarios[0], Equals, scenario1)193 c.Assert(len(specWithOtherItems.Scenarios), Equals, 0)194}195func (s *MySuite) TestScenarioSpanFilterWithWrongScenarioIndex(c *C) {196 scenario1 := &gauge.Scenario{197 Heading: &gauge.Heading{Value: "First Scenario"},198 Span: &gauge.Span{Start: 1, End: 3},199 }200 spec := &gauge.Specification{201 Items: []gauge.Item{scenario1},202 Scenarios: []*gauge.Scenario{scenario1},203 }204 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{5}))205 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 0)206 c.Assert(len(specWithOtherItems.Scenarios), Equals, 1)207 c.Assert(specWithOtherItems.Scenarios[0], Equals, scenario1)208}209func (s *MySuite) TestScenarioSpanFilterWithMultipleLineNumbers(c *C) {210 scenario1 := &gauge.Scenario{211 Heading: &gauge.Heading{Value: "First Scenario"},212 Span: &gauge.Span{Start: 1, End: 3},213 }214 scenario2 := &gauge.Scenario{215 Heading: &gauge.Heading{Value: "Second Scenario"},216 Span: &gauge.Span{Start: 4, End: 6},217 }218 scenario3 := &gauge.Scenario{219 Heading: &gauge.Heading{Value: "Third Scenario"},220 Span: &gauge.Span{Start: 7, End: 10},221 }222 scenario4 := &gauge.Scenario{223 Heading: &gauge.Heading{Value: "Fourth Scenario"},224 Span: &gauge.Span{Start: 11, End: 15},225 }226 spec := &gauge.Specification{227 Items: []gauge.Item{scenario1, scenario2, scenario3, scenario4},228 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3, scenario4},229 }230 specWithFilteredItems, specWithOtherItems := spec.Filter(NewScenarioFilterBasedOnSpan([]int{3, 13}))231 c.Assert(len(specWithFilteredItems.Scenarios), Equals, 2)232 c.Assert(specWithFilteredItems.Scenarios[0], Equals, scenario1)233 c.Assert(specWithFilteredItems.Scenarios[1], Equals, scenario4)234 c.Assert(len(specWithOtherItems.Scenarios), Equals, 2)235 c.Assert(specWithOtherItems.Scenarios[0], Equals, scenario2)236 c.Assert(specWithOtherItems.Scenarios[1], Equals, scenario3)237}238func (s *MySuite) TestToFilterSpecsByTagExpOfTwoTags(c *C) {239 myTags := []string{"tag1", "tag2"}240 scenario1 := &gauge.Scenario{241 Heading: &gauge.Heading{Value: "First Scenario"},242 Span: &gauge.Span{Start: 1, End: 3},243 }244 scenario2 := &gauge.Scenario{245 Heading: &gauge.Heading{Value: "Second Scenario"},246 Span: &gauge.Span{Start: 4, End: 6},247 }248 spec1 := &gauge.Specification{249 Items: []gauge.Item{scenario1, scenario2},250 Scenarios: []*gauge.Scenario{scenario1, scenario2},251 Tags: &gauge.Tags{RawValues: [][]string{myTags}},252 }253 spec2 := &gauge.Specification{254 Items: []gauge.Item{scenario1, scenario2},255 Scenarios: []*gauge.Scenario{scenario1, scenario2},256 }257 var specs []*gauge.Specification258 specs = append(specs, spec1, spec2)259 c.Assert(specs[0].Tags.Values()[0], Equals, myTags[0])260 c.Assert(specs[0].Tags.Values()[1], Equals, myTags[1])261 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")262 c.Assert(len(filteredSpecs), Equals, 1)263 c.Assert(len(otherSpecs), Equals, 1)264}265func (s *MySuite) TestToEvaluateTagExpression(c *C) {266 myTags := []string{"tag1", "tag2"}267 scenario1 := &gauge.Scenario{268 Heading: &gauge.Heading{Value: "First Scenario"},269 Span: &gauge.Span{Start: 1, End: 3},270 Tags: &gauge.Tags{RawValues: [][]string{{myTags[0]}}},271 }272 scenario2 := &gauge.Scenario{273 Heading: &gauge.Heading{Value: "Second Scenario"},274 Span: &gauge.Span{Start: 4, End: 6},275 Tags: &gauge.Tags{RawValues: [][]string{{"tag3"}}},276 }277 scenario3 := &gauge.Scenario{278 Heading: &gauge.Heading{Value: "Third Scenario"},279 Span: &gauge.Span{Start: 1, End: 3},280 }281 scenario4 := &gauge.Scenario{282 Heading: &gauge.Heading{Value: "Fourth Scenario"},283 Span: &gauge.Span{Start: 4, End: 6},284 }285 spec1 := &gauge.Specification{286 Items: []gauge.Item{scenario1, scenario2},287 Scenarios: []*gauge.Scenario{scenario1, scenario2},288 }289 spec2 := &gauge.Specification{290 Items: []gauge.Item{scenario3, scenario4},291 Scenarios: []*gauge.Scenario{scenario3, scenario4},292 Tags: &gauge.Tags{RawValues: [][]string{myTags}},293 }294 var specs []*gauge.Specification295 specs = append(specs, spec1, spec2)296 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & !(tag1 & tag4) & (tag2 | tag3)")297 c.Assert(len(filteredSpecs), Equals, 1)298 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 2)299 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario3)300 c.Assert(filteredSpecs[0].Scenarios[1], Equals, scenario4)301 c.Assert(len(otherSpecs), Equals, 1)302 c.Assert(len(otherSpecs[0].Scenarios), Equals, 2)303 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)304 c.Assert(otherSpecs[0].Scenarios[1], Equals, scenario2)305}306func (s *MySuite) TestToFilterSpecsByWrongTagExpression(c *C) {307 myTags := []string{"tag1", "tag2"}308 scenario1 := &gauge.Scenario{309 Heading: &gauge.Heading{Value: "First Scenario"},310 Span: &gauge.Span{Start: 1, End: 3},311 }312 scenario2 := &gauge.Scenario{313 Heading: &gauge.Heading{Value: "Second Scenario"},314 Span: &gauge.Span{Start: 4, End: 6},315 }316 spec1 := &gauge.Specification{317 Items: []gauge.Item{scenario1, scenario2},318 Scenarios: []*gauge.Scenario{scenario1, scenario2},319 Tags: &gauge.Tags{RawValues: [][]string{myTags}},320 }321 spec2 := &gauge.Specification{322 Items: []gauge.Item{scenario1, scenario2},323 Scenarios: []*gauge.Scenario{scenario1, scenario2},324 }325 var specs []*gauge.Specification326 specs = append(specs, spec1, spec2)327 c.Assert(specs[0].Tags.Values()[0], Equals, myTags[0])328 c.Assert(specs[0].Tags.Values()[1], Equals, myTags[1])329 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "(tag1 & tag2")330 c.Assert(len(filteredSpecs), Equals, 0)331 c.Assert(len(otherSpecs), Equals, 2)332}333func (s *MySuite) TestToFilterMultipleScenariosByMultipleTags(c *C) {334 myTags := []string{"tag1", "tag2"}335 scenario1 := &gauge.Scenario{336 Heading: &gauge.Heading{Value: "First Scenario"},337 Span: &gauge.Span{Start: 1, End: 3},338 Tags: &gauge.Tags{RawValues: [][]string{{"tag1"}}},339 }340 scenario2 := &gauge.Scenario{341 Heading: &gauge.Heading{Value: "Second Scenario"},342 Span: &gauge.Span{Start: 4, End: 6},343 Tags: &gauge.Tags{RawValues: [][]string{myTags}},344 }345 scenario3 := &gauge.Scenario{346 Heading: &gauge.Heading{Value: "Third Scenario"},347 Span: &gauge.Span{Start: 1, End: 3},348 Tags: &gauge.Tags{RawValues: [][]string{myTags}},349 }350 scenario4 := &gauge.Scenario{351 Heading: &gauge.Heading{Value: "Fourth Scenario"},352 Span: &gauge.Span{Start: 4, End: 6},353 Tags: &gauge.Tags{RawValues: [][]string{{"prod", "tag7", "tag1", "tag2"}}},354 }355 spec1 := &gauge.Specification{356 Items: []gauge.Item{scenario1, scenario2, scenario3, scenario4},357 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3, scenario4},358 }359 var specs []*gauge.Specification360 specs = append(specs, spec1)361 c.Assert(len(specs[0].Scenarios), Equals, 4)362 c.Assert(len(specs[0].Scenarios[0].Tags.Values()), Equals, 1)363 c.Assert(len(specs[0].Scenarios[1].Tags.Values()), Equals, 2)364 c.Assert(len(specs[0].Scenarios[2].Tags.Values()), Equals, 2)365 c.Assert(len(specs[0].Scenarios[3].Tags.Values()), Equals, 4)366 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")367 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 3)368 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario2)369 c.Assert(filteredSpecs[0].Scenarios[1], Equals, scenario3)370 c.Assert(filteredSpecs[0].Scenarios[2], Equals, scenario4)371 c.Assert(len(otherSpecs[0].Scenarios), Equals, 1)372 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)373}374func (s *MySuite) TestToFilterScenariosByTagsAtSpecLevel(c *C) {375 myTags := []string{"tag1", "tag2"}376 scenario1 := &gauge.Scenario{377 Heading: &gauge.Heading{Value: "First Scenario"},378 Span: &gauge.Span{Start: 1, End: 3},379 }380 scenario2 := &gauge.Scenario{381 Heading: &gauge.Heading{Value: "Second Scenario"},382 Span: &gauge.Span{Start: 4, End: 6},383 }384 scenario3 := &gauge.Scenario{385 Heading: &gauge.Heading{Value: "Third Scenario"},386 Span: &gauge.Span{Start: 4, End: 6},387 }388 spec1 := &gauge.Specification{389 Items: []gauge.Item{scenario1, scenario2, scenario3},390 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},391 Tags: &gauge.Tags{RawValues: [][]string{myTags}},392 }393 var specs []*gauge.Specification394 specs = append(specs, spec1)395 c.Assert(len(specs[0].Scenarios), Equals, 3)396 c.Assert(len(specs[0].Tags.Values()), Equals, 2)397 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")398 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 3)399 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario1)400 c.Assert(filteredSpecs[0].Scenarios[1], Equals, scenario2)401 c.Assert(filteredSpecs[0].Scenarios[2], Equals, scenario3)402 c.Assert(len(otherSpecs), Equals, 0)403}404func (s *MySuite) TestToFilterScenariosByTagExpWithDuplicateTagNames(c *C) {405 myTags := []string{"tag1", "tag12"}406 scenario1 := &gauge.Scenario{407 Heading: &gauge.Heading{Value: "First Scenario"},408 Span: &gauge.Span{Start: 1, End: 3},409 Tags: &gauge.Tags{RawValues: [][]string{myTags}},410 }411 scenario2 := &gauge.Scenario{412 Heading: &gauge.Heading{Value: "Second Scenario"},413 Span: &gauge.Span{Start: 4, End: 6},414 Tags: &gauge.Tags{RawValues: [][]string{{"tag1"}}},415 }416 scenario3 := &gauge.Scenario{417 Heading: &gauge.Heading{Value: "Third Scenario"},418 Span: &gauge.Span{Start: 4, End: 6},419 Tags: &gauge.Tags{RawValues: [][]string{{"tag12"}}},420 }421 spec1 := &gauge.Specification{422 Items: []gauge.Item{scenario1, scenario2, scenario3},423 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},424 }425 var specs []*gauge.Specification426 specs = append(specs, spec1)427 c.Assert(len(specs), Equals, 1)428 c.Assert(len(specs[0].Scenarios), Equals, 3)429 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag12")430 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 1)431 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario1)432 c.Assert(len(otherSpecs), Equals, 1)433 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario2)434 c.Assert(otherSpecs[0].Scenarios[1], Equals, scenario3)435}436func (s *MySuite) TestFilterTags(c *C) {437 specTags := []string{"abcd", "foo", "bar", "foo bar"}438 tagFilter := NewScenarioFilterBasedOnTags(specTags, "abcd & foo bar")439 evaluateTrue := tagFilter.filterTags(specTags)440 c.Assert(evaluateTrue, Equals, true)441}442func (s *MySuite) TestFilterMixedCaseTags(c *C) {443 before()444 defer after()445 specTags := []string{"abcd", "foo", "BAR", "foo bar"}446 tagFilter := NewScenarioFilterBasedOnTags(specTags, "abcd & FOO bar")447 evaluateTrue := tagFilter.filterTags(specTags)448 c.Assert(evaluateTrue, Equals, true)449}450func (s *MySuite) TestSanitizeTags(c *C) {451 specTags := []string{"abcd", "foo", "bar", "foo bar"}452 tagFilter := NewScenarioFilterBasedOnTags(specTags, "abcd & foo bar | true")453 evaluateTrue := tagFilter.filterTags(specTags)454 c.Assert(evaluateTrue, Equals, true)455}456func (s *MySuite) TestSanitizeMixedCaseTags(c *C) {457 before()458 defer after()459 specTags := []string{"abcd", "foo", "bar", "foo bar"}460 tagFilter := NewScenarioFilterBasedOnTags(specTags, "abcd & foo Bar | true")461 evaluateTrue := tagFilter.filterTags(specTags)462 c.Assert(evaluateTrue, Equals, true)463}464func (s *MySuite) TestToFilterSpecsByTags(c *C) {465 myTags := []string{"tag1", "tag2"}466 scenario1 := &gauge.Scenario{467 Heading: &gauge.Heading{Value: "First Scenario"},468 Span: &gauge.Span{Start: 1, End: 3},469 Tags: &gauge.Tags{RawValues: [][]string{myTags}},470 }471 scenario2 := &gauge.Scenario{472 Heading: &gauge.Heading{Value: "Second Scenario"},473 Span: &gauge.Span{Start: 4, End: 6},474 }475 scenario3 := &gauge.Scenario{476 Heading: &gauge.Heading{Value: "Third Scenario"},477 Span: &gauge.Span{Start: 1, End: 3},478 }479 spec1 := &gauge.Specification{480 Items: []gauge.Item{scenario1, scenario2},481 Scenarios: []*gauge.Scenario{scenario1, scenario2},482 }483 spec2 := &gauge.Specification{484 Items: []gauge.Item{scenario2, scenario3},485 Scenarios: []*gauge.Scenario{scenario2, scenario3},486 }487 spec3 := &gauge.Specification{488 Items: []gauge.Item{scenario1, scenario3},489 Scenarios: []*gauge.Scenario{scenario1, scenario3},490 }491 var specs []*gauge.Specification492 specs = append(specs, spec1, spec2, spec3)493 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")494 c.Assert(len(filteredSpecs), Equals, 2)495 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 1)496 c.Assert(len(filteredSpecs[1].Scenarios), Equals, 1)497 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario1)498 c.Assert(filteredSpecs[1].Scenarios[0], Equals, scenario1)499 c.Assert(len(otherSpecs), Equals, 3)500 c.Assert(len(otherSpecs[0].Scenarios), Equals, 1)501 c.Assert(len(otherSpecs[1].Scenarios), Equals, 2)502 c.Assert(len(otherSpecs[2].Scenarios), Equals, 1)503 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario2)504 c.Assert(otherSpecs[1].Scenarios[0], Equals, scenario2)505 c.Assert(otherSpecs[1].Scenarios[1], Equals, scenario3)506 c.Assert(otherSpecs[2].Scenarios[0], Equals, scenario3)507}508func (s *MySuite) TestToFilterSpecsByMixedCaseTags(c *C) {509 before()510 defer after()511 myTags := []string{"tag1", "TAG2"}512 scenario1 := &gauge.Scenario{513 Heading: &gauge.Heading{Value: "First Scenario"},514 Span: &gauge.Span{Start: 1, End: 3},515 Tags: &gauge.Tags{RawValues: [][]string{myTags}},516 }517 scenario2 := &gauge.Scenario{518 Heading: &gauge.Heading{Value: "Second Scenario"},519 Span: &gauge.Span{Start: 4, End: 6},520 }521 scenario3 := &gauge.Scenario{522 Heading: &gauge.Heading{Value: "Third Scenario"},523 Span: &gauge.Span{Start: 1, End: 3},524 }525 spec1 := &gauge.Specification{526 Items: []gauge.Item{scenario1, scenario2},527 Scenarios: []*gauge.Scenario{scenario1, scenario2},528 }529 spec2 := &gauge.Specification{530 Items: []gauge.Item{scenario2, scenario3},531 Scenarios: []*gauge.Scenario{scenario2, scenario3},532 }533 spec3 := &gauge.Specification{534 Items: []gauge.Item{scenario1, scenario3},535 Scenarios: []*gauge.Scenario{scenario1, scenario3},536 }537 var specs []*gauge.Specification538 specs = append(specs, spec1, spec2, spec3)539 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & Tag2")540 c.Assert(len(filteredSpecs), Equals, 2)541 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 1)542 c.Assert(len(filteredSpecs[1].Scenarios), Equals, 1)543 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario1)544 c.Assert(filteredSpecs[1].Scenarios[0], Equals, scenario1)545 c.Assert(len(otherSpecs), Equals, 3)546 c.Assert(len(otherSpecs[0].Scenarios), Equals, 1)547 c.Assert(len(otherSpecs[1].Scenarios), Equals, 2)548 c.Assert(len(otherSpecs[2].Scenarios), Equals, 1)549 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario2)550 c.Assert(otherSpecs[1].Scenarios[0], Equals, scenario2)551 c.Assert(otherSpecs[1].Scenarios[1], Equals, scenario3)552 c.Assert(otherSpecs[2].Scenarios[0], Equals, scenario3)553}554func (s *MySuite) TestToFilterScenariosByTag(c *C) {555 myTags := []string{"tag1", "tag2"}556 scenario1 := &gauge.Scenario{557 Heading: &gauge.Heading{Value: "First Scenario"},558 Span: &gauge.Span{Start: 1, End: 3},559 }560 scenario2 := &gauge.Scenario{561 Heading: &gauge.Heading{Value: "Second Scenario"},562 Span: &gauge.Span{Start: 4, End: 6},563 Tags: &gauge.Tags{RawValues: [][]string{myTags}},564 }565 scenario3 := &gauge.Scenario{566 Heading: &gauge.Heading{Value: "Third Scenario"},567 Span: &gauge.Span{Start: 4, End: 6},568 }569 spec1 := &gauge.Specification{570 Items: []gauge.Item{scenario1, scenario2, scenario3},571 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},572 }573 var specs []*gauge.Specification574 specs = append(specs, spec1)575 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")576 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 1)577 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario2)578 c.Assert(len(otherSpecs), Equals, 1)579 c.Assert(len(otherSpecs[0].Scenarios), Equals, 2)580 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)581 c.Assert(otherSpecs[0].Scenarios[1], Equals, scenario3)582}583func (s *MySuite) TestToFilterScenariosByMixedCaseTag(c *C) {584 before()585 defer after()586 myTags := []string{"Tag-1", "tag2"}587 scenario1 := &gauge.Scenario{588 Heading: &gauge.Heading{Value: "First Scenario"},589 Span: &gauge.Span{Start: 1, End: 3},590 }591 scenario2 := &gauge.Scenario{592 Heading: &gauge.Heading{Value: "Second Scenario"},593 Span: &gauge.Span{Start: 4, End: 6},594 Tags: &gauge.Tags{RawValues: [][]string{myTags}},595 }596 scenario3 := &gauge.Scenario{597 Heading: &gauge.Heading{Value: "Third Scenario"},598 Span: &gauge.Span{Start: 4, End: 6},599 }600 spec1 := &gauge.Specification{601 Items: []gauge.Item{scenario1, scenario2, scenario3},602 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},603 }604 var specs []*gauge.Specification605 specs = append(specs, spec1)606 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag-1 & tag2")607 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 1)608 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario2)609 c.Assert(len(otherSpecs), Equals, 1)610 c.Assert(len(otherSpecs[0].Scenarios), Equals, 2)611 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)612 c.Assert(otherSpecs[0].Scenarios[1], Equals, scenario3)613}614func (s *MySuite) TestToFilterMultipleScenariosByTags(c *C) {615 myTags := []string{"tag1", "tag2"}616 scenario1 := &gauge.Scenario{617 Heading: &gauge.Heading{Value: "First Scenario"},618 Span: &gauge.Span{Start: 1, End: 3},619 Tags: &gauge.Tags{RawValues: [][]string{{"tag1"}}},620 }621 scenario2 := &gauge.Scenario{622 Heading: &gauge.Heading{Value: "Second Scenario"},623 Span: &gauge.Span{Start: 4, End: 6},624 Tags: &gauge.Tags{RawValues: [][]string{myTags}},625 }626 scenario3 := &gauge.Scenario{627 Heading: &gauge.Heading{Value: "Third Scenario"},628 Span: &gauge.Span{Start: 4, End: 6},629 Tags: &gauge.Tags{RawValues: [][]string{myTags}},630 }631 spec1 := &gauge.Specification{632 Items: []gauge.Item{scenario1, scenario2, scenario3},633 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},634 }635 var specs []*gauge.Specification636 specs = append(specs, spec1)637 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag1 & tag2")638 c.Assert(len(filteredSpecs[0].Scenarios), Equals, 2)639 c.Assert(filteredSpecs[0].Scenarios[0], Equals, scenario2)640 c.Assert(filteredSpecs[0].Scenarios[1], Equals, scenario3)641 c.Assert(len(otherSpecs), Equals, 1)642 c.Assert(len(otherSpecs[0].Scenarios), Equals, 1)643 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)644}645func (s *MySuite) TestToFilterScenariosByUnavailableTags(c *C) {646 myTags := []string{"tag1", "tag2"}647 scenario1 := &gauge.Scenario{648 Heading: &gauge.Heading{Value: "First Scenario"},649 Span: &gauge.Span{Start: 1, End: 3},650 }651 scenario2 := &gauge.Scenario{652 Heading: &gauge.Heading{Value: "Second Scenario"},653 Span: &gauge.Span{Start: 4, End: 6},654 Tags: &gauge.Tags{RawValues: [][]string{myTags}},655 }656 scenario3 := &gauge.Scenario{657 Heading: &gauge.Heading{Value: "Third Scenario"},658 Span: &gauge.Span{Start: 4, End: 6},659 }660 spec1 := &gauge.Specification{661 Items: []gauge.Item{scenario1, scenario2, scenario3},662 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},663 }664 var specs []*gauge.Specification665 specs = append(specs, spec1)666 filteredSpecs, otherSpecs := filterSpecsByTags(specs, "tag3")667 c.Assert(len(filteredSpecs), Equals, 0)668 c.Assert(len(otherSpecs), Equals, 1)669 c.Assert(len(otherSpecs[0].Scenarios), Equals, 3)670 c.Assert(otherSpecs[0].Scenarios[0], Equals, scenario1)671 c.Assert(otherSpecs[0].Scenarios[1], Equals, scenario2)672 c.Assert(otherSpecs[0].Scenarios[2], Equals, scenario3)673}674func (s *MySuite) TestFilterScenariosByName(c *C) {675 scenario1 := &gauge.Scenario{676 Heading: &gauge.Heading{Value: "First Scenario"},677 }678 scenario2 := &gauge.Scenario{679 Heading: &gauge.Heading{Value: "Second Scenario"},680 }681 spec1 := &gauge.Specification{682 Items: []gauge.Item{scenario1, scenario2},683 Scenarios: []*gauge.Scenario{scenario1, scenario2},684 }685 var scenarios = []string{"First Scenario"}686 var specs []*gauge.Specification687 specs = append(specs, spec1)688 c.Assert(len(specs[0].Scenarios), Equals, 2)689 specs = filterSpecsByScenarioName(specs, scenarios)690 c.Assert(len(specs[0].Scenarios), Equals, 1)691}692func (s *MySuite) TestFilterScenarioWhichDoesNotExists(c *C) {693 scenario1 := &gauge.Scenario{694 Heading: &gauge.Heading{Value: "First Scenario"},695 }696 scenario2 := &gauge.Scenario{697 Heading: &gauge.Heading{Value: "Second Scenario"},698 }699 spec1 := &gauge.Specification{700 Items: []gauge.Item{scenario1, scenario2},701 Scenarios: []*gauge.Scenario{scenario1, scenario2},702 }703 var scenarios = []string{"Third Scenario"}704 var specs []*gauge.Specification705 specs = append(specs, spec1)706 c.Assert(len(specs[0].Scenarios), Equals, 2)707 specs = filterSpecsByScenarioName(specs, scenarios)708 c.Assert(len(specs), Equals, 0)709}710func (s *MySuite) TestFilterMultipleScenariosByName(c *C) {711 scenario1 := &gauge.Scenario{712 Heading: &gauge.Heading{Value: "First Scenario"},713 }714 scenario2 := &gauge.Scenario{715 Heading: &gauge.Heading{Value: "Second Scenario"},716 }717 spec1 := &gauge.Specification{718 Items: []gauge.Item{scenario1, scenario2},719 Scenarios: []*gauge.Scenario{scenario1, scenario2},720 }721 var scenarios = []string{"First Scenario", "Second Scenario"}722 var specs []*gauge.Specification723 specs = append(specs, spec1)724 c.Assert(len(specs[0].Scenarios), Equals, 2)725 specs = filterSpecsByScenarioName(specs, scenarios)726 c.Assert(len(specs[0].Scenarios), Equals, 2)727}728func (s *MySuite) TestFilterInvalidScenarios(c *C) {729 scenario1 := &gauge.Scenario{730 Heading: &gauge.Heading{Value: "First Scenario"},731 }732 scenario2 := &gauge.Scenario{733 Heading: &gauge.Heading{Value: "Second Scenario"},734 }735 spec1 := &gauge.Specification{736 Items: []gauge.Item{scenario1, scenario2},737 Scenarios: []*gauge.Scenario{scenario1, scenario2},738 }739 var scenarios = []string{"First Scenario", "Third Scenario"}740 var specs []*gauge.Specification741 specs = append(specs, spec1)742 c.Assert(len(specs[0].Scenarios), Equals, 2)743 filteredScenarios := filterValidScenarios(specs, scenarios)744 c.Assert(len(filteredScenarios), Equals, 1)745 c.Assert(filteredScenarios[0], Equals, "First Scenario")746}747func (s *MySuite) TestScenarioTagFilterShouldNotRemoveNonScenarioKindItems(c *C) {748 scenario1 := &gauge.Scenario{749 Heading: &gauge.Heading{Value: "First Scenario"},750 Span: &gauge.Span{Start: 1, End: 3},751 }752 scenario2 := &gauge.Scenario{753 Heading: &gauge.Heading{Value: "Second Scenario"},754 Span: &gauge.Span{Start: 4, End: 6},755 Tags: &gauge.Tags{RawValues: [][]string{{"tag2"}}},756 }757 scenario3 := &gauge.Scenario{758 Heading: &gauge.Heading{Value: "Third Scenario"},759 Span: &gauge.Span{Start: 7, End: 10},760 Tags: &gauge.Tags{RawValues: [][]string{{"tag1"}}},761 }762 spec := &gauge.Specification{763 Items: []gauge.Item{scenario1, scenario2, scenario3, &gauge.Table{}, &gauge.Comment{Value: "Comment", LineNo: 1}, &gauge.Step{}},764 Scenarios: []*gauge.Scenario{scenario1, scenario2, scenario3},765 }766 specWithFilteredItems, specWithOtherItems := filterSpecsByTags([]*gauge.Specification{spec}, "tag1 | tag2")767 c.Assert(len(specWithFilteredItems), Equals, 1)768 c.Assert(len(specWithFilteredItems[0].Items), Equals, 5)769 c.Assert(len(specWithOtherItems), Equals, 1)770 c.Assert(len(specWithOtherItems[0].Items), Equals, 4)771}...
aggregator_test.go
Source:aggregator_test.go
...10 "github.com/efc2/efc2-agent/common/log"11 "github.com/stretchr/testify/assert"12)13type MetricSorter []Metric14func (m MetricSorter) Len() int { return len(m) }15func (m MetricSorter) Swap(i, j int) { m[i], m[j] = m[j], m[i] }16func (m MetricSorter) Less(i, j int) bool {17 if m[i].Name == m[j].Name {18 return getValue(m[i]) < getValue(m[j])19 }20 return m[i].Name < m[j].Name21}22func getValue(m Metric) float64 {23 value, _ := m.getCorrectedValue()24 return value25}26func TestNewAggregator(t *testing.T) {27 metricC := make(chan Metric, 5)28 defer close(metricC)29 formatter := func(m Metric) interface{} {30 return nil31 }32 agg := NewAggregator(metricC, 30, "test", formatter, nil, nil, 0)33 if a, ok := agg.(*aggregator); ok {34 assert.Equal(t, int64(DefaultRecentPointThreshold), a.recentPointThreshold)35 assert.Equal(t, int64(DefaultExpirySeconds), a.expirySeconds)36 }37 agg = NewAggregator(metricC, 30, "test", formatter, nil, nil, 0, 30)38 if a, ok := agg.(*aggregator); ok {39 assert.Equal(t, int64(30), a.expirySeconds)40 }41}42func TestAddMetrics(t *testing.T) {43 a := aggregator{44 metrics: make(chan Metric, 10),45 context: make(map[Context]Generator),46 }47 defer close(a.metrics)48 fields := map[string]interface{}{49 "usage": 99,50 }51 a.AddMetrics("gauge", "", fields, nil, "")52 assert.Len(t, a.context, 0)53 a.AddMetrics("gauge", "agg.test", fields, nil, "")54 expected := Metric{55 Name: "agg.test.usage",56 Value: 99,57 }58 assert.Contains(t, a.context, expected.context())59 assert.Len(t, a.context, 1)60 now := time.Now().UnixNano()61 a.AddMetrics("gauge", "agg.test", fields, nil, "", now)62 expected = Metric{63 Name: "agg.test.usage",64 Value: 99,65 Timestamp: now,66 }67 assert.Contains(t, a.context, expected.context())68 assert.Len(t, a.context, 1)69 a.AddMetrics("gauge", "agg.test", fields, []string{"agg:test"}, "")70 expected = Metric{71 Name: "agg.test.usage",72 Value: 99,73 Tags: []string{"agg:test"},74 }75 assert.Contains(t, a.context, expected.context())76 assert.Len(t, a.context, 2)77 a.AddMetrics("gauge", "agg.test", fields, []string{"agg:test"}, "agg")78 expected = Metric{79 Name: "agg.test.usage",80 Value: 99,81 Tags: []string{"agg:test"},82 DeviceName: "agg",83 }84 assert.Contains(t, a.context, expected.context())85 assert.Len(t, a.context, 3)86}87func TestAdd(t *testing.T) {88 a := aggregator{89 metrics: make(chan Metric, 10),90 context: make(map[Context]Generator),91 }92 defer close(a.metrics)93 testm := NewMetric("agg.test", 1)94 a.Add("gauge", testm)95 assert.Contains(t, a.context, testm.context())96 assert.Len(t, a.context, 1)97 testm = NewMetric("agg.test", 2, []string{"agg:test"})98 a.Add("gauge", testm)99 assert.Contains(t, a.context, testm.context())100 assert.Len(t, a.context, 2)101 a.Add("gauge", testm)102 assert.Len(t, a.context, 2)103}104func TestFlush(t *testing.T) {105 a := aggregator{106 metrics: make(chan Metric, 10),107 context: make(map[Context]Generator),108 }109 defer close(a.metrics)110 a.Add("gauge", NewMetric("agg.test", 1))111 a.Add("gauge", NewMetric("agg.test", 2, []string{"agg:test"}))112 a.Flush()113 assert.Len(t, a.context, 2)114 assert.Len(t, a.metrics, 2)115 time.Sleep(1 * time.Second)116 a.Flush()117 assert.Len(t, a.context, 0)118 assert.Len(t, a.metrics, 2)119 metrics := make([]Metric, 2)120 for i := 0; i < 2; i++ {121 metrics[i] = <-a.metrics122 }123 sort.Sort(MetricSorter(metrics))124 testm := metrics[0]125 assert.Equal(t, "agg.test", testm.Name)126 assert.EqualValues(t, 1, testm.Value)127 assert.Nil(t, testm.Tags)128 testm = metrics[1]129 assert.Equal(t, "agg.test", testm.Name)130 assert.EqualValues(t, 2, testm.Value)131 assert.Equal(t, []string{"agg:test"}, testm.Tags)132 now := time.Now().UnixNano()133 a.Add("gauge", Metric{134 Name: "agg.test",135 Value: 3,136 Tags: []string{"agg:test"},137 Timestamp: now,138 })139 a.Flush()140 testm = <-a.metrics141 assert.Equal(t, "agg.test", testm.Name)142 assert.EqualValues(t, 3, testm.Value)143 assert.Equal(t, []string{"agg:test"}, testm.Tags)144 assert.Equal(t, now, testm.Timestamp)145}146func TestCounterNormalization(t *testing.T) {147 a := aggregator{148 metrics: make(chan Metric, 10),149 context: make(map[Context]Generator),150 interval: 10,151 hostname: "myhost",152 }153 defer close(a.metrics)154 a.SubmitPackets("int:1|c")155 a.SubmitPackets("int:4|c")156 a.SubmitPackets("int:15|c")157 a.SubmitPackets("float:5|c")158 a.Flush()159 assert.Len(t, a.metrics, 2)160 metrics := make([]Metric, 2)161 for i := 0; i < 2; i++ {162 metrics[i] = <-a.metrics163 }164 sort.Sort(MetricSorter(metrics))165 testm := metrics[0]166 assert.Equal(t, "float", testm.Name)167 assert.EqualValues(t, 0.5, testm.Value)168 assert.Equal(t, "myhost", testm.Hostname)169 testm = metrics[1]170 assert.Equal(t, "int", testm.Name)171 assert.EqualValues(t, 2, testm.Value)172 assert.Equal(t, "myhost", testm.Hostname)173}174func TestHistogramNormalization(t *testing.T) {175 a := aggregator{176 metrics: make(chan Metric, 20),177 context: make(map[Context]Generator),178 interval: 10,179 hostname: "myhost",180 histogramAggregates: append(DefaultHistogramAggregates, "min"),181 }182 defer close(a.metrics)183 for i := 0; i < 5; i++ {184 a.SubmitPackets("h1:1|h")185 }186 for i := 0; i < 20; i++ {187 a.SubmitPackets("h2:1|h")188 }189 a.Flush()190 assert.Len(t, a.metrics, 12)191 metrics := make([]Metric, 12)192 for i := 0; i < 12; i++ {193 metrics[i] = <-a.metrics194 }195 sort.Sort(MetricSorter(metrics))196 testm := metrics[2]197 assert.EqualValues(t, 0.5, testm.Value)198 testm = metrics[8]199 assert.EqualValues(t, 2, testm.Value)200}201func TestTags(t *testing.T) {202 a := aggregator{203 metrics: make(chan Metric, 10),204 context: make(map[Context]Generator),205 interval: 1,206 hostname: "myhost",207 }208 defer close(a.metrics)209 a.SubmitPackets("gauge:1|c")210 a.SubmitPackets("gauge:2|c|@1")211 a.SubmitPackets("gauge:4|c|#tag1,tag2")212 a.SubmitPackets("gauge:8|c|#tag2,tag1")213 a.SubmitPackets("gauge:16|c|#tag3,tag4")214 a.Flush()215 assert.Len(t, a.metrics, 3)216 metrics := make([]Metric, 3)217 for i := 0; i < 3; i++ {218 metrics[i] = <-a.metrics219 }220 sort.Sort(MetricSorter(metrics))221 testm := metrics[0]222 assert.Equal(t, "gauge", testm.Name)223 assert.EqualValues(t, 3, testm.Value)224 assert.Nil(t, testm.Tags)225 assert.Equal(t, "myhost", testm.Hostname)226 testm = metrics[1]227 assert.Equal(t, "gauge", testm.Name)228 assert.EqualValues(t, 12, testm.Value)229 assert.Equal(t, []string{"tag1", "tag2"}, testm.Tags)230 assert.Equal(t, "myhost", testm.Hostname)231 testm = metrics[2]232 assert.Equal(t, "gauge", testm.Name)233 assert.EqualValues(t, 16, testm.Value)234 assert.Equal(t, []string{"tag3", "tag4"}, testm.Tags)235 assert.Equal(t, "myhost", testm.Hostname)236}237func TestMagicTags(t *testing.T) {238 a := aggregator{239 metrics: make(chan Metric, 10),240 context: make(map[Context]Generator),241 interval: 1,242 hostname: "myhost",243 }244 defer close(a.metrics)245 a.SubmitPackets("my.gauge.a:1|c|#host:test-a")246 a.SubmitPackets("my.gauge.b:4|c|#tag1,tag2,host:test-b")247 a.SubmitPackets("my.gauge.b:8|c|#host:test-b,tag2,tag1")248 a.SubmitPackets("my.gauge.c:10|c|#tag3")249 a.SubmitPackets("my.gauge.c:16|c|#device:floppy,tag3")250 a.Flush()251 assert.Len(t, a.metrics, 4)252 metrics := make([]Metric, 4)253 for i := 0; i < 4; i++ {254 metrics[i] = <-a.metrics255 }256 sort.Sort(MetricSorter(metrics))257 testm := metrics[0]258 assert.Equal(t, "my.gauge.a", testm.Name)259 assert.EqualValues(t, 1, testm.Value)260 assert.Nil(t, testm.Tags)261 assert.Equal(t, "test-a", testm.Hostname)262 testm = metrics[1]263 assert.Equal(t, "my.gauge.b", testm.Name)264 assert.EqualValues(t, 12, testm.Value)265 assert.Equal(t, []string{"tag1", "tag2"}, testm.Tags)266 assert.Equal(t, "test-b", testm.Hostname)267 testm = metrics[2]268 assert.Equal(t, "my.gauge.c", testm.Name)269 assert.EqualValues(t, 10, testm.Value)270 assert.Equal(t, []string{"tag3"}, testm.Tags)271 assert.Empty(t, testm.DeviceName)272 testm = metrics[3]273 assert.Equal(t, "my.gauge.c", testm.Name)274 assert.EqualValues(t, 16, testm.Value)275 assert.Equal(t, []string{"tag3"}, testm.Tags)276 assert.Equal(t, "floppy", testm.DeviceName)277}278func TestCounter(t *testing.T) {279 a := aggregator{280 metrics: make(chan Metric, 10),281 context: make(map[Context]Generator),282 interval: 1,283 hostname: "myhost",284 }285 defer close(a.metrics)286 a.SubmitPackets("my.first.counter:1|c")287 a.SubmitPackets("my.first.counter:5|c")288 a.SubmitPackets("my.second.counter:1|c")289 a.SubmitPackets("my.third.counter:3|c")290 a.Flush()291 assert.Len(t, a.metrics, 3)292 metrics := make([]Metric, 3)293 for i := 0; i < 3; i++ {294 metrics[i] = <-a.metrics295 }296 sort.Sort(MetricSorter(metrics))297 testm := metrics[0]298 assert.Equal(t, "my.first.counter", testm.Name)299 assert.EqualValues(t, 6, testm.Value)300 assert.Equal(t, "myhost", testm.Hostname)301 testm = metrics[1]302 assert.Equal(t, "my.second.counter", testm.Name)303 assert.EqualValues(t, 1, testm.Value)304 testm = metrics[2]305 assert.Equal(t, "my.third.counter", testm.Name)306 assert.EqualValues(t, 3, testm.Value)307 a.Flush()308 assert.Len(t, a.metrics, 3)309 for i := 0; i < 3; i++ {310 metrics[i] = <-a.metrics311 }312 sort.Sort(MetricSorter(metrics))313 testm = metrics[0]314 assert.Equal(t, "my.first.counter", testm.Name)315 assert.EqualValues(t, 0, testm.Value)316 testm = metrics[1]317 assert.Equal(t, "my.second.counter", testm.Name)318 assert.EqualValues(t, 0, testm.Value)319 testm = metrics[2]320 assert.Equal(t, "my.third.counter", testm.Name)321 assert.EqualValues(t, 0, testm.Value)322}323func TestSampledCounter(t *testing.T) {324 a := aggregator{325 metrics: make(chan Metric, 10),326 context: make(map[Context]Generator),327 interval: 1,328 hostname: "myhost",329 }330 defer close(a.metrics)331 a.SubmitPackets("sampled.counter:1|c|@0.5")332 a.Flush()333 assert.Len(t, a.metrics, 1)334 testm := <-a.metrics335 assert.Equal(t, "sampled.counter", testm.Name)336 assert.EqualValues(t, 2, testm.Value)337}338func TestGauge(t *testing.T) {339 a := aggregator{340 metrics: make(chan Metric, 10),341 context: make(map[Context]Generator),342 interval: 1,343 hostname: "myhost",344 }345 defer close(a.metrics)346 a.SubmitPackets("my.first.gauge:1|g")347 a.SubmitPackets("my.first.gauge:5|g")348 a.SubmitPackets("my.second.gauge:1.5|g")349 a.Flush()350 assert.Len(t, a.metrics, 2)351 metrics := make([]Metric, 2)352 for i := 0; i < 2; i++ {353 metrics[i] = <-a.metrics354 }355 sort.Sort(MetricSorter(metrics))356 testm := metrics[0]357 assert.Equal(t, "my.first.gauge", testm.Name)358 assert.EqualValues(t, 5, testm.Value)359 assert.Equal(t, "myhost", testm.Hostname)360 testm = metrics[1]361 assert.Equal(t, "my.second.gauge", testm.Name)362 assert.EqualValues(t, 1.5, testm.Value)363 // Ensure that old gauges get dropped due to old timestamps364 a.Add("gauge", NewMetric("my.first.gauge", 5))365 a.Add("gauge", Metric{366 Name: "my.first.gauge",367 Value: 1,368 Timestamp: 1000000000,369 })370 a.Add("gauge", Metric{371 Name: "my.second.gauge",372 Value: 20,373 Timestamp: 1000000000,374 })375 a.Flush()376 assert.Len(t, a.metrics, 1)377 testm = <-a.metrics378 assert.Equal(t, "my.first.gauge", testm.Name)379 assert.EqualValues(t, 5, testm.Value)380 assert.Equal(t, "myhost", testm.Hostname)381}382func TestSampledGauge(t *testing.T) {383 a := aggregator{384 metrics: make(chan Metric, 10),385 context: make(map[Context]Generator),386 interval: 1,387 hostname: "myhost",388 }389 defer close(a.metrics)390 a.SubmitPackets("sampled.gauge:10|g|@0.1")391 a.Flush()392 assert.Len(t, a.metrics, 1)393 testm := <-a.metrics394 assert.Equal(t, "sampled.gauge", testm.Name)395 assert.EqualValues(t, 10, testm.Value)396}397func TestSets(t *testing.T) {398 a := aggregator{399 metrics: make(chan Metric, 10),400 context: make(map[Context]Generator),401 interval: 1,402 hostname: "myhost",403 }404 defer close(a.metrics)405 a.SubmitPackets("my.set:10|s")406 a.SubmitPackets("my.set:20|s")407 a.SubmitPackets("my.set:20|s")408 a.SubmitPackets("my.set:30|s")409 a.SubmitPackets("my.set:30|s")410 a.SubmitPackets("my.set:30|s")411 a.Flush()412 assert.Len(t, a.metrics, 1)413 testm := <-a.metrics414 assert.Equal(t, "my.set", testm.Name)415 assert.EqualValues(t, 3, testm.Value)416 // Assert there are no more sets417 a.Flush()418 assert.Len(t, a.metrics, 0)419}420func TestStringSets(t *testing.T) {421 a := aggregator{422 metrics: make(chan Metric, 10),423 context: make(map[Context]Generator),424 interval: 1,425 hostname: "myhost",426 }427 defer close(a.metrics)428 a.SubmitPackets("my.set:string|s")429 a.SubmitPackets("my.set:sets|s")430 a.SubmitPackets("my.set:sets|s")431 a.SubmitPackets("my.set:test|s")432 a.SubmitPackets("my.set:test|s")433 a.SubmitPackets("my.set:test|s")434 a.Flush()435 assert.Len(t, a.metrics, 1)436 testm := <-a.metrics437 assert.Equal(t, "my.set", testm.Name)438 assert.EqualValues(t, 3, testm.Value)439 // Assert there are no more sets440 a.Flush()441 assert.Len(t, a.metrics, 0)442}443func TestRate(t *testing.T) {444 a := aggregator{445 metrics: make(chan Metric, 10),446 context: make(map[Context]Generator),447 interval: 1,448 hostname: "myhost",449 }450 defer close(a.metrics)451 a.Add("rate", NewMetric("my.rate", 10))452 a.Flush()453 time.Sleep(1 * time.Second)454 a.Add("rate", NewMetric("my.rate", 40))455 a.Flush()456 assert.Len(t, a.metrics, 1)457 // Check that the rate is calculated correctly458 testm := <-a.metrics459 assert.Equal(t, "my.rate", testm.Name)460 assert.EqualValues(t, 30, testm.Value)461 // Assert that no more rates are given462 a.Flush()463 assert.Len(t, a.metrics, 0)464}465func TestCount(t *testing.T) {466 a := aggregator{467 metrics: make(chan Metric, 10),468 context: make(map[Context]Generator),469 interval: 1,470 hostname: "myhost",471 }472 defer close(a.metrics)473 for i := 0; i < 10; i++ {474 a.Add("count", NewMetric("my.count", 2))475 a.Add("count", NewMetric("my.count", 3))476 a.Flush()477 assert.Len(t, a.metrics, 1)478 // Check that the count is calculated correctly479 testm := <-a.metrics480 assert.Equal(t, "my.count", testm.Name)481 assert.EqualValues(t, 5, testm.Value)482 }483}484func TestMonotonicCount(t *testing.T) {485 a := aggregator{486 metrics: make(chan Metric, 10),487 context: make(map[Context]Generator),488 interval: 1,489 hostname: "myhost",490 }491 defer close(a.metrics)492 a.Add("monotoniccount", NewMetric("my.monotoniccount", 1))493 a.Flush()494 assert.Len(t, a.metrics, 0)495 a.Add("monotoniccount", NewMetric("my.monotoniccount", 2))496 a.Add("monotoniccount", NewMetric("my.monotoniccount", 3))497 a.Add("monotoniccount", NewMetric("my.monotoniccount", 7))498 a.Flush()499 assert.Len(t, a.metrics, 1)500 // Check that the monotoniccount is calculated correctly501 testm := <-a.metrics502 assert.Equal(t, "my.monotoniccount", testm.Name)503 assert.EqualValues(t, 6, testm.Value)504 a.Add("monotoniccount", NewMetric("my.monotoniccount", 11))505 a.Flush()506 assert.Len(t, a.metrics, 1)507 // Check that the monotoniccount is calculated correctly508 testm = <-a.metrics509 assert.EqualValues(t, 4, testm.Value)510}511func TestRateErrors(t *testing.T) {512 a := aggregator{513 metrics: make(chan Metric, 10),514 context: make(map[Context]Generator),515 interval: 1,516 hostname: "myhost",517 }518 defer close(a.metrics)519 a.Add("rate", NewMetric("my.rate", 10))520 time.Sleep(1 * time.Second)521 a.Add("rate", NewMetric("my.rate", 9))522 a.Flush()523 // Since the difference < 0 we shouldn't get a value524 assert.Len(t, a.metrics, 0)525 a.Add("rate", NewMetric("my.rate", 10))526 a.Add("rate", NewMetric("my.rate", 40))527 a.Flush()528 assert.Len(t, a.metrics, 0)529}530func TestHistogram(t *testing.T) {531 a := aggregator{532 metrics: make(chan Metric, 10),533 context: make(map[Context]Generator),534 interval: 1,535 hostname: "myhost",536 histogramAggregates: append(DefaultHistogramAggregates, "min"),537 }538 defer close(a.metrics)539 percentiles := rand.Perm(100)540 for i := range percentiles {541 for j := 0; j < 20; j++ {542 for _, mType := range []string{"h", "ms"} {543 m := fmt.Sprintf("my.p:%d|%s", i+1, mType)544 a.SubmitPackets(m)545 }546 }547 }548 a.Flush()549 assert.Len(t, a.metrics, 6)550 metrics := make([]Metric, 6)551 for i := 0; i < 6; i++ {552 metrics[i] = <-a.metrics553 }554 sort.Sort(MetricSorter(metrics))555 testm := metrics[0]556 assert.Equal(t, "my.p.95percentile", testm.Name)557 assert.EqualValues(t, 95, testm.Value)558 assert.Equal(t, "myhost", testm.Hostname)559 testm = metrics[1]560 assert.Equal(t, "my.p.avg", testm.Name)561 assert.EqualValues(t, 50.5, testm.Value)562 testm = metrics[2]563 assert.Equal(t, "my.p.count", testm.Name)564 assert.EqualValues(t, 4000, testm.Value)565 testm = metrics[3]566 assert.Equal(t, "my.p.max", testm.Name)567 assert.EqualValues(t, 100, testm.Value)568 testm = metrics[4]569 assert.Equal(t, "my.p.median", testm.Name)570 assert.EqualValues(t, 50, testm.Value)571 testm = metrics[5]572 assert.Equal(t, "my.p.min", testm.Name)573 assert.EqualValues(t, 1, testm.Value)574 // Ensure that histograms are reset.575 a.Flush()576 assert.Len(t, a.metrics, 0)577}578func TestSampledHistogram(t *testing.T) {579 a := aggregator{580 metrics: make(chan Metric, 10),581 context: make(map[Context]Generator),582 interval: 1,583 hostname: "myhost",584 histogramAggregates: append(DefaultHistogramAggregates, "min"),585 }586 defer close(a.metrics)587 a.SubmitPackets("sampled.hist:5|h|@0.5")588 a.Flush()589 assert.Len(t, a.metrics, 6)590 metrics := make([]Metric, 6)591 for i := 0; i < 6; i++ {592 metrics[i] = <-a.metrics593 }594 for _, m := range metrics {595 if m.Name == "sampled.hist.count" {596 assert.EqualValues(t, 2, m.Value)597 } else {598 assert.EqualValues(t, 5, m.Value)599 }600 }601}602func TestBatchSubmission(t *testing.T) {603 a := aggregator{604 metrics: make(chan Metric, 10),605 context: make(map[Context]Generator),606 interval: 1,607 hostname: "myhost",608 }609 defer close(a.metrics)610 packets := []string{611 "counter:1|c",612 "counter:1|c",613 "gauge:1|g",614 }615 packet := strings.Join(packets, "\n")616 a.SubmitPackets(packet)617 a.Flush()618 assert.Len(t, a.metrics, 2)619 metrics := make([]Metric, 2)620 for i := 0; i < 2; i++ {621 metrics[i] = <-a.metrics622 }623 sort.Sort(MetricSorter(metrics))624 testm := metrics[0]625 assert.Equal(t, "counter", testm.Name)626 assert.EqualValues(t, 2, testm.Value)627 testm = metrics[1]628 assert.Equal(t, "gauge", testm.Name)629 assert.EqualValues(t, 1, testm.Value)630}631func TestMonkeyBatchingWithoutTags(t *testing.T) {632 a := aggregator{633 metrics: make(chan Metric, 10),634 context: make(map[Context]Generator),635 interval: 1,636 hostname: "myhost",637 histogramAggregates: append(DefaultHistogramAggregates, "min"),638 }639 defer close(a.metrics)640 a.SubmitPackets("test_hist:0.3|ms:2.5|ms|@0.5:3|ms")641 a.Flush()642 assert.Len(t, a.metrics, 6)643 metrics := make([]Metric, 6)644 for i := 0; i < 6; i++ {645 metrics[i] = <-a.metrics646 }647 sort.Sort(MetricSorter(metrics))648 aRef := aggregator{649 metrics: make(chan Metric, 10),650 context: make(map[Context]Generator),651 interval: 1,652 hostname: "myhost",653 histogramAggregates: append(DefaultHistogramAggregates, "min"),654 }655 defer close(aRef.metrics)656 packets := []string{657 "test_hist:0.3|ms",658 "test_hist:2.5|ms|@0.5",659 "test_hist:3|ms",660 }661 packet := strings.Join(packets, "\n")662 aRef.SubmitPackets(packet)663 aRef.Flush()664 assert.Len(t, aRef.metrics, 6)665 metricsRef := make([]Metric, 6)666 for i := 0; i < 6; i++ {667 metricsRef[i] = <-aRef.metrics668 }669 sort.Sort(MetricSorter(metricsRef))670 for i := 0; i < 6; i++ {671 assert.EqualValues(t, metrics[i].Value, metricsRef[i].Value)672 }673}674func TestMonkeyBatchingWithTags(t *testing.T) {675 a := aggregator{676 metrics: make(chan Metric, 10),677 context: make(map[Context]Generator),678 interval: 1,679 hostname: "myhost",680 }681 defer close(a.metrics)682 a.SubmitPackets("test_gauge:1.5|g|#tag1:one,tag2:two:2.3|g|#tag3:three:3|g")683 a.Flush()684 assert.Len(t, a.metrics, 3)685 metrics := make([]Metric, 3)686 for i := 0; i < 3; i++ {687 metrics[i] = <-a.metrics688 }689 sort.Sort(MetricSorter(metrics))690 aRef := aggregator{691 metrics: make(chan Metric, 10),692 context: make(map[Context]Generator),693 interval: 1,694 hostname: "myhost",695 }696 defer close(aRef.metrics)697 packets := []string{698 "test_gauge:1.5|g|#tag1:one,tag2:two",699 "test_gauge:2.3|g|#tag3:three",700 "test_gauge:3|g",701 }702 packet := strings.Join(packets, "\n")703 aRef.SubmitPackets(packet)704 aRef.Flush()705 assert.Len(t, aRef.metrics, 3)706 metricsRef := make([]Metric, 3)707 for i := 0; i < 3; i++ {708 metricsRef[i] = <-aRef.metrics709 }710 sort.Sort(MetricSorter(metricsRef))711 for i := 0; i < 3; i++ {712 assert.EqualValues(t, metrics[i].Value, metricsRef[i].Value)713 assert.Equal(t, metrics[i].Tags, metricsRef[i].Tags)714 }715}716func TestMonkeyBatchingWithTagsAndSamplerate(t *testing.T) {717 a := aggregator{718 metrics: make(chan Metric, 10),719 context: make(map[Context]Generator),720 interval: 1,721 hostname: "myhost",722 histogramAggregates: append(DefaultHistogramAggregates, "min"),723 }724 defer close(a.metrics)725 a.SubmitPackets("test_metric:1.5|c|#tag1:one,tag2:two:2.3|g|#tag3:three:3|g:42|h|#tag1:12,tag42:42|@0.22")726 a.Flush()727 assert.Len(t, a.metrics, 9)728 metrics := make([]Metric, 9)729 for i := 0; i < 9; i++ {730 metrics[i] = <-a.metrics731 }732 sort.Sort(MetricSorter(metrics))733 aRef := aggregator{734 metrics: make(chan Metric, 10),735 context: make(map[Context]Generator),736 interval: 1,737 hostname: "myhost",738 histogramAggregates: append(DefaultHistogramAggregates, "min"),739 }740 defer close(aRef.metrics)741 packets := []string{742 "test_metric:1.5|c|#tag1:one,tag2:two",743 "test_metric:2.3|g|#tag3:three",744 "test_metric:3|g",745 "test_metric:42|h|#tag1:12,tag42:42|@0.22",746 }747 packet := strings.Join(packets, "\n")748 aRef.SubmitPackets(packet)749 aRef.Flush()750 assert.Len(t, aRef.metrics, 9)751 metricsRef := make([]Metric, 9)752 for i := 0; i < 9; i++ {753 metricsRef[i] = <-aRef.metrics754 }755 sort.Sort(MetricSorter(metricsRef))756 for i := 0; i < 9; i++ {757 assert.EqualValues(t, metrics[i].Value, metricsRef[i].Value)758 assert.Equal(t, metrics[i].Tags, metricsRef[i].Tags)759 }760}761func TestInvalidPackets(t *testing.T) {762 invalidPackets := []string{763 "missing.value.and.type",764 "missing.type:2",...
gauge_test.go
Source:gauge_test.go
...40 // test for module get gauges41 suite.SetupTest()42 // initial module gauges check43 gauges := suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)44 suite.Require().Len(gauges, 0)45 lockOwners := suite.SetupManyLocks(5, defaultLiquidTokens, defaultLPTokens, time.Second)46 gaugeID, _, coins, startTime := suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 10)})47 // evenly distributed per lock48 expectedCoinsPerLock := sdk.Coins{sdk.NewInt64Coin("stake", 2)}49 // check gauges50 gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)51 suite.Require().Len(gauges, 1)52 suite.Require().Equal(gauges[0].Id, gaugeID)53 suite.Require().Equal(gauges[0].Coins, coins)54 suite.Require().Equal(gauges[0].NumEpochsPaidOver, uint64(2))55 suite.Require().Equal(gauges[0].FilledEpochs, uint64(0))56 suite.Require().Equal(gauges[0].DistributedCoins, sdk.Coins(nil))57 suite.Require().Equal(gauges[0].StartTime.Unix(), startTime.Unix())58 // check rewards estimation59 rewardsEst := suite.App.IncentivesKeeper.GetRewardsEst(suite.Ctx, lockOwners[0], []lockuptypes.PeriodLock{}, 100)60 suite.Require().Equal(expectedCoinsPerLock.String(), rewardsEst.String())61 // add to gauge62 addCoins := sdk.Coins{sdk.NewInt64Coin("stake", 200)}63 suite.AddToGauge(addCoins, gaugeID)64 // check gauges65 gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)66 suite.Require().Len(gauges, 1)67 expectedGauge := types.Gauge{68 Id: gaugeID,69 IsPerpetual: false,70 DistributeTo: lockuptypes.QueryCondition{71 LockQueryType: lockuptypes.ByDuration,72 Denom: "lptoken",73 Duration: time.Second,74 },75 Coins: coins.Add(addCoins...),76 NumEpochsPaidOver: 2,77 FilledEpochs: 0,78 DistributedCoins: sdk.Coins{},79 StartTime: startTime,80 }81 suite.Require().Equal(gauges[0].String(), expectedGauge.String())82 // check upcoming gauges83 gauges = suite.App.IncentivesKeeper.GetUpcomingGauges(suite.Ctx)84 suite.Require().Len(gauges, 1)85 // start distribution86 suite.Ctx = suite.Ctx.WithBlockTime(startTime)87 gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)88 suite.Require().NoError(err)89 err = suite.App.IncentivesKeeper.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge)90 suite.Require().NoError(err)91 // check upcoming gauges92 gauges = suite.App.IncentivesKeeper.GetUpcomingGauges(suite.Ctx)93 suite.Require().Len(gauges, 0)94 // distribute coins to stakers95 distrCoins, err := suite.App.IncentivesKeeper.Distribute(suite.Ctx, []types.Gauge{*gauge})96 suite.Require().NoError(err)97 suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 105)})98 // check active gauges99 gauges = suite.App.IncentivesKeeper.GetActiveGauges(suite.Ctx)100 suite.Require().Len(gauges, 1)101 // check gauge ids by denom102 gaugeIds := suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")103 suite.Require().Len(gaugeIds, 1)104 // finish distribution105 err = suite.App.IncentivesKeeper.MoveActiveGaugeToFinishedGauge(suite.Ctx, *gauge)106 suite.Require().NoError(err)107 // check finished gauges108 gauges = suite.App.IncentivesKeeper.GetFinishedGauges(suite.Ctx)109 suite.Require().Len(gauges, 1)110 // check gauge by ID111 gauge, err = suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)112 suite.Require().NoError(err)113 suite.Require().NotNil(gauge)114 suite.Require().Equal(*gauge, gauges[0])115 // check invalid gauge ID116 _, err = suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID+1000)117 suite.Require().Error(err)118 rewardsEst = suite.App.IncentivesKeeper.GetRewardsEst(suite.Ctx, lockOwners[0], []lockuptypes.PeriodLock{}, 100)119 suite.Require().Equal(sdk.Coins{}, rewardsEst)120}121func (suite *KeeperTestSuite) TestPerpetualGaugeOperations() {122 // test for module get gauges123 suite.SetupTest()124 // initial module gauges check125 gauges := suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)126 suite.Require().Len(gauges, 0)127 // setup lock and gauge128 lockOwner, gaugeID, coins, startTime := suite.SetupLockAndGauge(true)129 // check gauges130 gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)131 suite.Require().Len(gauges, 1)132 expectedGauge := types.Gauge{133 Id: gaugeID,134 IsPerpetual: true,135 DistributeTo: lockuptypes.QueryCondition{136 LockQueryType: lockuptypes.ByDuration,137 Denom: "lptoken",138 Duration: time.Second,139 },140 Coins: coins,141 NumEpochsPaidOver: 1,142 FilledEpochs: 0,143 DistributedCoins: sdk.Coins{},144 StartTime: startTime,145 }146 suite.Require().Equal(gauges[0].String(), expectedGauge.String())147 // check rewards estimation148 rewardsEst := suite.App.IncentivesKeeper.GetRewardsEst(suite.Ctx, lockOwner, []lockuptypes.PeriodLock{}, 100)149 suite.Require().Equal(coins.String(), rewardsEst.String())150 // check gauges151 gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)152 suite.Require().Len(gauges, 1)153 expectedGauge = types.Gauge{154 Id: gaugeID,155 IsPerpetual: true,156 DistributeTo: lockuptypes.QueryCondition{157 LockQueryType: lockuptypes.ByDuration,158 Denom: "lptoken",159 Duration: time.Second,160 },161 Coins: coins,162 NumEpochsPaidOver: 1,163 FilledEpochs: 0,164 DistributedCoins: sdk.Coins{},165 StartTime: startTime,166 }167 suite.Require().Equal(gauges[0].String(), expectedGauge.String())168 // check upcoming gauges169 gauges = suite.App.IncentivesKeeper.GetUpcomingGauges(suite.Ctx)170 suite.Require().Len(gauges, 1)171 // start distribution172 suite.Ctx = suite.Ctx.WithBlockTime(startTime)173 gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)174 suite.Require().NoError(err)175 err = suite.App.IncentivesKeeper.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge)176 suite.Require().NoError(err)177 // check upcoming gauges178 gauges = suite.App.IncentivesKeeper.GetUpcomingGauges(suite.Ctx)179 suite.Require().Len(gauges, 0)180 // distribute coins to stakers, since it's perpetual distribute everything on single distribution181 distrCoins, err := suite.App.IncentivesKeeper.Distribute(suite.Ctx, []types.Gauge{*gauge})182 suite.Require().NoError(err)183 suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 10)})184 // distributing twice without adding more for perpetual gauge185 gauge, err = suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)186 suite.Require().NoError(err)187 distrCoins, err = suite.App.IncentivesKeeper.Distribute(suite.Ctx, []types.Gauge{*gauge})188 suite.Require().NoError(err)189 suite.Require().True(distrCoins.Empty())190 // add to gauge191 addCoins := sdk.Coins{sdk.NewInt64Coin("stake", 200)}192 suite.AddToGauge(addCoins, gaugeID)193 // distributing twice with adding more for perpetual gauge194 gauge, err = suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)195 suite.Require().NoError(err)196 distrCoins, err = suite.App.IncentivesKeeper.Distribute(suite.Ctx, []types.Gauge{*gauge})197 suite.Require().NoError(err)198 suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 200)})199 // check active gauges200 gauges = suite.App.IncentivesKeeper.GetActiveGauges(suite.Ctx)201 suite.Require().Len(gauges, 1)202 // check gauge ids by denom203 gaugeIds := suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")204 suite.Require().Len(gaugeIds, 1)205 // check finished gauges206 gauges = suite.App.IncentivesKeeper.GetFinishedGauges(suite.Ctx)207 suite.Require().Len(gauges, 0)208 // check rewards estimation209 rewardsEst = suite.App.IncentivesKeeper.GetRewardsEst(suite.Ctx, lockOwner, []lockuptypes.PeriodLock{}, 100)210 suite.Require().Equal(sdk.Coins(nil), rewardsEst)211}212func (suite *KeeperTestSuite) TestGaugesByDenom() {213 // TODO: This is not a good test. We should refactor it to be table driven,214 // specifying a list of gauges to define, and then the expected result of gauges by denom215 testGaugeByDenom := func(isPerpetual bool) {216 // test for module get gauges217 suite.SetupTest()218 // initial module gauges check219 gaugeIds := suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")220 suite.Require().Len(gaugeIds, 0)221 // setup lock and gauge222 _, gaugeID, _, startTime := suite.SetupLockAndGauge(isPerpetual)223 // check gauges224 gaugeIds = suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")225 suite.Require().Len(gaugeIds, 1, "perpetual %b", isPerpetual)226 suite.Require().Equal(gaugeIds[0], gaugeID)227 // start distribution228 suite.Ctx = suite.Ctx.WithBlockTime(startTime)229 gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID)230 suite.Require().NoError(err)231 err = suite.App.IncentivesKeeper.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge)232 suite.Require().NoError(err)233 // check gauge ids by denom234 gaugeIds = suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")235 suite.Require().Len(gaugeIds, 1)236 // check gauge ids by other denom237 gaugeIds = suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lpt")238 suite.Require().Len(gaugeIds, 0)239 // distribute coins to stakers240 _, err = suite.App.IncentivesKeeper.Distribute(suite.Ctx, []types.Gauge{*gauge})241 suite.Require().NoError(err)242 // finish distribution for non perpetual gauge243 if !gauge.IsPerpetual {244 err = suite.App.IncentivesKeeper.MoveActiveGaugeToFinishedGauge(suite.Ctx, *gauge)245 suite.Require().NoError(err)246 }247 expectedNumGauges := 1248 if !isPerpetual {249 expectedNumGauges = 0250 }251 // check gauge ids by denom252 gaugeIds = suite.App.IncentivesKeeper.GetAllGaugeIDsByDenom(suite.Ctx, "lptoken")253 suite.Require().Len(gaugeIds, expectedNumGauges)254 }255 testGaugeByDenom(true)256 testGaugeByDenom(false)257}...
Len
Using AI Code Generation
1import (2type Gauge struct {3}4func (gauge Gauge) Len() int {5}6func main() {7 gauge := Gauge{10}8 fmt.Println(gauge.Len())9}
Len
Using AI Code Generation
1import (2func main() {3 g.Increment()4 g.Increment()5 fmt.Println(g.Len())6}
Len
Using AI Code Generation
1import "fmt"2func main() {3 g := new(Gauge)4 g.Set(5)5 fmt.Println(g.Len())6}7type Gauge struct {8}9func (g *Gauge) Len() int {10}11func (g *Gauge) Set(value int) {12}
Len
Using AI Code Generation
1import (2func main() {3 g := gauge.New(1)4 fmt.Println(stringutil.Reverse("Hello, world."))5 fmt.Println(g.Len())6}
Len
Using AI Code Generation
1import (2type Gauge struct {3}4func (g *Gauge) Len() int {5}6func (g *Gauge) Drops() int {7}8func (g *Gauge) AddDrop() {9}10func (g *Gauge) RemoveDrop() {11}12func (g *Gauge) Add() {13 if g.Len() > g.Drops() {14 g.AddDrop()15 }16}17func (g *Gauge) Remove() {18 if g.Drops() > 0 {19 g.RemoveDrop()20 }21}22func (g *Gauge) Fill() {23 for g.Len() > g.Drops() {24 g.AddDrop()25 }26}27func (g *Gauge) Empty() {28 for g.Drops() > 0 {29 g.RemoveDrop()30 }31}32func (g *Gauge) Rain() {33 rand.Seed(time.Now().UnixNano())34 if rand.Intn(2) == 0 {35 g.Add()36 }37}38func (g *Gauge) Evaporate() {39 rand.Seed(time.Now().UnixNano())40 if rand.Intn(2) == 0 {41 g.Remove()42 }43}44func main() {45 g := &Gauge{10, 0}46 g.Fill()47 fmt.Println("No of drops in the gauge:", g.Drops())48 g.Empty()49 fmt.Println("No of drops in the gauge:", g.Drops())50}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!