github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sources/manager_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 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 sources 16 17 import ( 18 "testing" 19 "time" 20 21 "k8s.io/heapster/metrics/util" 22 ) 23 24 func TestAllSourcesReplyInTime(t *testing.T) { 25 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 26 util.NewDummyMetricsSource("s1", time.Second), 27 util.NewDummyMetricsSource("s2", time.Second)) 28 29 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 30 now := time.Now() 31 end := now.Truncate(10 * time.Second) 32 dataBatch := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 33 34 elapsed := time.Now().Sub(now) 35 if elapsed > 3*time.Second { 36 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 37 } 38 39 present := make(map[string]bool) 40 for key := range dataBatch.MetricSets { 41 present[key] = true 42 } 43 44 if _, ok := present["s1"]; !ok { 45 t.Fatal("s1 not found") 46 } 47 48 if _, ok := present["s2"]; !ok { 49 t.Fatal("s2 not found") 50 } 51 } 52 53 func TestOneSourcesReplyInTime(t *testing.T) { 54 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 55 util.NewDummyMetricsSource("s1", time.Second), 56 util.NewDummyMetricsSource("s2", 30*time.Second)) 57 58 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 59 now := time.Now() 60 end := now.Truncate(10 * time.Second) 61 dataBatch := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 62 elapsed := time.Now().Sub(now) 63 64 if elapsed > 4*time.Second { 65 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 66 } 67 68 if elapsed < 2*time.Second { 69 t.Fatalf("ScrapeMetrics took too short: %s", elapsed) 70 } 71 72 present := make(map[string]bool) 73 for key := range dataBatch.MetricSets { 74 present[key] = true 75 } 76 77 if _, ok := present["s1"]; !ok { 78 t.Fatal("s1 not found") 79 } 80 81 if _, ok := present["s2"]; ok { 82 t.Fatal("s2 found") 83 } 84 } 85 86 func TestNoSourcesReplyInTime(t *testing.T) { 87 metricsSourceProvider := util.NewDummyMetricsSourceProvider( 88 util.NewDummyMetricsSource("s1", 30*time.Second), 89 util.NewDummyMetricsSource("s2", 30*time.Second)) 90 91 manager, _ := NewSourceManager(metricsSourceProvider, time.Second*3) 92 now := time.Now() 93 end := now.Truncate(10 * time.Second) 94 dataBatch := manager.ScrapeMetrics(end.Add(-10*time.Second), end) 95 elapsed := time.Now().Sub(now) 96 97 if elapsed > 4*time.Second { 98 t.Fatalf("ScrapeMetrics took too long: %s", elapsed) 99 } 100 101 if elapsed < 2*time.Second { 102 t.Fatalf("ScrapeMetrics took too short: %s", elapsed) 103 } 104 105 present := make(map[string]bool) 106 for key := range dataBatch.MetricSets { 107 present[key] = true 108 } 109 110 if _, ok := present["s1"]; ok { 111 t.Fatal("s1 found") 112 } 113 114 if _, ok := present["s2"]; ok { 115 t.Fatal("s2 found") 116 } 117 }