github.com/google/cloudprober@v0.11.3/probes/common/statskeeper/statskeeper_test.go (about) 1 // Copyright 2019 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package statskeeper 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/google/cloudprober/metrics" 23 "github.com/google/cloudprober/probes/options" 24 "github.com/google/cloudprober/targets/endpoint" 25 ) 26 27 // probeRunResult captures the results of a single probe run. The way we work with 28 // stats makes sure that probeRunResult and its fields are not accessed concurrently 29 // (see documentation with statsKeeper below). That's the reason we use metrics.Int 30 // types instead of metrics.AtomicInt. 31 type probeRunResult struct { 32 target string 33 sent metrics.Int 34 rcvd metrics.Int 35 rtt metrics.Int // microseconds 36 } 37 38 func newProbeRunResult(target string) probeRunResult { 39 return probeRunResult{ 40 target: target, 41 } 42 } 43 44 // Metrics converts probeRunResult into a map of the metrics that is suitable for 45 // working with metrics.EventMetrics. 46 func (prr probeRunResult) Metrics() *metrics.EventMetrics { 47 return metrics.NewEventMetrics(time.Now()). 48 AddMetric("sent", &prr.sent). 49 AddMetric("rcvd", &prr.rcvd). 50 AddMetric("rtt", &prr.rtt) 51 } 52 53 // Target returns the p.target. 54 func (prr probeRunResult) Target() string { 55 return prr.target 56 } 57 58 func TestStatsKeeper(t *testing.T) { 59 targets := []endpoint.Endpoint{ 60 {Name: "target1"}, 61 {Name: "target2"}, 62 } 63 pType := "test" 64 pName := "testProbe" 65 exportInterval := 2 * time.Second 66 67 resultsChan := make(chan ProbeResult, len(targets)) 68 ctx, cancelFunc := context.WithCancel(context.Background()) 69 defer cancelFunc() 70 71 targetsFunc := func() []endpoint.Endpoint { 72 return targets 73 } 74 dataChan := make(chan *metrics.EventMetrics, len(targets)) 75 76 opts := &options.Options{ 77 StatsExportInterval: exportInterval, 78 } 79 go StatsKeeper(ctx, pType, pName, opts, targetsFunc, resultsChan, dataChan) 80 81 for _, target := range targets { 82 prr := newProbeRunResult(target.Name) 83 prr.sent.Inc() 84 prr.rcvd.Inc() 85 prr.rtt.IncBy(metrics.NewInt(20000)) 86 resultsChan <- prr 87 } 88 time.Sleep(3 * time.Second) 89 90 for i := 0; i < len(dataChan); i++ { 91 em := <-dataChan 92 var foundTarget bool 93 for _, target := range targets { 94 if em.Label("dst") == target.Name { 95 foundTarget = true 96 break 97 } 98 } 99 if !foundTarget { 100 t.Error("didn't get expected target label in the event metric") 101 } 102 expectedValues := map[string]int64{ 103 "sent": 1, 104 "rcvd": 1, 105 "rtt": 20000, 106 } 107 for key, eVal := range expectedValues { 108 val := em.Metric(key).(metrics.NumValue).Int64() 109 if val != eVal { 110 t.Errorf("%s metric is not set correctly. Got: %d, Expected: %d", key, val, eVal) 111 } 112 } 113 } 114 }