github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/internal/utils/timer_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Timer", func() {
    11  	const d = 10 * time.Millisecond
    12  
    13  	It("doesn't fire a newly created timer", func() {
    14  		t := NewTimer()
    15  		Consistently(t.Chan()).ShouldNot(Receive())
    16  	})
    17  
    18  	It("works", func() {
    19  		t := NewTimer()
    20  		t.Reset(time.Now().Add(d))
    21  		Eventually(t.Chan()).Should(Receive())
    22  	})
    23  
    24  	It("returns the deadline", func() {
    25  		t := NewTimer()
    26  		deadline := time.Now().Add(d)
    27  		t.Reset(deadline)
    28  		Expect(t.Deadline()).To(Equal(deadline))
    29  		Eventually(t.Chan()).Should(Receive())
    30  	})
    31  
    32  	It("works multiple times with reading", func() {
    33  		t := NewTimer()
    34  		for i := 0; i < 10; i++ {
    35  			t.Reset(time.Now().Add(d))
    36  			Eventually(t.Chan()).Should(Receive())
    37  			t.SetRead()
    38  		}
    39  	})
    40  
    41  	It("works multiple times without reading", func() {
    42  		t := NewTimer()
    43  		for i := 0; i < 10; i++ {
    44  			t.Reset(time.Now().Add(d))
    45  			time.Sleep(d * 2)
    46  		}
    47  		Eventually(t.Chan()).Should(Receive())
    48  	})
    49  
    50  	It("works when resetting without expiration", func() {
    51  		t := NewTimer()
    52  		for i := 0; i < 10; i++ {
    53  			t.Reset(time.Now().Add(time.Hour))
    54  		}
    55  		t.Reset(time.Now().Add(d))
    56  		Eventually(t.Chan()).Should(Receive())
    57  	})
    58  
    59  	It("immediately fires the timer, if the deadlines has already passed", func() {
    60  		t := NewTimer()
    61  		t.Reset(time.Now().Add(-time.Second))
    62  		Eventually(t.Chan()).Should(Receive())
    63  	})
    64  
    65  	It("doesn't set a timer if the deadline is the zero value", func() {
    66  		t := NewTimer()
    67  		t.Reset(time.Time{})
    68  		Consistently(t.Chan()).ShouldNot(Receive())
    69  	})
    70  
    71  	It("fires the timer twice, if reset to the same deadline", func() {
    72  		deadline := time.Now().Add(-time.Millisecond)
    73  		t := NewTimer()
    74  		t.Reset(deadline)
    75  		Eventually(t.Chan()).Should(Receive())
    76  		t.SetRead()
    77  		t.Reset(deadline)
    78  		Eventually(t.Chan()).Should(Receive())
    79  	})
    80  
    81  	It("only fires the timer once, if it is reset to the same deadline, but not read in between", func() {
    82  		deadline := time.Now().Add(-time.Millisecond)
    83  		t := NewTimer()
    84  		t.Reset(deadline)
    85  		Eventually(t.Chan()).Should(Receive())
    86  		Consistently(t.Chan()).ShouldNot(Receive())
    87  	})
    88  
    89  	It("stops", func() {
    90  		t := NewTimer()
    91  		t.Reset(time.Now().Add(50 * time.Millisecond))
    92  		t.Stop()
    93  		Consistently(t.Chan()).ShouldNot(Receive())
    94  	})
    95  })