Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

highlighting: add support for simulating highlighting timeouts #4364

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/frontend/graphqlbackend/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/frontend/internal/pkg/discussions/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

time.Sleep is sadness. Is there no other way to simulate this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there isn't. I know time.Sleep is ugly from first glance but it really is the right choice here. I considered adjusting the context timeout on its own and while that would work for this particular issue it would not raise the other UX issues here like the issue this helped me uncover where we apparently also no longer have any loading indicator for files that load slowly :/


// Never pass binary files to the syntax highlighter.
if IsBinary(content) {
Expand Down