github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/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 sinks 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/stretchr/testify/assert" 22 kube_api "k8s.io/api/core/v1" 23 24 "k8s.io/heapster/events/core" 25 "k8s.io/heapster/events/util" 26 ) 27 28 func doThreeBatches(manager core.EventSink) time.Duration { 29 now := time.Now() 30 batch := core.EventBatch{ 31 Timestamp: now, 32 Events: []*kube_api.Event{}, 33 } 34 35 manager.ExportEvents(&batch) 36 manager.ExportEvents(&batch) 37 manager.ExportEvents(&batch) 38 39 elapsed := time.Now().Sub(now) 40 return elapsed 41 } 42 43 func TestAllExportsInTime(t *testing.T) { 44 timeout := 3 * time.Second 45 46 sink1 := util.NewDummySink("s1", time.Second) 47 sink2 := util.NewDummySink("s2", time.Second) 48 manager, _ := NewEventSinkManager([]core.EventSink{sink1, sink2}, timeout, timeout) 49 50 elapsed := doThreeBatches(manager) 51 if elapsed > 2*timeout+2*time.Second { 52 t.Fatalf("3xExportEvents took too long: %s", elapsed) 53 } 54 55 assert.Equal(t, 3, sink1.GetExportCount()) 56 assert.Equal(t, 3, sink2.GetExportCount()) 57 } 58 59 func TestOneExportInTime(t *testing.T) { 60 timeout := 3 * time.Second 61 62 sink1 := util.NewDummySink("s1", time.Second) 63 sink2 := util.NewDummySink("s2", 30*time.Second) 64 manager, _ := NewEventSinkManager([]core.EventSink{sink1, sink2}, timeout, timeout) 65 66 elapsed := doThreeBatches(manager) 67 if elapsed > 2*timeout+2*time.Second { 68 t.Fatalf("3xExportEvents took too long: %s", elapsed) 69 } 70 if elapsed < 2*timeout-1*time.Second { 71 t.Fatalf("3xExportEvents took too short: %s", elapsed) 72 } 73 74 assert.Equal(t, 3, sink1.GetExportCount()) 75 assert.Equal(t, 1, sink2.GetExportCount()) 76 } 77 78 func TestNoExportInTime(t *testing.T) { 79 timeout := 3 * time.Second 80 81 sink1 := util.NewDummySink("s1", 30*time.Second) 82 sink2 := util.NewDummySink("s2", 30*time.Second) 83 manager, _ := NewEventSinkManager([]core.EventSink{sink1, sink2}, timeout, timeout) 84 85 elapsed := doThreeBatches(manager) 86 if elapsed > 2*timeout+2*time.Second { 87 t.Fatalf("3xExportEvents took too long: %s", elapsed) 88 } 89 if elapsed < 2*timeout-1*time.Second { 90 t.Fatalf("3xExportEvents took too short: %s", elapsed) 91 } 92 93 assert.Equal(t, 1, sink1.GetExportCount()) 94 assert.Equal(t, 1, sink2.GetExportCount()) 95 } 96 97 func TestStop(t *testing.T) { 98 timeout := 3 * time.Second 99 100 sink1 := util.NewDummySink("s1", 30*time.Second) 101 sink2 := util.NewDummySink("s2", 30*time.Second) 102 manager, _ := NewEventSinkManager([]core.EventSink{sink1, sink2}, timeout, timeout) 103 104 now := time.Now() 105 manager.Stop() 106 elapsed := time.Now().Sub(now) 107 if elapsed > time.Second { 108 t.Fatalf("stop too long: %s", elapsed) 109 } 110 time.Sleep(time.Second) 111 112 assert.Equal(t, true, sink1.IsStopped()) 113 assert.Equal(t, true, sink2.IsStopped()) 114 }