Skip to content
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

fix(windows): fixing tests on windows #752

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion v1/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
90 changes: 53 additions & 37 deletions v1/config/file_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion v1/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package utils
import (
"os"
"path/filepath"
"strings"
)

const (
LockKeyPrefix = "machinery_lock_"
)

func GetLockName(name, spec string) string {
return LockKeyPrefix + filepath.Base(os.Args[0]) + name + spec
cmd := filepath.Base(os.Args[0])
return strings.Join([]string{LockKeyPrefix, cmd, name, spec}, "")
}
6 changes: 5 additions & 1 deletion v1/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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)
}