File: stopwatch_test.go

package info (click to toggle)
golang-gomega 1.36.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,984 kB
  • sloc: xml: 277; javascript: 59; makefile: 3
file content (66 lines) | stat: -rw-r--r-- 2,141 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
package gmeasure_test

import (
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gmeasure"
)

var _ = Describe("Stopwatch", func() {
	var e *gmeasure.Experiment
	var stopwatch *gmeasure.Stopwatch

	BeforeEach(func() {
		e = gmeasure.NewExperiment("My Test Experiment")
		stopwatch = e.NewStopwatch()
	})

	It("records durations", func() {
		time.Sleep(100 * time.Millisecond)
		stopwatch.Record("recordings", gmeasure.Annotation("A"))
		time.Sleep(100 * time.Millisecond)
		stopwatch.Record("recordings", gmeasure.Annotation("B")).Reset()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Record("recordings", gmeasure.Annotation("C")).Reset()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Pause()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Resume()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Pause()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Resume()
		time.Sleep(100 * time.Millisecond)
		stopwatch.Record("recordings", gmeasure.Annotation("D"))
		durations := e.Get("recordings").Durations
		annotations := e.Get("recordings").Annotations
		Ω(annotations).Should(Equal([]string{"A", "B", "C", "D"}))
		Ω(durations[0]).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
		Ω(durations[1]).Should(BeNumerically("~", 200*time.Millisecond, 50*time.Millisecond))
		Ω(durations[2]).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
		Ω(durations[3]).Should(BeNumerically("~", 300*time.Millisecond, 50*time.Millisecond))

	})

	It("panics when asked to record but not running", func() {
		stopwatch.Pause()
		Ω(func() {
			stopwatch.Record("A")
		}).Should(PanicWith("stopwatch is not running - call Resume or Reset before calling Record"))
	})

	It("panics when paused but not running", func() {
		stopwatch.Pause()
		Ω(func() {
			stopwatch.Pause()
		}).Should(PanicWith("stopwatch is not running - call Resume or Reset before calling Pause"))
	})

	It("panics when asked to resume but not paused", func() {
		Ω(func() {
			stopwatch.Resume()
		}).Should(PanicWith("stopwatch is running - call Pause before calling Resume"))
	})
})