github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/bundle_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package plugins
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestBundle(t *testing.T) {
    26  	bundle := NewBundledStates("merged")
    27  	if gotCount, gotSum := bundle.Total(time.Unix(0, 10)); gotCount != 0 || gotSum != 0 {
    28  		t.Errorf("bundle.Total(time.Unix(0, 10)) = (%d, %d), want (0, 0)", gotCount, gotSum)
    29  	}
    30  	if got := bundle.Percentile(time.Unix(0, 10), 50); got != 0 {
    31  		t.Errorf("bundle.Percentile(time.Unix(0, 10), 50) = %s, want 0", got)
    32  	}
    33  
    34  	// Enable fifty issues, each at 1 minute interval. State changes every time
    35  	for i := 0; i < 50; i++ {
    36  		if !bundle.ReceiveEvent(strconv.Itoa(i), "merged", "", time.Unix(60*int64(i), 0)) {
    37  			t.Error("ReceiveEvent must have triggered a state change.")
    38  		}
    39  	}
    40  
    41  	// we have 50 triggered state
    42  	wantCount := 50
    43  	// Total age at time 5000 is: (50*51)/2
    44  	wantSum := int64(1275)
    45  	if gotCount, gotSum := bundle.Total(time.Unix(50*60, 0)); gotCount != wantCount || gotSum != wantSum {
    46  		t.Errorf("bundle.Total() = (%d, %d), want (%d, %d)", gotCount, gotSum, wantCount, wantSum)
    47  	}
    48  	// The issue in the middle has been opened for 25 minutes
    49  	want := 25 * time.Minute
    50  	if got := bundle.Percentile(time.Unix(50*60, 0), 50); got != want {
    51  		t.Errorf("bundle.Percentile(time.Unix(50*60, 0), 50) = %s, want %s", got, want)
    52  	}
    53  }