Skip to content
Merged
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
3 changes: 2 additions & 1 deletion core/matching/first_match_strategy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package matching_test

import (
"sync"
"testing"

"github.com/SpectoLabs/hoverfly/core/matching"
Expand Down Expand Up @@ -1106,7 +1107,7 @@ func Test_FirstMatchShouldBeCachableIfMatchedOnEverythingApartFromStateZeroTimes
Path: "miss",
}

result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.FirstMatchStrategy{})
result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.FirstMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand Down
7 changes: 5 additions & 2 deletions core/matching/state_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ func StateMatcher(currentState *state.State, requiredState map[string]string) *F
}
}

currentState.RWMutex.RLock()
copy_state := currentState.State
currentState.RWMutex.RUnlock()
for key, value := range requiredState {
if _, ok := currentState.State[key]; !ok {
if _, ok := copy_state[key]; !ok {
matched = false
}
if currentState.State[key] != value {
if copy_state[key] != value {
matched = false
} else {
score++
Expand Down
5 changes: 4 additions & 1 deletion core/matching/strongest_match_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ func (s *StrongestMatchStrategy) PostMatching(req models.RequestDetails, request
} else if s.matched == false && s.requestMatch == nil && s.score >= s.closestMissScore {
s.closestMissScore = s.score
view := matchingPair.BuildView()
state.RWMutex.RLock()
copy_state := state.State
state.RWMutex.RUnlock()
s.closestMiss = &models.ClosestMiss{
RequestDetails: req,
RequestMatcher: view.RequestMatcher,
Response: view.Response,
MissedFields: s.missedFields,
State: state.State,
State: copy_state,
}
}

Expand Down
15 changes: 8 additions & 7 deletions core/matching/strongest_match_strategy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package matching_test

import (
"sync"
"testing"

"github.com/SpectoLabs/hoverfly/core/handlers/v2"
Expand Down Expand Up @@ -1839,7 +1840,7 @@ func Test_MatchingStrategyRunner_RequestMatchersShouldMatchOnStateAndNotBeCachab
r,
false,
simulation,
&state.State{map[string]string{"key1": "value1", "key2": "value2"}},
&state.State{map[string]string{"key1": "value1", "key2": "value2"}, sync.RWMutex{}},
&matching.StrongestMatchStrategy{})

Expect(result.Error).To(BeNil())
Expand Down Expand Up @@ -1920,7 +1921,7 @@ func Test_StrongestMatch_ShouldNotBeCachableIfMatchedOnEverythingApartFromStateA
Path: "/foo",
}

result := matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result := matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeFalse())
Expand Down Expand Up @@ -1999,7 +2000,7 @@ func Test_StrongestMatch__ShouldBeCachableIfMatchedOnEverythingApartFromStateZer
Path: "/foo",
}

result := matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result := matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand All @@ -2015,7 +2016,7 @@ func Test_StrongestMatch__ShouldBeCachableIfMatchedOnEverythingApartFromStateZer
Path: "/foo",
}

result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand All @@ -2031,7 +2032,7 @@ func Test_StrongestMatch__ShouldBeCachableIfMatchedOnEverythingApartFromStateZer
Path: "/foo",
}

result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand All @@ -2047,7 +2048,7 @@ func Test_StrongestMatch__ShouldBeCachableIfMatchedOnEverythingApartFromStateZer
Path: "/foo",
}

result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand All @@ -2063,7 +2064,7 @@ func Test_StrongestMatch__ShouldBeCachableIfMatchedOnEverythingApartFromStateZer
Path: "miss",
}

result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}}, &matching.StrongestMatchStrategy{})
result = matching.MatchingStrategyRunner(r, false, simulation, &state.State{map[string]string{"miss": "me"}, sync.RWMutex{}}, &matching.StrongestMatchStrategy{})

Expect(result.Error).ToNot(BeNil())
Expect(result.Cachable).To(BeTrue())
Expand Down
17 changes: 15 additions & 2 deletions core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package state
import (
"fmt"
"strings"
"sync"
)

type State struct {
State map[string]string
State map[string]string
RWMutex sync.RWMutex
}

func NewState() *State {
Expand All @@ -30,28 +32,38 @@ func NewStateFromState(incomingState map[string]string) *State {
}

func (s *State) GetState(key string) string {
return s.State[key]
s.RWMutex.RLock()
val := s.State[key]
s.RWMutex.RUnlock()
return val
}

func (s *State) SetState(state map[string]string) {
s.RWMutex.Lock()
s.State = state
s.RWMutex.Unlock()
}

func (s *State) PatchState(toPatch map[string]string) {
s.RWMutex.Lock()
for k, v := range toPatch {
s.State[k] = v
}
s.RWMutex.Unlock()
}

func (s *State) RemoveState(toRemove []string) {
s.RWMutex.Lock()
for _, key := range toRemove {
delete(s.State, key)
}
s.RWMutex.Unlock()
}

func (s *State) GetNewSequenceKey() string {
returnKey := ""
i := 1
s.RWMutex.RLock()
for returnKey == "" {
tempKey := fmt.Sprintf("sequence:%v", i)
if s.State[tempKey] == "" {
Expand All @@ -60,5 +72,6 @@ func (s *State) GetNewSequenceKey() string {
i = i + 1
}
}
s.RWMutex.RUnlock()
return returnKey
}