88 "time"
99
1010 "github.com/pingcap/tiproxy/lib/util/errors"
11+ "github.com/pingcap/tiproxy/pkg/sqlreplay/capture"
12+ "github.com/pingcap/tiproxy/pkg/sqlreplay/replay"
1113 "github.com/stretchr/testify/require"
1214)
1315
@@ -20,8 +22,7 @@ func TestIsRunning(t *testing.T) {
2022 {
2123 job : & captureJob {
2224 job : job {
23- StartTime : time .Now (),
24- Duration : 10 * time .Second ,
25+ startTime : time .Now (),
2526 },
2627 },
2728 tp : Capture ,
@@ -30,8 +31,7 @@ func TestIsRunning(t *testing.T) {
3031 {
3132 job : & replayJob {
3233 job : job {
33- StartTime : time .Now (),
34- Duration : 10 * time .Second ,
34+ startTime : time .Now (),
3535 },
3636 },
3737 tp : Replay ,
@@ -40,10 +40,9 @@ func TestIsRunning(t *testing.T) {
4040 {
4141 job : & captureJob {
4242 job : job {
43- StartTime : time .Now ().Add (- 5 * time .Second ),
44- Duration : 10 * time .Second ,
45- Progress : 0.5 ,
46- Error : errors .New ("stopped manually" ),
43+ startTime : time .Now ().Add (- 5 * time .Second ),
44+ progress : 0.5 ,
45+ err : errors .New ("stopped manually" ),
4746 },
4847 },
4948 tp : Capture ,
@@ -52,9 +51,8 @@ func TestIsRunning(t *testing.T) {
5251 {
5352 job : & replayJob {
5453 job : job {
55- StartTime : time .Now ().Add (- 20 * time .Second ),
56- Duration : 10 * time .Second ,
57- Progress : 1.0 ,
54+ startTime : time .Now ().Add (- 20 * time .Second ),
55+ progress : 1.0 ,
5856 },
5957 },
6058 tp : Replay ,
@@ -93,12 +91,73 @@ func TestSetProgress(t *testing.T) {
9391 for i , test := range tests {
9492 job := & captureJob {
9593 job : job {
96- StartTime : time .Now (),
97- Duration : 10 * time .Second ,
94+ startTime : time .Now (),
9895 },
9996 }
100- job .SetProgress (test .progress , test .err )
101- require .Equal (t , test .expectedProgress , job .Progress , "case %d" , i )
97+ now := time .Now ()
98+ job .SetProgress (test .progress , now , test .err )
99+ require .Equal (t , now , job .endTime , "case %d" , i )
100+ require .Equal (t , test .expectedProgress , job .progress , "case %d" , i )
102101 require .Equal (t , test .running , job .IsRunning (), "case %d" , i )
103102 }
104103}
104+
105+ func TestMarshalJob (t * testing.T ) {
106+ startTime , err := time .Parse ("2006-01-02 15:04:05" , "2020-01-01 00:00:00" )
107+ require .NoError (t , err )
108+ endTime , err := time .Parse ("2006-01-02 15:04:05" , "2020-01-01 02:01:01" )
109+ require .NoError (t , err )
110+
111+ tests := []struct {
112+ job Job
113+ marshal string
114+ }{
115+ {
116+ job : & captureJob {
117+ job : job {
118+ startTime : startTime ,
119+ endTime : endTime ,
120+ progress : 0.5 ,
121+ err : errors .New ("mock error" ),
122+ },
123+ cfg : capture.CaptureConfig {
124+ Output : "/tmp/traffic" ,
125+ Duration : 2 * time .Hour ,
126+ },
127+ },
128+ marshal : `{"type":"capture","status":"canceled","start_time":"2020-01-01 00:00:00 +0000 UTC","end_time":"2020-01-01 02:01:01 +0000 UTC","duration":"2h0m0s","output":"/tmp/traffic","progress":"50%","error":"mock error"}` ,
129+ },
130+ {
131+ job : & replayJob {
132+ job : job {
133+ startTime : startTime ,
134+ progress : 0 ,
135+ },
136+ cfg : replay.ReplayConfig {
137+ Input : "/tmp/traffic" ,
138+ Username : "root" ,
139+ },
140+ },
141+ marshal : `{"type":"replay","status":"running","start_time":"2020-01-01 00:00:00 +0000 UTC","input":"/tmp/traffic","username":"root","speed":1,"progress":"0%"}` ,
142+ },
143+ {
144+ job : & replayJob {
145+ job : job {
146+ startTime : startTime ,
147+ endTime : endTime ,
148+ progress : 1 ,
149+ },
150+ cfg : replay.ReplayConfig {
151+ Input : "/tmp/traffic" ,
152+ Username : "root" ,
153+ Speed : 0.5 ,
154+ },
155+ },
156+ marshal : `{"type":"replay","status":"done","start_time":"2020-01-01 00:00:00 +0000 UTC","end_time":"2020-01-01 02:01:01 +0000 UTC","input":"/tmp/traffic","username":"root","speed":0.5,"progress":"100%"}` ,
157+ },
158+ }
159+
160+ for i , test := range tests {
161+ require .Equal (t , test .marshal , test .job .String (), "case %d" , i )
162+ }
163+ }
0 commit comments