github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/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  
    23  	"k8s.io/heapster/metrics/core"
    24  	"k8s.io/heapster/metrics/util"
    25  )
    26  
    27  func TestAllExportsInTime(t *testing.T) {
    28  	timeout := 3 * time.Second
    29  
    30  	sink1 := util.NewDummySink("s1", time.Second)
    31  	sink2 := util.NewDummySink("s2", time.Second)
    32  	manager, _ := NewDataSinkManager([]core.DataSink{sink1, sink2}, timeout, timeout)
    33  
    34  	now := time.Now()
    35  	batch := core.DataBatch{
    36  		Timestamp:  now,
    37  		MetricSets: map[string]*core.MetricSet{},
    38  	}
    39  
    40  	manager.ExportData(&batch)
    41  	manager.ExportData(&batch)
    42  	manager.ExportData(&batch)
    43  
    44  	elapsed := time.Now().Sub(now)
    45  	if elapsed > 3*timeout+2*time.Second {
    46  		t.Fatalf("3xExportData took too long: %s", elapsed)
    47  	}
    48  
    49  	time.Sleep(time.Second)
    50  	assert.Equal(t, 3, sink1.GetExportCount())
    51  	assert.Equal(t, 3, sink2.GetExportCount())
    52  }
    53  
    54  func TestOneExportInTime(t *testing.T) {
    55  	timeout := 3 * time.Second
    56  
    57  	sink1 := util.NewDummySink("s1", time.Second)
    58  	sink2 := util.NewDummySink("s2", 30*time.Second)
    59  	manager, _ := NewDataSinkManager([]core.DataSink{sink1, sink2}, timeout, timeout)
    60  
    61  	now := time.Now()
    62  	batch := core.DataBatch{
    63  		Timestamp:  now,
    64  		MetricSets: map[string]*core.MetricSet{},
    65  	}
    66  
    67  	manager.ExportData(&batch)
    68  	manager.ExportData(&batch)
    69  	manager.ExportData(&batch)
    70  
    71  	elapsed := time.Now().Sub(now)
    72  	if elapsed > 2*timeout+2*time.Second {
    73  		t.Fatalf("3xExportData took too long: %s", elapsed)
    74  	}
    75  	if elapsed < 2*timeout-1*time.Second {
    76  		t.Fatalf("3xExportData took too short: %s", elapsed)
    77  	}
    78  
    79  	time.Sleep(time.Second)
    80  	assert.Equal(t, 3, sink1.GetExportCount())
    81  	assert.Equal(t, 1, sink2.GetExportCount())
    82  }
    83  
    84  func TestNoExportInTime(t *testing.T) {
    85  	timeout := 3 * time.Second
    86  
    87  	sink1 := util.NewDummySink("s1", 30*time.Second)
    88  	sink2 := util.NewDummySink("s2", 30*time.Second)
    89  	manager, _ := NewDataSinkManager([]core.DataSink{sink1, sink2}, timeout, timeout)
    90  
    91  	now := time.Now()
    92  	batch := core.DataBatch{
    93  		Timestamp:  now,
    94  		MetricSets: map[string]*core.MetricSet{},
    95  	}
    96  
    97  	manager.ExportData(&batch)
    98  	manager.ExportData(&batch)
    99  	manager.ExportData(&batch)
   100  
   101  	elapsed := time.Now().Sub(now)
   102  	if elapsed > 2*timeout+2*time.Second {
   103  		t.Fatalf("3xExportData took too long: %s", elapsed)
   104  	}
   105  	if elapsed < 2*timeout-1*time.Second {
   106  		t.Fatalf("3xExportData took too short: %s", elapsed)
   107  	}
   108  
   109  	time.Sleep(time.Second)
   110  	assert.Equal(t, 1, sink1.GetExportCount())
   111  	assert.Equal(t, 1, sink2.GetExportCount())
   112  }
   113  
   114  func TestStop(t *testing.T) {
   115  	timeout := 3 * time.Second
   116  
   117  	sink1 := util.NewDummySink("s1", 30*time.Second)
   118  	sink2 := util.NewDummySink("s2", 30*time.Second)
   119  	manager, _ := NewDataSinkManager([]core.DataSink{sink1, sink2}, timeout, timeout)
   120  
   121  	now := time.Now()
   122  	manager.Stop()
   123  	elapsed := time.Now().Sub(now)
   124  	if elapsed > time.Second {
   125  		t.Fatalf("stop too long: %s", elapsed)
   126  	}
   127  	time.Sleep(time.Second)
   128  
   129  	assert.Equal(t, true, sink1.IsStopped())
   130  	assert.Equal(t, true, sink2.IsStopped())
   131  }