generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_test.go
124 lines (105 loc) · 2.58 KB
/
lock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package resource_test
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"testing"
"time"
"github.com/cucumber/godog"
"github.com/godogx/resource"
"github.com/stretchr/testify/assert"
)
type lock struct {
lock *resource.Lock
}
func (l *lock) setup(s *godog.ScenarioContext) {
l.lock.Register(s)
s.Step(`^I acquire "([^"]*)"$`, func(ctx context.Context, name string) (context.Context, error) {
ok, err := l.lock.Acquire(ctx, name)
if err != nil {
return ctx, err
}
if !ok {
return ctx, fmt.Errorf("failed to acquire lock")
}
return ctx, nil
})
s.Step(`^I should be blocked for "([^"]*)"$`, func(ctx context.Context, name string) error {
if !l.lock.IsLocked(ctx, name) {
return fmt.Errorf("%s is not locked", name)
}
return nil
})
s.Step(`^I should not be blocked for "([^"]*)"$`, func(ctx context.Context, name string) error {
if l.lock.IsLocked(ctx, name) {
return fmt.Errorf("%s is locked", name)
}
return nil
})
s.Step("^I sleep$", func() {
time.Sleep(time.Microsecond * time.Duration(rand.Int63n(10000)+1000)) //nolint:gosec
})
s.Step("^I sleep longer$", func() {
time.Sleep(time.Millisecond * 100)
})
}
func TestNewLock(t *testing.T) {
l := &lock{lock: resource.NewLock(nil)}
out := bytes.Buffer{}
suite := godog.TestSuite{
ScenarioInitializer: l.setup,
Options: &godog.Options{
Output: &out,
Format: "pretty",
Strict: true,
Paths: []string{"_testdata/NoBlock.feature"},
Concurrency: 10,
},
}
if suite.Run() != 0 {
t.Fatal("test failed")
}
}
func TestNewLock_blocked(t *testing.T) {
l := &lock{lock: resource.NewLock(nil)}
out := bytes.Buffer{}
suite := godog.TestSuite{
ScenarioInitializer: l.setup,
Options: &godog.Options{
Output: &out,
Format: "pretty",
Strict: true,
Paths: []string{"_testdata/Block.feature"},
Concurrency: 10,
},
}
if suite.Run() != 0 {
t.Fatal("test failed", out.String())
}
}
func TestNewLock_failOnRelease(t *testing.T) {
l := &lock{lock: resource.NewLock(func(name string) error {
return errors.New("failed")
})}
out := bytes.Buffer{}
suite := godog.TestSuite{
ScenarioInitializer: l.setup,
Options: &godog.Options{
Output: &out,
Format: "pretty",
Strict: true,
Paths: []string{"_testdata/Block.feature"},
Concurrency: 10,
},
}
if suite.Run() != 1 {
t.Fatal("test failed", out.String())
}
}
func TestLock_Acquire(t *testing.T) {
l := resource.NewLock(nil)
_, err := l.Acquire(context.Background(), "test")
assert.EqualError(t, err, resource.ErrMissingScenarioLock.Error())
}