-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/telemetry: test that telemetry counters are written
Change-Id: Iceb8406cf3290180690f29bcba9f2fe6019285ad Reviewed-on: https://go-review.googlesource.com/c/tools/+/517135 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Robert Findley <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build go1.21 && !openbsd && !js && !wasip1 && !solaris && !android && !386 | ||
// +build go1.21,!openbsd,!js,!wasip1,!solaris,!android,!386 | ||
|
||
package telemetry_test | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"golang.org/x/telemetry/counter" | ||
"golang.org/x/telemetry/counter/countertest" // requires go1.21+ | ||
"golang.org/x/tools/gopls/internal/bug" | ||
"golang.org/x/tools/gopls/internal/hooks" | ||
. "golang.org/x/tools/gopls/internal/lsp/regtest" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
tmp, err := os.MkdirTemp("", "gopls-telemetry-test") | ||
if err != nil { | ||
panic(err) | ||
} | ||
countertest.Open(tmp) | ||
defer os.RemoveAll(tmp) | ||
Main(m, hooks.Options) | ||
} | ||
|
||
func TestTelemetry(t *testing.T) { | ||
var ( | ||
goversion = "" | ||
editor = "vscode" // We set ClientName("Visual Studio Code") below. | ||
) | ||
|
||
// Verify that a properly configured session gets notified of a bug on the | ||
// server. | ||
WithOptions( | ||
Modes(Default), // must be in-process to receive the bug report below | ||
Settings{"showBugReports": true}, | ||
ClientName("Visual Studio Code"), | ||
).Run(t, "", func(t *testing.T, env *Env) { | ||
goversion = strconv.Itoa(env.GoVersion()) | ||
const desc = "got a bug" | ||
bug.Report(desc) // want a stack counter with the trace starting from here. | ||
env.Await(ShownMessage(desc)) | ||
}) | ||
|
||
// gopls/editor:client | ||
// gopls/goversion:1.x | ||
for _, c := range []*counter.Counter{ | ||
counter.New("gopls/client:" + editor), | ||
counter.New("gopls/goversion:1." + goversion), | ||
} { | ||
count, err := countertest.ReadCounter(c) | ||
if err != nil || count != 1 { | ||
t.Errorf("ReadCounter(%q) = (%v, %v), want (1, nil)", c.Name(), count, err) | ||
} | ||
} | ||
|
||
// gopls/bug | ||
bugcount := bug.BugReportCount | ||
counts, err := countertest.ReadStackCounter(bugcount) | ||
if err != nil { | ||
t.Fatalf("ReadStackCounter(bugreportcount) failed - %v", err) | ||
} | ||
if len(counts) != 1 || !hasEntry(counts, t.Name(), 1) { | ||
t.Errorf("read stackcounter(%q) = (%#v, %v), want one entry", "gopls/bug", counts, err) | ||
} | ||
} | ||
|
||
func hasEntry(counts map[string]uint64, pattern string, want uint64) bool { | ||
for k, v := range counts { | ||
if strings.Contains(k, pattern) && v == want { | ||
return true | ||
} | ||
} | ||
return false | ||
} |