1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/lsp/protocol"
16 type fakeClient struct {
19 token protocol.ProgressToken
22 created, begun, reported, messages, ended int
25 func (c *fakeClient) checkToken(token protocol.ProgressToken) {
27 panic("nil token in progress message")
29 if c.token != nil && c.token != token {
30 panic(fmt.Errorf("invalid token in progress message: got %v, want %v", token, c.token))
34 func (c *fakeClient) WorkDoneProgressCreate(ctx context.Context, params *protocol.WorkDoneProgressCreateParams) error {
37 c.checkToken(params.Token)
42 func (c *fakeClient) Progress(ctx context.Context, params *protocol.ProgressParams) error {
45 c.checkToken(params.Token)
46 switch params.Value.(type) {
47 case *protocol.WorkDoneProgressBegin:
49 case *protocol.WorkDoneProgressReport:
51 case *protocol.WorkDoneProgressEnd:
54 panic(fmt.Errorf("unknown progress value %T", params.Value))
59 func (c *fakeClient) ShowMessage(context.Context, *protocol.ShowMessageParams) error {
66 func setup(token protocol.ProgressToken) (context.Context, *progressTracker, *fakeClient) {
68 tracker := newProgressTracker(c)
69 tracker.supportsWorkDoneProgress = true
70 return context.Background(), tracker, c
73 func TestProgressTracker_Reporting(t *testing.T) {
74 for _, test := range []struct {
77 token protocol.ProgressToken
78 wantReported, wantCreated, wantBegun, wantEnded int
102 name: "numeric token",
111 t.Run(test.name, func(t *testing.T) {
112 ctx, tracker, client := setup(test.token)
113 ctx, cancel := context.WithCancel(ctx)
115 tracker.supportsWorkDoneProgress = test.supported
116 work := tracker.start(ctx, "work", "message", test.token, nil)
118 gotCreated, gotBegun := client.created, client.begun
120 if gotCreated != test.wantCreated {
121 t.Errorf("got %d created tokens, want %d", gotCreated, test.wantCreated)
123 if gotBegun != test.wantBegun {
124 t.Errorf("got %d work begun, want %d", gotBegun, test.wantBegun)
126 // Ignore errors: this is just testing the reporting behavior.
127 work.report("report", 50)
129 gotReported := client.reported
131 if gotReported != test.wantReported {
132 t.Errorf("got %d progress reports, want %d", gotReported, test.wantCreated)
136 gotEnded, gotMessages := client.ended, client.messages
138 if gotEnded != test.wantEnded {
139 t.Errorf("got %d ended reports, want %d", gotEnded, test.wantEnded)
141 if gotMessages != test.wantMessages {
142 t.Errorf("got %d messages, want %d", gotMessages, test.wantMessages)
148 func TestProgressTracker_Cancellation(t *testing.T) {
149 for _, token := range []protocol.ProgressToken{nil, 1, "a"} {
150 ctx, tracker, _ := setup(token)
152 cancel := func() { canceled = true }
153 work := tracker.start(ctx, "work", "message", token, cancel)
154 if err := tracker.cancel(ctx, work.token); err != nil {
158 t.Errorf("tracker.cancel(...): cancel not called")