Skip to content

Commit 8890a52

Browse files
committed
updating test (core download not working under concurrent race
1 parent 4026e4e commit 8890a52

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

auth/auth_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package auth_test

cmd/cmd_test.go

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ func TestLibDownload(t *testing.T) {
122122

123123
// getting what I want...
124124
var have, want output.LibProcessResults
125-
err = json.Unmarshal([]byte(fmt.Sprintf(`{"libraries":[{"name":"invalidLibrary","error":"Library not found"},{"name":"YoutubeApi","status":"Downloaded","path":"%s/YoutubeApi-1.0.0.zip"},{"name":"YouMadeIt","error":"Version Not Found"}]}`,
126-
stagingFolder)), &want)
125+
jsonObj := fmt.Sprintf(`{"libraries":[{"name":"invalidLibrary","error":"Library not found"},{"name":"YoutubeApi","status":"Downloaded","path":"%s/YoutubeApi-1.0.0.zip"},{"name":"YouMadeIt","error":"Version Not Found"}]}`,
126+
stagingFolder)
127+
t.Log(jsonObj)
128+
err = json.Unmarshal([]byte(jsonObj), &want)
127129
if err != nil {
128130
t.Error("JSON marshalling error. TestLibDownload want. " + err.Error())
129131
}
@@ -140,7 +142,7 @@ func TestLibDownload(t *testing.T) {
140142
d, _ := ioutil.ReadAll(tempFile)
141143
err = json.Unmarshal(d, &have)
142144
if err != nil {
143-
t.Error("JSON marshalling error. TestLibDownload have")
145+
t.Error("JSON marshalling error. TestLibDownload have ", jsonObj)
144146
}
145147

146148
//checking if it is what I want...
@@ -164,6 +166,76 @@ func TestLibDownload(t *testing.T) {
164166
}
165167
}
166168

169+
func TestCoreDownload(t *testing.T) {
170+
tempFile := createTempRedirect()
171+
defer cleanTempRedirect(tempFile)
172+
173+
// getting the paths to create the want path of the want object.
174+
stagingFolder, err := common.GetDownloadCacheFolder("packages")
175+
if err != nil {
176+
t.Error("Cannot get cache folder")
177+
}
178+
179+
// getting what I want...
180+
var have, want output.CoreProcessResults
181+
jsonObj := strings.Replace(`{"cores":[{"name":"unparsablearg","error":"Invalid item (not PACKAGER:CORE[=VERSION])"},{"name":"sam","error":"Version notexistingversion Not Found"},{"name":"sam","error":"Version 1.0.0 Not Found"},{"name":"samd","status":"Downloaded","path":"%s/samd-1.6.15.tar.bz2"}],"tools":[{"name":"arduinoOTA","status":"Downloaded","path":"%s/arduinoOTA-1.2.0-linux_amd64.tar.bz2"},{"name":"openocd","status":"Downloaded","path":"%s/openocd-0.9.0-arduino6-static-x86_64-linux-gnu.tar.bz2"},{"name":"CMSIS-Atmel","status":"Downloaded","path":"%s/CMSIS-Atmel-1.1.0.tar.bz2"},{"name":"CMSIS","status":"Downloaded","path":"%s/CMSIS-4.5.0.tar.bz2"},{"name":"arm-none-eabi-gcc","status":"Downloaded","path":"%s/gcc-arm-none-eabi-4.8.3-2014q1-linux64.tar.gz"},{"name":"bossac","status":"Downloaded","path":"%s/bossac-1.7.0-x86_64-linux-gnu.tar.gz"}]}`,
182+
"%s", stagingFolder, 8)
183+
184+
t.Log(jsonObj)
185+
err = json.Unmarshal([]byte(jsonObj), &want)
186+
if err != nil {
187+
t.Error("JSON marshalling error. TestCoreDownload want. " + err.Error())
188+
}
189+
190+
// arduino lib download YoutubeApi --format json
191+
executeWithArgs("core", "download", "arduino:samd", "unparsablearg", "arduino:sam=notexistingversion", "arduino:sam=1.0.0", "--format", "json")
192+
193+
//resetting the file to allow the full read (it has been written by executeWithArgs)
194+
_, err = tempFile.Seek(0, 0)
195+
if err != nil {
196+
t.Error("Cannot set file for read mode")
197+
}
198+
199+
d, _ := ioutil.ReadAll(tempFile)
200+
err = json.Unmarshal(d, &have)
201+
if err != nil {
202+
t.Error("JSON marshalling error ", d)
203+
}
204+
205+
//checking if it is what I want...
206+
if len(have.Cores) != len(want.Cores) || len(have.Tools) != len(want.Tools) {
207+
t.Error("Output not matching, different line number from command", len(have.Cores), len(want.Cores), len(have.Tools), len(want.Tools))
208+
}
209+
210+
for _, itemHave := range have.Cores {
211+
ok := false
212+
for _, itemWant := range want.Cores {
213+
//t.Log(itemHave, " -------- ", itemWant)
214+
if itemHave.String() == itemWant.String() {
215+
ok = true
216+
break
217+
}
218+
}
219+
if !ok {
220+
t.Errorf(`Got "%s" not found`, itemHave)
221+
}
222+
}
223+
224+
for _, itemHave := range have.Tools {
225+
ok := false
226+
for _, itemWant := range want.Tools {
227+
//t.Log(itemHave, " -------- ", itemWant)
228+
if itemHave.String() == itemWant.String() {
229+
ok = true
230+
break
231+
}
232+
}
233+
if !ok {
234+
t.Errorf(`Got "%s" not found`, itemHave)
235+
}
236+
}
237+
}
238+
167239
func checkOutput(t *testing.T, want []string, tempFile *os.File) {
168240
_, err := tempFile.Seek(0, 0)
169241
if err != nil {

common/net_functions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func DownloadPackage(URL string, initialData *os.File, totalSize int64, handleRe
7676
return errors.New("Cannot fill a nil file pointer")
7777
}
7878

79-
client := http.DefaultClient
79+
client := &http.Client{}
8080

8181
var initialSize int64
8282
stats, err := initialData.Stat()

task/concurrency.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ package task
3131

3232
import (
3333
"fmt"
34-
"sync"
3534

3635
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3736
)
@@ -81,21 +80,20 @@ func ExecuteSequence(taskWrappers []Wrapper, ignoreOnFailure []bool, verbosity i
8180
// ExecuteParallelFromMap executes a set of taskwrappers in parallel, taking input from a map[string]Wrapper.
8281
func ExecuteParallelFromMap(taskMap map[string]Wrapper, verbosity int) map[string]Result {
8382
results := make(chan resultWithKey, len(taskMap))
84-
wg := sync.WaitGroup{}
85-
wg.Add(len(taskMap))
83+
turn := make(chan bool)
8684

8785
for key, task := range taskMap {
88-
go func(key string, task Wrapper, wg *sync.WaitGroup) {
86+
go func(key string, task Wrapper) {
87+
turn <- true
8988
results <- resultWithKey{
9089
Key: key,
9190
Result: func() Result {
92-
defer wg.Done()
9391
return task.Execute(verbosity)
9492
}(),
9593
}
96-
}(key, task, &wg)
94+
<-turn
95+
}(key, task)
9796
}
98-
wg.Wait()
9997
close(results)
10098
mapResult := make(map[string]Result, len(results))
10199
for result := range results {
@@ -108,17 +106,16 @@ func ExecuteParallelFromMap(taskMap map[string]Wrapper, verbosity int) map[strin
108106
// ExecuteParallel executes a set of Wrappers in parallel, handling concurrency for results.
109107
func ExecuteParallel(taskWrappers []Wrapper, verbosity int) []Result {
110108
results := make(chan Result, len(taskWrappers))
111-
wg := sync.WaitGroup{}
112-
wg.Add(len(taskWrappers))
109+
turn := make(chan bool)
113110
for _, task := range taskWrappers {
114-
go func(task Wrapper, wg *sync.WaitGroup) {
115-
results <- func(wg *sync.WaitGroup) Result {
116-
defer wg.Done()
111+
go func(task Wrapper) {
112+
turn <- true
113+
results <- func() Result {
117114
return task.Execute(verbosity)
118-
}(wg)
119-
}(task, &wg)
115+
}()
116+
<-turn
117+
}(task)
120118
}
121-
wg.Wait()
122119
close(results)
123120
array := make([]Result, len(results))
124121
for i := range array {

0 commit comments

Comments
 (0)