Skip to content

Commit

Permalink
lock assured interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Michael committed Aug 30, 2017
1 parent 622b195 commit ddb2a6d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions assured/assured_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assured
import (
"context"
"errors"
"sync"

"github.com/go-kit/kit/endpoint"
kitlog "github.com/go-kit/kit/log"
Expand All @@ -13,6 +14,7 @@ type AssuredEndpoints struct {
logger kitlog.Logger
assuredCalls map[string][]*Call
madeCalls map[string][]*Call
sync.Mutex
}

// NewAssuredEndpoints creates a new instance of assured endpoints
Expand All @@ -37,8 +39,11 @@ func (a *AssuredEndpoints) WrappedEndpoint(handler func(context.Context, *Call)

// GivenEndpoint is used to stub out a call for a given path
func (a *AssuredEndpoints) GivenEndpoint(ctx context.Context, call *Call) (interface{}, error) {
a.Lock()
a.assuredCalls[call.ID()] = append(a.assuredCalls[call.ID()], call)
a.Unlock()
a.logger.Log("message", "assured call set", "path", call.ID())

return call, nil
}

Expand All @@ -49,11 +54,11 @@ func (a *AssuredEndpoints) WhenEndpoint(ctx context.Context, call *Call) (interf
return nil, errors.New("No assured calls")
}

a.Lock()
a.madeCalls[call.ID()] = append(a.madeCalls[call.ID()], call)

assured := a.assuredCalls[call.ID()][0]

a.assuredCalls[call.ID()] = append(a.assuredCalls[call.ID()][1:], assured)
a.Unlock()

return assured, nil
}
Expand All @@ -65,17 +70,21 @@ func (a *AssuredEndpoints) VerifyEndpoint(ctx context.Context, call *Call) (inte

//ClearEndpoint is used to clear a specific assured call
func (a *AssuredEndpoints) ClearEndpoint(ctx context.Context, call *Call) (interface{}, error) {
a.Lock()
delete(a.assuredCalls, call.ID())
delete(a.madeCalls, call.ID())
a.Unlock()
a.logger.Log("message", "cleared calls for path", "path", call.ID())

return nil, nil
}

//ClearAllEndpoint is used to clear all assured calls
func (a *AssuredEndpoints) ClearAllEndpoint(ctx context.Context, i interface{}) (interface{}, error) {
a.Lock()
a.assuredCalls = map[string][]*Call{}
a.madeCalls = map[string][]*Call{}
a.Unlock()
a.logger.Log("message", "cleared all calls")

return nil, nil
Expand Down

0 comments on commit ddb2a6d

Please sign in to comment.