File: sometimes_test.go

package info (click to toggle)
golang-golang-x-time 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 116 kB
  • sloc: makefile: 2
file content (94 lines) | stat: -rw-r--r-- 1,991 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rate_test

import (
	"fmt"
	"math"
	"testing"
	"time"

	"golang.org/x/time/rate"
)

func ExampleSometimes_once() {
	// The zero value of Sometimes behaves like sync.Once, though less efficiently.
	var s rate.Sometimes
	s.Do(func() { fmt.Println("1") })
	s.Do(func() { fmt.Println("2") })
	s.Do(func() { fmt.Println("3") })
	// Output:
	// 1
}

func ExampleSometimes_first() {
	s := rate.Sometimes{First: 2}
	s.Do(func() { fmt.Println("1") })
	s.Do(func() { fmt.Println("2") })
	s.Do(func() { fmt.Println("3") })
	// Output:
	// 1
	// 2
}

func ExampleSometimes_every() {
	s := rate.Sometimes{Every: 2}
	s.Do(func() { fmt.Println("1") })
	s.Do(func() { fmt.Println("2") })
	s.Do(func() { fmt.Println("3") })
	// Output:
	// 1
	// 3
}

func ExampleSometimes_interval() {
	s := rate.Sometimes{Interval: 1 * time.Second}
	s.Do(func() { fmt.Println("1") })
	s.Do(func() { fmt.Println("2") })
	time.Sleep(1 * time.Second)
	s.Do(func() { fmt.Println("3") })
	// Output:
	// 1
	// 3
}

func ExampleSometimes_mix() {
	s := rate.Sometimes{
		First:    2,
		Every:    2,
		Interval: 2 * time.Second,
	}
	s.Do(func() { fmt.Println("1 (First:2)") })
	s.Do(func() { fmt.Println("2 (First:2)") })
	s.Do(func() { fmt.Println("3 (Every:2)") })
	time.Sleep(2 * time.Second)
	s.Do(func() { fmt.Println("4 (Interval)") })
	s.Do(func() { fmt.Println("5 (Every:2)") })
	s.Do(func() { fmt.Println("6") })
	// Output:
	// 1 (First:2)
	// 2 (First:2)
	// 3 (Every:2)
	// 4 (Interval)
	// 5 (Every:2)
}

func TestSometimesZero(t *testing.T) {
	s := rate.Sometimes{Interval: 0}
	s.Do(func() {})
	s.Do(func() {})
}

func TestSometimesMax(t *testing.T) {
	s := rate.Sometimes{Interval: math.MaxInt64}
	s.Do(func() {})
	s.Do(func() {})
}

func TestSometimesNegative(t *testing.T) {
	s := rate.Sometimes{Interval: -1}
	s.Do(func() {})
	s.Do(func() {})
}