Skip to content

Commit

Permalink
fix: enable adding multiple modifiers to the same interaction (#47)
Browse files Browse the repository at this point in the history
There was a bug which meant that modifiers based on attempt count would
be overwitten because they were stored with the same key in the
modifiers map. We fix this by adding the attempt count to the key so
that now the keys are different and multiple modifiers can be added for
the same interaction based on attempt.

Co-authored-by: Andrea Rosa <[email protected]>
  • Loading branch information
shreyagarge-f3 and andrea-f3 authored Nov 16, 2023
1 parent 83a4313 commit 9a9fbeb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
62 changes: 60 additions & 2 deletions internal/app/concurrent_proxy_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"testing"
"time"

"github.com/form3tech-oss/pact-proxy/internal/app/configuration"
"github.com/form3tech-oss/pact-proxy/pkg/pactproxy"
"github.com/pact-foundation/pact-go/dsl"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/form3tech-oss/pact-proxy/internal/app/configuration"
"github.com/form3tech-oss/pact-proxy/pkg/pactproxy"
)

const (
Expand Down Expand Up @@ -256,6 +257,26 @@ func (s *ConcurrentProxyStage) the_second_user_response_should_have_the_right_st
return s
}

func (s *ConcurrentProxyStage) all_responses_should_have_the_expected_return_values() *ConcurrentProxyStage {
statuses := make(map[int]int)
bodies := make(map[string]int)
for _, res := range s.userResponses {
statuses[res.StatusCode] += 1
bd, err := io.ReadAll(res.Body)
s.assert.NoError(err)
res.Body.Close()
bodies[strings.ReplaceAll(strings.TrimSpace(string(bd)), "\"", "")] += 1
}
s.assert.Len(statuses, 3)
s.assert.Len(bodies, 2)
s.assert.Equal(1, statuses[http.StatusInternalServerError])
s.assert.Equal(1, statuses[http.StatusOK])
s.assert.Equal(1, statuses[http.StatusTeapot])
s.assert.Equal(2, bodies["{name:any}"])
s.assert.Equal(1, bodies["{name:Form3}"])
return s
}

func (s *ConcurrentProxyStage) all_the_address_responses_should_have_the_right_status_code() *ConcurrentProxyStage {
expectedLen := s.concurrentAddressRequestsPerSecond * int(s.concurrentAddressRequestsDuration/time.Second)
s.assert.Len(s.addressResponses, expectedLen, "number of address responses is not as expected")
Expand All @@ -280,3 +301,40 @@ func (s *ConcurrentProxyStage) the_proxy_waits_for_all_address_responses() *Conc

return s
}

func (s *ConcurrentProxyStage) the_concurrent_requests_are_sent_with_multiple_modifiers_for_same_interaction() {
err := s.pact.Verify(func() (err error) {
attempt := 1
s.proxy.ForInteraction(postNamePactWithAnyName).AddModifier("$.status", fmt.Sprintf("%d", http.StatusTeapot), &attempt)
attempt = 2
s.proxy.ForInteraction(postNamePactWithAnyName).AddModifier("$.status", fmt.Sprintf("%d", http.StatusInternalServerError), &attempt)
attempt = 3
s.proxy.ForInteraction(postNamePactWithAnyName).AddModifier("$.body.name", "Form3", &attempt)

wg := sync.WaitGroup{}

wg.Add(1)
go func() {
defer wg.Done()
s.makeUserRequest()
}()

wg.Add(1)
go func() {
defer wg.Done()
s.makeUserRequest()
}()

wg.Add(1)
go func() {
defer wg.Done()
s.makeUserRequest()
}()

wg.Wait()

return nil
})

s.assert.NoError(err)
}
11 changes: 11 additions & 0 deletions internal/app/concurrent_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func TestConcurrentRequestsForSameModifierBasedOnAttempt(t *testing.T) {
the_second_user_response_should_have_the_right_status_code_and_body()
}

func TestMultipleModifiersForSameInteraction(t *testing.T) {
given, when, then := NewConcurrentProxyStage(t)
given.
a_pact_that_allows_any_names()
when.
the_concurrent_requests_are_sent_with_multiple_modifiers_for_same_interaction()
then.
all_responses_should_have_the_expected_return_values()

}

func TestConcurrentRequestsWaitForAllPacts(t *testing.T) {
given, when, then := NewConcurrentProxyStage(t)

Expand Down
8 changes: 7 additions & 1 deletion internal/app/pactproxy/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ type interactionModifiers struct {
}

func (im *interactionModifier) Key() string {
return strings.Join([]string{im.Interaction, im.Path}, "_")
var key string
if im.Attempt != nil {
key = strings.Join([]string{im.Interaction, im.Path, strconv.Itoa(*im.Attempt)}, "_")
} else {
key = strings.Join([]string{im.Interaction, im.Path}, "_")
}
return key
}

func (ims *interactionModifiers) AddModifier(modifier *interactionModifier) {
Expand Down

0 comments on commit 9a9fbeb

Please sign in to comment.