github.phpd.cn/cilium/cilium@v1.6.12/pkg/trigger/trigger_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     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  // +build !privileged_tests
    16  
    17  package trigger
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/cilium/cilium/pkg/lock"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  func Test(t *testing.T) {
    30  	TestingT(t)
    31  }
    32  
    33  type TriggerTestSuite struct{}
    34  
    35  var _ = Suite(&TriggerTestSuite{})
    36  
    37  func (s *TriggerTestSuite) TestNeedsDelay(c *C) {
    38  	t := &Trigger{params: Parameters{}}
    39  
    40  	needsDelay, _ := t.needsDelay()
    41  	c.Assert(needsDelay, Equals, false)
    42  
    43  	t.params.MinInterval = time.Second
    44  
    45  	t.lastTrigger = time.Now().Add(time.Second * -2)
    46  	needsDelay, _ = t.needsDelay()
    47  	c.Assert(needsDelay, Equals, false)
    48  
    49  	t.lastTrigger = time.Now().Add(time.Millisecond * -900)
    50  	needsDelay, _ = t.needsDelay()
    51  	c.Assert(needsDelay, Equals, true)
    52  	time.Sleep(time.Millisecond * 200)
    53  	needsDelay, _ = t.needsDelay()
    54  	c.Assert(needsDelay, Equals, false)
    55  }
    56  
    57  // TestMinInterval ensures that the MinInterval parameter is being respected
    58  func (s *TriggerTestSuite) TestMinInterval(c *C) {
    59  	var (
    60  		mutex     lock.Mutex
    61  		triggered int
    62  	)
    63  
    64  	t, err := NewTrigger(Parameters{
    65  		TriggerFunc: func(reasons []string) {
    66  			mutex.Lock()
    67  			triggered++
    68  			mutex.Unlock()
    69  		},
    70  		MinInterval:   time.Millisecond * 500,
    71  		sleepInterval: time.Millisecond,
    72  	})
    73  	c.Assert(err, IsNil)
    74  	c.Assert(t, Not(IsNil))
    75  
    76  	// Trigger multiple times and sleep in between to guarantee that the
    77  	// background routine probed in the meantime
    78  	for i := 0; i < 5; i++ {
    79  		t.Trigger()
    80  		time.Sleep(time.Millisecond * 20)
    81  	}
    82  
    83  	mutex.Lock()
    84  	triggeredCopy := triggered
    85  	mutex.Unlock()
    86  	c.Assert(triggeredCopy, Equals, 1)
    87  
    88  	t.Shutdown()
    89  }
    90  
    91  // TestLongTrigger tests that a trigger that takes a second is only invoked
    92  // once even though triggers are occurring in the background
    93  func (s *TriggerTestSuite) TestLongTrigger(c *C) {
    94  	var (
    95  		mutex     lock.Mutex
    96  		triggered int
    97  	)
    98  
    99  	t, err := NewTrigger(Parameters{
   100  		TriggerFunc: func(reasons []string) {
   101  			mutex.Lock()
   102  			triggered++
   103  			mutex.Unlock()
   104  			time.Sleep(time.Second)
   105  		},
   106  		sleepInterval: time.Millisecond,
   107  	})
   108  	c.Assert(err, IsNil)
   109  	c.Assert(t, Not(IsNil))
   110  
   111  	// Trigger multiple times and sleep in between to guarantee that the
   112  	// background routine probed in the meantime
   113  	for i := 0; i < 5; i++ {
   114  		t.Trigger()
   115  		time.Sleep(time.Millisecond * 20)
   116  	}
   117  
   118  	mutex.Lock()
   119  	triggeredCopy := triggered
   120  	mutex.Unlock()
   121  	c.Assert(triggeredCopy, Equals, 1)
   122  
   123  	t.Shutdown()
   124  }