Skip to content

Commit 5cd9806

Browse files
improve tests
Signed-off-by: Tim Vaillancourt <[email protected]>
1 parent e3d49ce commit 5cd9806

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

doc/interactive-commands.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Both interfaces may serve at the same time. Both respond to simple text command,
1717
- `help`: shows a brief list of available commands
1818
- `status`: returns a detailed status summary of migration progress and configuration
1919
- `sup`: returns a brief status summary of migration progress
20-
- `cpu-profile`: returns a base64-encoded runtime/pprof CPU profile with options, default '30s'. Comma-separated options 'gzip' and/or 'blocking' (blocking profile) may follow a profile duration
20+
- `cpu-profile`: returns a base64-encoded runtime/pprof CPU profile using a duration, default: `30s`. Comma-separated options `gzip` and/or `block` (blocked profile) may follow the profile duration
2121
- `coordinates`: returns recent (though not exactly up to date) binary log coordinates of the inspected server
2222
- `applier`: returns the hostname of the applier
2323
- `inspector`: returns the hostname of the inspector

go/logic/server.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ import (
2525
"github.com/github/gh-ost/go/base"
2626
)
2727

28-
const defaultCPUProfileDuration = time.Second * 30
29-
3028
var (
3129
ErrCPUProfilingBadOption = errors.New("unrecognized cpu profiling option")
3230
ErrCPUProfilingInProgress = errors.New("cpu profiling already in progress")
31+
defaultCPUProfileDuration = time.Second * 30
3332
)
3433

3534
type printStatusFunc func(PrintStatusRule, io.Writer)
@@ -63,15 +62,17 @@ func (this *Server) runCPUProfile(args string) (string, error) {
6362
var useGzip bool
6463
if args != "" {
6564
s := strings.Split(args, ",")
65+
// a duration string must be the 1st field, if any
6666
if duration, err = time.ParseDuration(s[0]); err != nil {
6767
return "", err
6868
}
6969
for _, arg := range s[1:] {
7070
switch arg {
71-
case "gzip":
72-
useGzip = true
7371
case "block", "blocked", "blocking":
7472
runtime.SetBlockProfileRate(1)
73+
defer runtime.SetBlockProfileRate(0)
74+
case "gzip":
75+
useGzip = true
7576
default:
7677
return "", ErrCPUProfilingBadOption
7778
}
@@ -82,8 +83,7 @@ func (this *Server) runCPUProfile(args string) (string, error) {
8283
defer atomic.StoreInt64(&this.isCPUProfiling, 0)
8384

8485
var buf bytes.Buffer
85-
var writer io.Writer
86-
writer = &buf
86+
var writer io.Writer = &buf
8787
if useGzip {
8888
writer = gzip.NewWriter(&buf)
8989
}
@@ -93,7 +93,7 @@ func (this *Server) runCPUProfile(args string) (string, error) {
9393

9494
time.Sleep(duration)
9595
pprof.StopCPUProfile()
96-
this.migrationContext.Log.Infof("Captured %d byte runtime/pprof CPU profile", buf.Len())
96+
this.migrationContext.Log.Infof("Captured %d byte runtime/pprof CPU profile (gzip=%v)", buf.Len(), useGzip)
9797

9898
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
9999
}
@@ -205,7 +205,7 @@ func (this *Server) applyServerCommand(command string, writer *bufio.Writer) (pr
205205
fmt.Fprint(writer, `available commands:
206206
status # Print a detailed status message
207207
sup # Print a short status message
208-
cpu-profile=<options> # Print a base64-encoded runtime/pprof CPU profile with options, default '30s'. Comma-separated options 'gzip' and/or 'blocking' (blocking profile) may follow a profile duration
208+
cpu-profile=<options> # Print a base64-encoded runtime/pprof CPU profile using a duration, default: 30s. Comma-separated options 'gzip' and/or 'block' (blocked profile) may follow the profile duration
209209
coordinates # Print the currently inspected coordinates
210210
applier # Print the hostname of the applier
211211
inspector # Print the hostname of the inspector

go/logic/server_test.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package logic
33
import (
44
"encoding/base64"
55
"testing"
6+
"time"
67

78
"github.com/github/gh-ost/go/base"
89
"github.com/openark/golib/tests"
@@ -11,25 +12,22 @@ import (
1112
func TestServerRunCPUProfile(t *testing.T) {
1213
t.Parallel()
1314

14-
t.Run("failed bad arg", func(t *testing.T) {
15-
s := &Server{isCPUProfiling: 0}
16-
profile, err := s.runCPUProfile("should-fail")
17-
tests.S(t).ExpectNotNil(err)
18-
tests.S(t).ExpectEquals(profile, "")
19-
})
20-
2115
t.Run("failed already running", func(t *testing.T) {
2216
s := &Server{isCPUProfiling: 1}
2317
profile, err := s.runCPUProfile("15ms")
2418
tests.S(t).ExpectEquals(err, ErrCPUProfilingInProgress)
2519
tests.S(t).ExpectEquals(profile, "")
2620
})
2721

28-
t.Run("failed with bad option", func(t *testing.T) {
29-
s := &Server{
30-
isCPUProfiling: 0,
31-
migrationContext: base.NewMigrationContext(),
32-
}
22+
t.Run("failed bad duration", func(t *testing.T) {
23+
s := &Server{isCPUProfiling: 0}
24+
profile, err := s.runCPUProfile("should-fail")
25+
tests.S(t).ExpectNotNil(err)
26+
tests.S(t).ExpectEquals(profile, "")
27+
})
28+
29+
t.Run("failed bad option", func(t *testing.T) {
30+
s := &Server{isCPUProfiling: 0}
3331
profile, err := s.runCPUProfile("10ms,badoption")
3432
tests.S(t).ExpectEquals(err, ErrCPUProfilingBadOption)
3533
tests.S(t).ExpectEquals(profile, "")
@@ -40,7 +38,8 @@ func TestServerRunCPUProfile(t *testing.T) {
4038
isCPUProfiling: 0,
4139
migrationContext: base.NewMigrationContext(),
4240
}
43-
profile, err := s.runCPUProfile("10ms")
41+
defaultCPUProfileDuration = time.Millisecond * 10
42+
profile, err := s.runCPUProfile("")
4443
tests.S(t).ExpectNil(err)
4544
tests.S(t).ExpectNotEquals(profile, "")
4645

0 commit comments

Comments
 (0)