Skip to content

Fix double 100% progress on CompileResponse #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 3, 2023
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
8 changes: 8 additions & 0 deletions internal/integrationtest/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/arduino/arduino-cli/arduino"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -171,6 +172,7 @@ func TestDaemonCompileOptions(t *testing.T) {
// Build sketch (without errors)
compile, err = grpcInst.Compile(context.Background(), "arduino:avr:uno:some_menu=good", sk.String(), "")
require.NoError(t, err)
analyzer := NewTaskProgressAnalyzer(t)
for {
msg, err := compile.Recv()
if err == io.EOF {
Expand All @@ -180,7 +182,13 @@ func TestDaemonCompileOptions(t *testing.T) {
if msg.ErrStream != nil {
fmt.Printf("COMPILE> %v\n", string(msg.GetErrStream()))
}
analyzer.Process(msg.GetProgress())
}
// https://github.com/arduino/arduino-cli/issues/2016
// assert that the task progress is increasing and doesn't contain multiple 100% values
results := analyzer.Results[""]
require.True(t, results[len(results)-1].Completed)
require.IsNonDecreasing(t, f.Map(results, (*commands.TaskProgress).GetPercent))
}

func TestDaemonCompileAfterFailedLibInstall(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions internal/integrationtest/daemon/task_progress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of arduino-cli.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package daemon_test

import (
"testing"

"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)

// TaskProgressAnalyzer analyzes TaskProgress messages for consistency
type TaskProgressAnalyzer struct {
t *testing.T
Results map[string][]*commands.TaskProgress
}

// NewTaskProgressAnalyzer creates a new TaskProgressAnalyzer
func NewTaskProgressAnalyzer(t *testing.T) *TaskProgressAnalyzer {
return &TaskProgressAnalyzer{
t: t,
Results: map[string][]*commands.TaskProgress{},
}
}

// Process the given TaskProgress message.
func (a *TaskProgressAnalyzer) Process(progress *commands.TaskProgress) {
if progress == nil {
return
}

taskName := progress.GetName()
a.Results[taskName] = append(a.Results[taskName], progress)
}
31 changes: 26 additions & 5 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *Builder) Run(ctx *types.Context) error {
return err
}

var _err error
var _err, mainErr error
commands := []types.Command{
&ContainerSetupHardwareToolsLibsSketchAndProps{},

Expand Down Expand Up @@ -92,12 +92,25 @@ func (s *Builder) Run(ctx *types.Context) error {
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
}

mainErr := runCommands(ctx, commands)
ctx.Progress.AddSubSteps(len(commands) + 4)
defer ctx.Progress.RemoveSubSteps()

for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
mainErr = errors.WithStack(err)
break
}
ctx.Progress.CompleteStep()
ctx.PushProgress()
}

if ctx.CompilationDatabase != nil {
ctx.CompilationDatabase.SaveToFile()
}

var otherErr error
commands = []types.Command{
&PrintUsedAndNotUsedLibraries{SketchError: mainErr != nil},

Expand All @@ -107,7 +120,16 @@ func (s *Builder) Run(ctx *types.Context) error {

&phases.Sizer{SketchError: mainErr != nil},
}
otherErr := runCommands(ctx, commands)
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
otherErr = errors.WithStack(err)
break
}
ctx.Progress.CompleteStep()
ctx.PushProgress()
}

if mainErr != nil {
return mainErr
Expand Down Expand Up @@ -193,8 +215,7 @@ func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
}

func RunBuilder(ctx *types.Context) error {
command := Builder{}
return command.Run(ctx)
return runCommands(ctx, []types.Command{&Builder{}})
}

func RunParseHardware(ctx *types.Context) error {
Expand Down
5 changes: 2 additions & 3 deletions legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ import (
type ContainerSetupHardwareToolsLibsSketchAndProps struct{}

func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context) error {
// total number of steps in this container: 4
ctx.Progress.AddSubSteps(4)
defer ctx.Progress.RemoveSubSteps()
commands := []types.Command{
&AddAdditionalEntriesToContext{},
&FailIfBuildPathEqualsSketchPath{},
&LibrariesLoader{},
}
ctx.Progress.AddSubSteps(len(commands))
defer ctx.Progress.RemoveSubSteps()
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
Expand Down
5 changes: 4 additions & 1 deletion legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map {

func (ctx *Context) PushProgress() {
if ctx.ProgressCB != nil {
ctx.ProgressCB(&rpc.TaskProgress{Percent: ctx.Progress.Progress})
ctx.ProgressCB(&rpc.TaskProgress{
Percent: ctx.Progress.Progress,
Completed: ctx.Progress.Progress >= 100.0,
})
}
}

Expand Down