-
Notifications
You must be signed in to change notification settings - Fork 41
/
scenario.go
81 lines (70 loc) · 1.95 KB
/
scenario.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
// Aquires and exclusive lock against the test suite so that it is the only scenario executing until the secnario
// finishes executing.
//
// Given LOCK
//
// Releases the exclusive lock previously acquired. Not required, any aquired lock is automatically released at the
// end of scenario.
//
// Given UNLOCK
//
// Sleeps for the given number of seconds.
//
// And I sleep for 0.5 second
package cucumber
import (
"context"
"github.com/rs/xid"
"sync"
"time"
"github.com/cucumber/godog"
)
var testCaseLock sync.RWMutex
func init() {
StepModules = append(StepModules, func(ctx *godog.ScenarioContext, s *TestScenario) {
ctx.Step(`^LOCK-*$`, s.lock)
ctx.Step(`^UNLOCK-*$`, s.unlock)
ctx.Step(`^I sleep for (\d+(.\d+)?) seconds?$`, s.iSleepForSecond)
ctx.Step(`^I store an UID as \${([^"]*)}$`, s.iStoreARandomValueAs)
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
testCaseLock.RLock()
return ctx, nil
})
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
if s.hasTestCaseLock {
testCaseLock.Unlock()
} else {
testCaseLock.RUnlock()
}
return ctx, nil
})
})
}
func (s *TestScenario) lock() error {
// User might have already locked the scenario.
if !s.hasTestCaseLock {
// Convert to write lock to be the only executing scenario.
testCaseLock.RUnlock()
testCaseLock.Lock()
s.hasTestCaseLock = true
}
return nil
}
func (s *TestScenario) unlock() error {
// User might have already unlocked the scenario.
if s.hasTestCaseLock {
// Convert to read lock to to allow other scenarios to keep running.
testCaseLock.Unlock()
testCaseLock.RLock()
s.hasTestCaseLock = false
}
return nil
}
func (s *TestScenario) iSleepForSecond(seconds float64) error {
time.Sleep(time.Duration(seconds * float64(time.Second)))
return nil
}
func (s *TestScenario) iStoreARandomValueAs(as string) error {
s.Variables[as] = xid.New().String()
return nil
}