1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package time
17
18 import (
19 "os"
20 "testing"
21
22 . "github.com/onsi/ginkgo/v2"
23 . "github.com/onsi/gomega"
24
25 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/tasks"
26 "github.com/chaos-mesh/chaos-mesh/pkg/log"
27 "github.com/chaos-mesh/chaos-mesh/test/pkg/timer"
28 )
29
30
31
32
33 func TestModifyTime(t *testing.T) {
34 RegisterFailHandler(Fail)
35
36 RunSpecs(t, "Time Suit")
37 }
38
39 var _ = BeforeSuite(func(done Done) {
40 By("change working directory")
41
42 err := os.Chdir("../../")
43 Expect(err).NotTo(HaveOccurred())
44
45 close(done)
46 })
47
48
49
50
51 var _ = Describe("ModifyTime", func() {
52 var t *timer.Timer
53 logger, err := log.NewDefaultZapLogger()
54 Expect(err).ShouldNot(HaveOccurred())
55 BeforeEach(func() {
56 var err error
57
58 t, err = timer.StartTimer()
59 Expect(err).ShouldNot(HaveOccurred())
60 })
61
62 AfterEach(func() {
63 err := t.Stop()
64 Expect(err).ShouldNot(HaveOccurred())
65 })
66
67 Context("Modify Time", func() {
68 It("should move forward successfully", func() {
69
70 Expect(t).NotTo(BeNil())
71 s, err := GetSkew(logger, NewConfig(10000, 0, 1))
72 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
73
74 now, err := t.GetTime()
75 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
76
77 sec := now.Unix()
78
79 err = s.Inject(tasks.SysPID(t.Pid()))
80 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
81
82 newTime, err := t.GetTime()
83 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
84
85 newSec := newTime.Unix()
86
87 Expect(newSec-sec).Should(BeNumerically(">=", 10000), "sec %d newSec %d", sec, newSec)
88 Expect(newSec-sec).Should(BeNumerically("<=", 10010), "sec %d newSec %d", sec, newSec)
89 })
90
91 It("should move backward successfully", func() {
92 Expect(t).NotTo(BeNil())
93 s, err := GetSkew(logger, NewConfig(10000, 0, 1))
94 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
95
96 err = s.Inject(tasks.SysPID(t.Pid()))
97 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
98
99 now, err := t.GetTime()
100 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
101
102 sec := now.Unix()
103
104 err = s.Recover(tasks.SysPID(t.Pid()))
105 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
106
107 newTime, err := t.GetTime()
108 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
109
110 newSec := newTime.Unix()
111 Expect(10000-(sec-newSec)).Should(BeNumerically("<=", 1), "sec %d newSec %d", sec, newSec)
112 })
113
114 It("should handle nsec overflow", func() {
115 Expect(t).NotTo(BeNil())
116
117 s, err := GetSkew(logger, NewConfig(0, 1000000000, 1))
118 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
119
120 now, err := t.GetTime()
121 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
122
123 sec := now.Unix()
124
125 err = s.Inject(tasks.SysPID(t.Pid()))
126 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
127
128 newTime, err := t.GetTime()
129 Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
130
131 newSec := newTime.Unix()
132 Expect(newSec-sec).Should(BeNumerically(">=", 1), "sec %d newSec %d", sec, newSec)
133 })
134 })
135 })
136