-
Notifications
You must be signed in to change notification settings - Fork 204
/
simulation.go
168 lines (140 loc) · 4.28 KB
/
simulation.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package models
import (
"reflect"
"strconv"
"strings"
"sync"
"github.com/SpectoLabs/hoverfly/core/state"
)
type Simulation struct {
matchingPairs []RequestMatcherResponsePair
ResponseDelays ResponseDelays
ResponseDelaysLogNormal ResponseDelaysLogNormal
Vars *Variables
Literals *Literals
RWMutex sync.RWMutex
}
func NewSimulation() *Simulation {
return &Simulation{
matchingPairs: []RequestMatcherResponsePair{},
ResponseDelays: &ResponseDelayList{},
ResponseDelaysLogNormal: &ResponseDelayLogNormalList{},
Literals: &Literals{},
Vars: &Variables{},
}
}
// Return a boolean indicates if the pair is added or not.
func (this *Simulation) AddPair(pair *RequestMatcherResponsePair) bool {
var duplicate bool
this.RWMutex.Lock()
for _, savedPair := range this.matchingPairs {
duplicate = reflect.DeepEqual(pair.RequestMatcher, savedPair.RequestMatcher)
if duplicate {
break
}
}
if !duplicate {
this.matchingPairs = append(this.matchingPairs, *pair)
}
this.RWMutex.Unlock()
return !duplicate
}
func (this *Simulation) AddPairWithOverwritingDuplicate(pair *RequestMatcherResponsePair) bool {
var duplicate bool
this.RWMutex.Lock()
for i, savedPair := range this.matchingPairs {
duplicate = reflect.DeepEqual(pair.RequestMatcher, savedPair.RequestMatcher)
if duplicate {
this.matchingPairs[i] = *pair
break
}
}
if !duplicate {
this.matchingPairs = append(this.matchingPairs, *pair)
}
this.RWMutex.Unlock()
return !duplicate
}
func (this *Simulation) AddPairWithoutCheck(pair *RequestMatcherResponsePair) {
this.RWMutex.Lock()
this.matchingPairs = append(this.matchingPairs, *pair)
this.RWMutex.Unlock()
}
func (this *Simulation) AddPairInSequence(pair *RequestMatcherResponsePair, state *state.State) {
var duplicate bool
updates := map[int]RequestMatcherResponsePair{}
var counter int
sequenceKey := "sequence:0"
pairNoState := pair.RequestMatcher
pairNoState.RequiresState = nil
this.RWMutex.Lock()
for i, savedPair := range this.matchingPairs {
savedPairNoState := savedPair.RequestMatcher
savedPairNoState.RequiresState = nil
duplicate = reflect.DeepEqual(pairNoState, savedPairNoState)
if duplicate {
counter = counter + 1
if savedPair.RequestMatcher.RequiresState == nil {
savedPair.RequestMatcher.RequiresState = map[string]string{}
}
if savedPair.Response.TransitionsState == nil {
savedPair.Response.TransitionsState = map[string]string{}
}
if pair.RequestMatcher.RequiresState == nil {
pair.RequestMatcher.RequiresState = map[string]string{}
}
sequenceKey = state.GetNewSequenceKey()
for key := range savedPair.RequestMatcher.RequiresState {
if strings.Contains(key, "sequence:") {
sequenceKey = key
break
}
}
sequenceState := savedPair.RequestMatcher.RequiresState[sequenceKey]
nextSequenceState := ""
if sequenceState == "" {
sequenceState = "1"
nextSequenceState = "2"
state.PatchState(map[string]string{sequenceKey: "1"})
} else {
currentSequenceState, _ := strconv.Atoi(sequenceState)
nextSequenceState = strconv.Itoa(currentSequenceState + 1)
}
savedPair.RequestMatcher.RequiresState[sequenceKey] = sequenceState
savedPair.Response.TransitionsState[sequenceKey] = nextSequenceState
updates[i] = savedPair
}
}
for i, updatedPair := range updates {
this.matchingPairs[i] = updatedPair
}
if counter != 0 {
pair.RequestMatcher.RequiresState[sequenceKey] = strconv.Itoa(counter + 1)
}
this.matchingPairs = append(this.matchingPairs, *pair)
this.RWMutex.Unlock()
}
func (this *Simulation) GetMatchingPairs() []RequestMatcherResponsePair {
this.RWMutex.RLock()
pairs := this.matchingPairs
this.RWMutex.RUnlock()
return pairs
}
func (this *Simulation) DeleteMatchingPairsAlongWithCustomData() {
var pairs []RequestMatcherResponsePair
this.RWMutex.Lock()
this.matchingPairs = pairs
this.Literals = &Literals{}
this.Vars = &Variables{}
this.RWMutex.Unlock()
}
func (this *Simulation) AddVariables(variables *Variables) {
this.RWMutex.Lock()
this.Vars = variables
this.RWMutex.Unlock()
}
func (this *Simulation) AddLiterals(literals *Literals) {
this.RWMutex.Lock()
this.Literals = literals
this.RWMutex.Unlock()
}