From 97deffcfdbcca2f3d61757356144e461a7b4e9db Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Wed, 5 Jun 2019 00:40:35 -0700 Subject: [PATCH] highlighting: add support for simulating highlighting timeouts This is useful for confirming our behavior of highlighting timeouts, and can be used in the future to automate testing of this behavior as part of an e2e test. Primarily adding to reproduce https://github.com/sourcegraph/sourcegraph/issues/4267 Note: This matches other cases where e.g. github.com/sourcegraphtest/AlwaysCloningTest is a special repository for testing gitserver cloning states end-to-end. Helps #4267 --- cmd/frontend/graphqlbackend/file.go | 5 +++-- cmd/frontend/internal/pkg/discussions/format.go | 2 +- pkg/highlight/highlight.go | 5 ++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/frontend/graphqlbackend/file.go b/cmd/frontend/graphqlbackend/file.go index 745ba910fb433..261544367f2ee 100644 --- a/cmd/frontend/graphqlbackend/file.go +++ b/cmd/frontend/graphqlbackend/file.go @@ -64,7 +64,7 @@ func (*schemaResolver) HighlightCode(ctx context.Context, args *struct { }) (string, error) { language := highlight.SyntectLanguageMap[strings.ToLower(args.FuzzyLanguage)] filePath := "file." + language - html, _, err := highlight.Code(ctx, []byte(args.Code), filePath, args.DisableTimeout, args.IsLightTheme) + html, _, err := highlight.Code(ctx, []byte(args.Code), filePath, args.DisableTimeout, args.IsLightTheme, false) if err != nil { return args.Code, err } @@ -110,7 +110,8 @@ func (r *gitTreeEntryResolver) Highlight(ctx context.Context, args *struct { html template.HTML result = &highlightedFileResolver{} ) - html, result.aborted, err = highlight.Code(ctx, content, r.path, args.DisableTimeout, args.IsLightTheme) + simulateTimeout := r.commit.repo.repo.Name == "github.com/sourcegraph/AlwaysHighlightTimeoutTest" + html, result.aborted, err = highlight.Code(ctx, content, r.path, args.DisableTimeout, args.IsLightTheme, simulateTimeout) if err != nil { return nil, err } diff --git a/cmd/frontend/internal/pkg/discussions/format.go b/cmd/frontend/internal/pkg/discussions/format.go index abd76b3bee30f..2d2b9a6ea4a17 100644 --- a/cmd/frontend/internal/pkg/discussions/format.go +++ b/cmd/frontend/internal/pkg/discussions/format.go @@ -78,7 +78,7 @@ func formatTargetRepoLinesHTML(ctx context.Context, tr *types.DiscussionThreadTa // contents along here. isLightTheme := false disableTimeout := false - html, _, err := highlight.Code(ctx, []byte(code), *tr.Path, disableTimeout, isLightTheme) + html, _, err := highlight.Code(ctx, []byte(code), *tr.Path, disableTimeout, isLightTheme, false) if err != nil { return err } diff --git a/pkg/highlight/highlight.go b/pkg/highlight/highlight.go index 1348c39c352ba..9ae6d8011a59d 100644 --- a/pkg/highlight/highlight.go +++ b/pkg/highlight/highlight.go @@ -43,12 +43,15 @@ func IsBinary(content []byte) bool { // // The returned boolean represents whether or not highlighting was aborted due // to timeout. In this scenario, a plain text table is returned. -func Code(ctx context.Context, content []byte, filepath string, disableTimeout bool, isLightTheme bool) (template.HTML, bool, error) { +func Code(ctx context.Context, content []byte, filepath string, disableTimeout bool, isLightTheme bool, simulateTimeout bool) (template.HTML, bool, error) { if !disableTimeout { var cancel func() ctx, cancel = context.WithTimeout(ctx, 3*time.Second) defer cancel() } + if simulateTimeout { + time.Sleep(4 * time.Second) + } // Never pass binary files to the syntax highlighter. if IsBinary(content) {