From f2b68e7e0317c9b6d84cb1235f3755687b35e751 Mon Sep 17 00:00:00 2001 From: ggodik Date: Mon, 4 Apr 2022 16:53:07 -0400 Subject: [PATCH 1/2] fix(windows): fixing tests on windows --- v1/config/file.go | 6 ++- v1/config/file_test.go | 90 +++++++++++++++++++++++++----------------- v1/utils/utils.go | 10 ++++- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/v1/config/file.go b/v1/config/file.go index d18622614..46214d3a8 100644 --- a/v1/config/file.go +++ b/v1/config/file.go @@ -9,6 +9,10 @@ import ( "gopkg.in/yaml.v2" ) +const ( + ConfigMaxSize = 1000 +) + // NewFromYaml creates a config object from YAML file func NewFromYaml(cnfPath string, keepReloading bool) (*Config, error) { cnf, err := fromFile(cnfPath) @@ -50,7 +54,7 @@ func ReadFromFile(cnfPath string) ([]byte, error) { } // Config file found, let's try to read it - data := make([]byte, 1000) + data := make([]byte, ConfigMaxSize) count, err := file.Read(data) if err != nil { return nil, fmt.Errorf("Read from file error: %s", err) diff --git a/v1/config/file_test.go b/v1/config/file_test.go index 59618c01d..82b4e2325 100644 --- a/v1/config/file_test.go +++ b/v1/config/file_test.go @@ -1,57 +1,73 @@ package config_test import ( + "bytes" + "fmt" + "os" + "path/filepath" "testing" "github.com/RichardKnop/machinery/v1/config" "github.com/stretchr/testify/assert" ) -var configYAMLData = `--- -broker: broker -default_queue: default_queue -result_backend: result_backend -results_expire_in: 123456 -amqp: - binding_key: binding_key - exchange: exchange - exchange_type: exchange_type - prefetch_count: 123 - queue_declare_args: - x-max-priority: 10 - queue_binding_args: - image-type: png - x-match: any -sqs: - receive_wait_time_seconds: 123 - receive_visibility_timeout: 456 -redis: - max_idle: 12 - max_active: 123 - max_idle_timeout: 456 - wait: false - read_timeout: 17 - write_timeout: 19 - connect_timeout: 21 - normal_tasks_poll_period: 1001 - delayed_tasks_poll_period: 23 - delayed_tasks_key: delayed_tasks_key - master_name: master_name -no_unix_signals: true -dynamodb: - task_states_table: task_states_table - group_metas_table: group_metas_table -` +const ( + tenBytes = "MACHINERY_" +) + +func testFile(lines int) ([]byte, string, func() error, error) { + b := bytes.NewBuffer([]byte{}) + name := filepath.Join(os.TempDir(), fmt.Sprintf("file_test_%d", lines)) + f, err := os.Create(name) + if err != nil { + return nil, "", nil, err + } + + for i := 0; i < lines; i++ { + _, err := f.WriteString(tenBytes) + if err != nil { + return nil, "", nil, err + } + _, err = b.WriteString(tenBytes) + if err != nil { + return nil, "", nil, err + } + } + + defer f.Close() + + return b.Bytes(), name, func() error { + return os.Remove(name) + }, nil +} func TestReadFromFile(t *testing.T) { t.Parallel() - data, err := config.ReadFromFile("testconfig.yml") + content, name, closer, err := testFile(config.ConfigMaxSize/len(tenBytes) - 1) + defer closer() + + data, err := config.ReadFromFile(name) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, content, data) +} + +func TestReadFromFile_TooLarge(t *testing.T) { + t.Parallel() + + content, name, closer, err := testFile(config.ConfigMaxSize/len(tenBytes) + 1) + defer closer() + + data, err := config.ReadFromFile(name) if err != nil { t.Fatal(err) } - assert.Equal(t, configYAMLData, string(data)) + assert.Equal(t, len(data), config.ConfigMaxSize) + assert.Equal(t, data, content[0:config.ConfigMaxSize]) } func TestNewFromYaml(t *testing.T) { diff --git a/v1/utils/utils.go b/v1/utils/utils.go index 2c2274095..9ba9ea18e 100644 --- a/v1/utils/utils.go +++ b/v1/utils/utils.go @@ -3,12 +3,20 @@ package utils import ( "os" "path/filepath" + "runtime" + "strings" ) const ( LockKeyPrefix = "machinery_lock_" + windows = "windows" ) func GetLockName(name, spec string) string { - return LockKeyPrefix + filepath.Base(os.Args[0]) + name + spec + cmd := filepath.Base(os.Args[0]) + if runtime.GOOS == windows { + cmd = strings.ReplaceAll(cmd, ".exe", "") + } + + return strings.Join([]string{LockKeyPrefix, cmd, name, spec}, "") } From c57ceed9c02d00238a95fb2f33ddd6880dd706b6 Mon Sep 17 00:00:00 2001 From: ggodik Date: Mon, 4 Apr 2022 17:41:16 -0400 Subject: [PATCH 2/2] fix(windows): removing windows specific code --- v1/utils/utils.go | 6 ------ v1/utils/utils_test.go | 6 +++++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/v1/utils/utils.go b/v1/utils/utils.go index 9ba9ea18e..edaf72cae 100644 --- a/v1/utils/utils.go +++ b/v1/utils/utils.go @@ -3,20 +3,14 @@ package utils import ( "os" "path/filepath" - "runtime" "strings" ) const ( LockKeyPrefix = "machinery_lock_" - windows = "windows" ) func GetLockName(name, spec string) string { cmd := filepath.Base(os.Args[0]) - if runtime.GOOS == windows { - cmd = strings.ReplaceAll(cmd, ".exe", "") - } - return strings.Join([]string{LockKeyPrefix, cmd, name, spec}, "") } diff --git a/v1/utils/utils_test.go b/v1/utils/utils_test.go index d4cbb05c6..1c1120be8 100644 --- a/v1/utils/utils_test.go +++ b/v1/utils/utils_test.go @@ -1,6 +1,9 @@ package utils import ( + "os" + "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -10,5 +13,6 @@ func TestGetLockName(t *testing.T) { t.Parallel() lockName := GetLockName("test", "*/3 * * *") - assert.Equal(t, "machinery_lock_utils.testtest*/3 * * *", lockName) + input := []string{LockKeyPrefix, filepath.Base(os.Args[0]), "test", "*/3 * * *"} + assert.Equal(t, strings.Join(input, ""), lockName) }