github.com/google/cadvisor@v0.49.1/summary/buffer_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 summary 16 17 import ( 18 "reflect" 19 "testing" 20 21 info "github.com/google/cadvisor/info/v2" 22 ) 23 24 func createSample(i uint64) info.Usage { 25 usage := info.Usage{} 26 usage.PercentComplete = 100 27 usage.Cpu = info.Percentiles{ 28 Present: true, 29 Mean: i * 50, 30 Max: i * 100, 31 Ninety: i * 90, 32 } 33 usage.Memory = info.Percentiles{ 34 Present: true, 35 Mean: i * 50 * 1024, 36 Max: i * 100 * 1024, 37 Ninety: i * 90 * 1024, 38 } 39 return usage 40 } 41 42 func expectSize(t *testing.T, b *SamplesBuffer, expectedSize int) { 43 if b.Size() != expectedSize { 44 t.Errorf("Expected size %d, got %d", expectedSize, b.Size()) 45 } 46 } 47 48 func expectElements(t *testing.T, b *SamplesBuffer, expected []info.Usage) { 49 50 out := b.RecentStats(b.Size()) 51 if len(out) != len(expected) { 52 t.Errorf("Expected %d elements, got %d", len(expected), len(out)) 53 } 54 for i, el := range out { 55 if !reflect.DeepEqual(*el, expected[i]) { 56 t.Errorf("Expected elements %v, got %v", expected[i], *el) 57 } 58 } 59 } 60 61 func TestEmpty(t *testing.T) { 62 b := NewSamplesBuffer(5) 63 expectSize(t, b, 0) 64 expectElements(t, b, []info.Usage{}) 65 } 66 67 func TestAddSingleSample(t *testing.T) { 68 b := NewSamplesBuffer(5) 69 70 sample := createSample(1) 71 b.Add(sample) 72 expectSize(t, b, 1) 73 expectElements(t, b, []info.Usage{sample}) 74 } 75 76 func TestFullBuffer(t *testing.T) { 77 maxSize := 5 78 b := NewSamplesBuffer(maxSize) 79 samples := []info.Usage{} 80 for i := 0; i < maxSize; i++ { 81 sample := createSample(uint64(i)) 82 samples = append(samples, sample) 83 b.Add(sample) 84 } 85 expectSize(t, b, maxSize) 86 expectElements(t, b, samples) 87 } 88 89 func TestOverflow(t *testing.T) { 90 maxSize := 5 91 overflow := 2 92 b := NewSamplesBuffer(maxSize) 93 samples := []info.Usage{} 94 for i := 0; i < maxSize+overflow; i++ { 95 sample := createSample(uint64(i)) 96 if i >= overflow { 97 samples = append(samples, sample) 98 } 99 b.Add(sample) 100 } 101 expectSize(t, b, maxSize) 102 expectElements(t, b, samples) 103 } 104 105 func TestReplaceAll(t *testing.T) { 106 maxSize := 5 107 b := NewSamplesBuffer(maxSize) 108 samples := []info.Usage{} 109 for i := 0; i < maxSize*2; i++ { 110 sample := createSample(uint64(i)) 111 if i >= maxSize { 112 samples = append(samples, sample) 113 } 114 b.Add(sample) 115 } 116 expectSize(t, b, maxSize) 117 expectElements(t, b, samples) 118 }