@@ -11,16 +11,15 @@ import (
1111 "time"
1212
1313 "github.com/pingcap/tiproxy/lib/util/errors"
14- "github.com/pingcap/tiproxy/lib/util/logger"
1514 "github.com/pingcap/tiproxy/lib/util/waitgroup"
1615 pnet "github.com/pingcap/tiproxy/pkg/proxy/net"
1716 "github.com/pingcap/tiproxy/pkg/sqlreplay/store"
1817 "github.com/stretchr/testify/require"
18+ "go.uber.org/zap"
1919)
2020
2121func TestStartAndStop (t * testing.T ) {
22- lg , _ := logger .CreateLoggerForTest (t )
23- cpt := NewCapture (lg )
22+ cpt := NewCapture (zap .NewNop ())
2423 defer cpt .Close ()
2524
2625 packet := append ([]byte {pnet .ComQuery .Byte ()}, []byte ("select 1" )... )
@@ -35,15 +34,12 @@ func TestStartAndStop(t *testing.T) {
3534 // start capture and the traffic should be outputted
3635 require .NoError (t , cpt .Start (cfg ))
3736 cpt .Capture (packet , time .Now (), 100 )
38- _ , err := cpt .Progress ()
39- require .NoError (t , err )
4037 cpt .Stop (errors .Errorf ("mock error" ))
41- _ , err = cpt .Progress ()
42- require .ErrorContains (t , err , "mock error" )
4338 cpt .wg .Wait ()
4439 data := writer .getData ()
4540 require .Greater (t , len (data ), 0 )
4641 require .Contains (t , string (data ), "select 1" )
42+ require .Equal (t , uint64 (1 ), cpt .capturedCmds )
4743
4844 // stop capture and traffic should not be outputted
4945 cpt .Capture (packet , time .Now (), 100 )
@@ -65,8 +61,7 @@ func TestStartAndStop(t *testing.T) {
6561}
6662
6763func TestConcurrency (t * testing.T ) {
68- lg , _ := logger .CreateLoggerForTest (t )
69- cpt := NewCapture (lg )
64+ cpt := NewCapture (zap .NewNop ())
7065 defer cpt .Close ()
7166
7267 writer := newMockWriter (store.WriterCfg {})
@@ -145,3 +140,38 @@ func TestCaptureCfgError(t *testing.T) {
145140 require .Equal (t , maxBuffers , cfg .maxBuffers )
146141 require .Equal (t , maxPendingCommands , cfg .maxPendingCommands )
147142}
143+
144+ func TestProgress (t * testing.T ) {
145+ cpt := NewCapture (zap .NewNop ())
146+ defer cpt .Close ()
147+
148+ writer := newMockWriter (store.WriterCfg {})
149+ cfg := CaptureConfig {
150+ Output : t .TempDir (),
151+ Duration : 10 * time .Second ,
152+ cmdLogger : writer ,
153+ }
154+ setStartTime := func (t time.Time ) {
155+ cpt .Lock ()
156+ cpt .startTime = t
157+ cpt .Unlock ()
158+ }
159+
160+ now := time .Now ()
161+ require .NoError (t , cpt .Start (cfg ))
162+ progress , err := cpt .Progress ()
163+ require .NoError (t , err )
164+ require .Less (t , progress , 0.3 )
165+
166+ setStartTime (now .Add (- 5 * time .Second ))
167+ progress , err = cpt .Progress ()
168+ require .NoError (t , err )
169+ require .GreaterOrEqual (t , progress , 0.5 )
170+
171+ cpt .Stop (errors .Errorf ("mock error" ))
172+ cpt .wg .Wait ()
173+ progress , err = cpt .Progress ()
174+ require .ErrorContains (t , err , "mock error" )
175+ require .GreaterOrEqual (t , progress , 0.5 )
176+ require .Less (t , progress , 1.0 )
177+ }
0 commit comments