Skip to content

Commit 807ea38

Browse files
fix: correct concurrency to align with documentation (#2014)
Co-authored-by: Trevor Brown <[email protected]>
1 parent 43a84a0 commit 807ea38

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

internal/config/config.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func defaultSettings() *Settings {
8383
AlwaysKeepDownload: false,
8484
PluginRepositoryLastCheckDuration: pluginRepoCheckDurationDefault,
8585
DisablePluginShortNameRepository: false,
86+
Concurrency: getConcurrency("auto"),
8687
}
8788
}
8889

@@ -169,7 +170,7 @@ func (c *Config) DisablePluginShortNameRepository() (bool, error) {
169170
func (c *Config) Concurrency() (string, error) {
170171
err := c.loadSettings()
171172
if err != nil {
172-
return "", err
173+
return getConcurrency("auto"), err
173174
}
174175

175176
return c.Settings.Concurrency, nil
@@ -248,7 +249,11 @@ func loadSettings(asdfrcPath string) (Settings, error) {
248249
boolOverride(&settings.LegacyVersionFile, mainConf, "legacy_version_file")
249250
boolOverride(&settings.AlwaysKeepDownload, mainConf, "always_keep_download")
250251
boolOverride(&settings.DisablePluginShortNameRepository, mainConf, "disable_plugin_short_name_repository")
251-
settings.Concurrency = strings.ToLower(mainConf.Key("concurrency").String())
252+
253+
concurrency := strings.ToLower(mainConf.Key("concurrency").String())
254+
if concurrency != "" {
255+
settings.Concurrency = getConcurrency(concurrency)
256+
}
252257

253258
return *settings, nil
254259
}
@@ -263,3 +268,15 @@ func boolOverride(field *bool, section *ini.Section, key string) {
263268
*field = false
264269
}
265270
}
271+
272+
func getConcurrency(concurrency string) string {
273+
concurrencyFromEnv := strings.ToLower(os.Getenv("ASDF_CONCURRENCY"))
274+
if concurrencyFromEnv != "" {
275+
concurrency = concurrencyFromEnv
276+
}
277+
278+
if concurrency == "auto" || concurrency == "" {
279+
return strconv.Itoa(runtime.NumCPU())
280+
}
281+
return concurrency
282+
}

internal/config/config_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"runtime"
5+
"strconv"
46
"testing"
57

68
"github.com/stretchr/testify/assert"
@@ -64,18 +66,39 @@ func TestLoadSettings(t *testing.T) {
6466
assert.True(t, settings.PluginRepositoryLastCheckDuration.Never, "PluginRepositoryLastCheckDuration field has wrong value")
6567
assert.Zero(t, settings.PluginRepositoryLastCheckDuration.Every, "PluginRepositoryLastCheckDuration field has wrong value")
6668
assert.True(t, settings.DisablePluginShortNameRepository, "DisablePluginShortNameRepository field has wrong value")
69+
assert.Equal(t, "5", settings.Concurrency, "Concurrency field has wrong value")
70+
})
71+
72+
t.Run("ASDF_CONCURRENCY=99 takes precedence over asdfrc value", func(t *testing.T) {
73+
t.Setenv("ASDF_CONCURRENCY", "99")
74+
settings, err := loadSettings("testdata/asdfrc")
75+
assert.Nil(t, err)
76+
77+
assert.True(t, settings.Loaded, "Expected Loaded field to be set to true")
78+
assert.Equal(t, "99", settings.Concurrency, "Concurrency field has wrong value")
79+
})
80+
81+
t.Run("ASDF_CONCURRENCY=auto takes precedence over asdfrc value", func(t *testing.T) {
82+
expectedConcurrency := strconv.Itoa(runtime.NumCPU())
83+
t.Setenv("ASDF_CONCURRENCY", "auto")
84+
settings, err := loadSettings("testdata/asdfrc")
85+
assert.Nil(t, err)
86+
87+
assert.True(t, settings.Loaded, "Expected Loaded field to be set to true")
88+
assert.Equal(t, expectedConcurrency, settings.Concurrency, "Concurrency field has wrong value")
6789
})
6890

6991
t.Run("When given path to empty file returns settings struct with defaults", func(t *testing.T) {
92+
expectedConcurrency := strconv.Itoa(runtime.NumCPU())
7093
settings, err := loadSettings("testdata/empty-asdfrc")
71-
7294
assert.Nil(t, err)
7395

7496
assert.False(t, settings.LegacyVersionFile, "LegacyVersionFile field has wrong value")
7597
assert.False(t, settings.AlwaysKeepDownload, "AlwaysKeepDownload field has wrong value")
7698
assert.False(t, settings.PluginRepositoryLastCheckDuration.Never, "PluginRepositoryLastCheckDuration field has wrong value")
7799
assert.Equal(t, settings.PluginRepositoryLastCheckDuration.Every, 60, "PluginRepositoryLastCheckDuration field has wrong value")
78100
assert.False(t, settings.DisablePluginShortNameRepository, "DisablePluginShortNameRepository field has wrong value")
101+
assert.Equal(t, expectedConcurrency, settings.Concurrency, "Concurrency field has wrong value")
79102
})
80103
}
81104

internal/config/testdata/asdfrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use_release_candidates = yes
55
always_keep_download = yes
66
plugin_repository_last_check_duration = never
77
disable_plugin_short_name_repository = yes
8+
concurrency = 5
89

910
# Hooks
1011
pre_asdf_plugin_add = echo Executing with args: $@

internal/versions/versions.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, versionStr str
160160
return VersionAlreadyInstalledError{version: version, toolName: plugin.Name}
161161
}
162162

163+
concurrency, _ := conf.Concurrency()
163164
env := map[string]string{
164165
"ASDF_INSTALL_TYPE": version.Type,
165166
"ASDF_INSTALL_VERSION": version.Value,
166167
"ASDF_INSTALL_PATH": installDir,
167168
"ASDF_DOWNLOAD_PATH": downloadDir,
168-
"ASDF_CONCURRENCY": asdfConcurrency(conf),
169+
"ASDF_CONCURRENCY": concurrency,
169170
}
170171

171172
err = os.MkdirAll(downloadDir, 0o777)
@@ -227,21 +228,6 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, versionStr str
227228
return nil
228229
}
229230

230-
func asdfConcurrency(conf config.Config) string {
231-
val, ok := os.LookupEnv("ASDF_CONCURRENCY")
232-
233-
if !ok {
234-
val, err := conf.Concurrency()
235-
if err != nil {
236-
return "1"
237-
}
238-
239-
return val
240-
}
241-
242-
return val
243-
}
244-
245231
// Latest invokes the plugin's latest-stable callback if it exists and returns
246232
// the version it returns. If the callback is missing it invokes the list-all
247233
// callback and returns the last version matching the query, if a query is

0 commit comments

Comments
 (0)