Skip to content

Commit

Permalink
Storage limit (#100)
Browse files Browse the repository at this point in the history
* Added request-storage-capacity
  • Loading branch information
jmartin82 authored Feb 27, 2020
1 parent 92399de commit 19ad0c2
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 40 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ To configure Mmock, use command line flags described in help.
Console server IP (default "public_ip")
-console-port int
Console server Port (default 8082)
-request-storage-capacity int
Request storage capacity (0 = infinite) (default 100)
-results-per-page uint
Number of results per page (default 25)
-server-ip string
Expand Down Expand Up @@ -358,7 +360,7 @@ You can find a comprehensive set of open tools for the OAI specification at: htt

### Verify

The Mmock records all requests it receives in memory (at least until it is reset).
The Mmock records the incoming requests in memory (last 100 by default).
This makes it possible to verify that a request matching a specific pattern was received, and also to fetch the requests details.

**Title** : Get all requests.<br>
Expand Down
7 changes: 4 additions & 3 deletions cmd/mmock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func startServer(ip string, port, portTLS int, configTLS string, done chan struc
dispatcher.Start()
done <- struct{}{}
}
func startConsole(ip string, port int, resultsPerPage uint, spy match.TransactionSpier, scenario match.ScenearioStorer, mapping config.Mapping, done chan struct{}, mLog chan match.Transaction) {
func startConsole(ip string, port int, resultsPerPage int, spy match.TransactionSpier, scenario match.ScenearioStorer, mapping config.Mapping, done chan struct{}, mLog chan match.Transaction) {
dispatcher := console.Dispatcher{
IP: ip,
Port: port,
Expand Down Expand Up @@ -167,7 +167,8 @@ func main() {
console := flag.Bool("console", true, "Console enabled (true/false)")
cPath := flag.String("config-path", path, "Mocks config folder")
cTLS := flag.String("tls-path", TLS, "TLS config folder (server.crt and server.key should be inside)")
cResultsPerPage := flag.Uint("results-per-page", 25, "Number of results per page")
cStorageCapacity := flag.Int("request-storage-capacity", 100, "Request storage capacity (0 = infinite)")
cResultsPerPage := flag.Int("results-per-page", 25, "Number of results per page")

flag.Parse()

Expand All @@ -179,7 +180,7 @@ func main() {
scenario := match.NewInMemoryScenarioStore()
comparator := payload.NewDefaultComparator()
tester := match.NewTester(comparator, scenario)
matchStore := match.NewInMemoryTransactionStore(tester)
matchStore := match.NewInMemoryTransactionStore(tester, *cStorageCapacity)
mapping := getMapping(*cPath)
spy := getTransactionSpy(tester, matchStore)
router := getRouter(mapping, tester)
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ go 1.12
require (
github.com/azer/url-router v0.0.0-20140924232216-1a0aa252538c
github.com/basgys/goxml2json v1.1.0
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/corpix/uarand v0.1.0 // indirect
github.com/elazarl/go-bindata-assetfs v1.0.0
github.com/ghodss/yaml v1.0.0
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576
github.com/kr/pretty v0.2.0 // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.2.9 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/radovskyb/watcher v1.0.6
github.com/ryanuber/go-glob v1.0.0
github.com/stathat/go v1.0.0
github.com/tidwall/gjson v1.3.2
github.com/tidwall/gjson v1.5.0
github.com/twinj/uuid v1.0.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
)
20 changes: 18 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ github.com/azer/url-router v0.0.0-20140924232216-1a0aa252538c h1:vpYPra7G75NhuxN
github.com/azer/url-router v0.0.0-20140924232216-1a0aa252538c/go.mod h1:YT0FE3RD8ecPJBNkCdpeEAdZXPZgD2CqcRtpwuzjt9M=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/corpix/uarand v0.1.0 h1:HgE/0ismPNM4n3z2VeZxzwpMJiN4uSZ+SMpxxvoyffY=
github.com/corpix/uarand v0.1.0/go.mod h1:SFKZvkcRoLqVRFZ4u25xPmp6m9ktANfbpXZ7SJ0/FNU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
Expand All @@ -18,6 +23,11 @@ github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428 h1:Mo9W14pwbO9VfRe+y
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428/go.mod h1:uhpZMVGznybq1itEKXj6RYw9I71qK4kH+OGMjRC4KEo=
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576 h1:k82KNEG8vk59eHv/8xwBUh4dSR/t1wPiht4aDJm0SOY=
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576/go.mod h1:pE5zuSeg07RZZfWS158WpV7oUWb1++8T2jZ/UklLM3E=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.2.9 h1:heVeuAYtevIQVYkGj6A41dtfT91LrvFG220lavpWhrU=
Expand All @@ -27,7 +37,10 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI=
github.com/myesui/uuid v1.0.0/go.mod h1:2CDfNgU0LR8mIdO8vdWd8i9gWWxLlcoIGGpSNgafq84=
github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZVNoHScRE3EO9pVMM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/radovskyb/watcher v1.0.6 h1:8WIQ9UxEYMZjem1OwU7dVH94DXXk9mAIE1i8eqHD+IY=
github.com/radovskyb/watcher v1.0.6/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
Expand All @@ -37,9 +50,10 @@ github.com/stathat/go v1.0.0 h1:HFIS5YkyaI6tXu7JXIRRZBLRvYstdNZm034zcCeaybI=
github.com/stathat/go v1.0.0/go.mod h1:+9Eg2szqkcOGWv6gfheJmBBsmq9Qf5KDbzy8/aYYR0c=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI=
github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/gjson v1.5.0 h1:QCssIUI7J0RStkzIcI4A7O6P8rDA5wi5IPf70uqKSxg=
github.com/tidwall/gjson v1.5.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
Expand All @@ -56,9 +70,11 @@ golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smto
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 2 additions & 2 deletions internal/console/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ActionResponse struct {
type Dispatcher struct {
IP string
Port int
ResultsPerPage uint
ResultsPerPage int
MatchSpy match.TransactionSpier
Scenario match.ScenearioStorer
Mapping config.Mapping
Expand Down Expand Up @@ -322,7 +322,7 @@ func (di *Dispatcher) requestAllPagedHandler(c echo.Context) error {
})
}

offset := uint(page-1) * di.ResultsPerPage
offset := (page - 1) * di.ResultsPerPage

return c.JSON(http.StatusOK, di.MatchSpy.Get(di.ResultsPerPage, offset))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/match/spy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package match

import (

"github.com/jmartin82/mmock/pkg/mock"
)

Expand Down Expand Up @@ -49,7 +48,7 @@ func (mc Spy) GetAll() []Transaction {
return mc.store.GetAll()
}

func (mc Spy) Get(limit uint, offset uint) []Transaction {
func (mc Spy) Get(limit int, offset int) []Transaction {
return mc.store.Get(limit, offset)
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/match/spy_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package match

import (
"github.com/jmartin82/mmock/pkg/match/payload"
"github.com/jmartin82/mmock/pkg/mock"
"testing"


"github.com/jmartin82/mmock/pkg/match/payload"
"github.com/jmartin82/mmock/pkg/mock"
)

type DummyScenarioManager struct {
Expand All @@ -32,7 +31,7 @@ func (dsm DummyScenarioManager) SetPaused(_ bool) {
}

func TestFindMatches(t *testing.T) {
spy := NewSpy(NewTester(payload.NewComparator(), DummyScenarioManager{}), NewInMemoryTransactionStore(DummyMatcher{}))
spy := NewSpy(NewTester(payload.NewComparator(), DummyScenarioManager{}), NewInMemoryTransactionStore(DummyMatcher{}, 10))

m1 := Transaction{Request: &mock.Request{Host: "TEST1"}}
spy.Save(m1)
Expand All @@ -56,7 +55,7 @@ func TestFindMatches(t *testing.T) {
}

func TestMatchByResult(t *testing.T) {
spy := NewSpy(NewTester(payload.NewComparator(), DummyScenarioManager{}), NewInMemoryTransactionStore(DummyMatcher{}))
spy := NewSpy(NewTester(payload.NewComparator(), DummyScenarioManager{}), NewInMemoryTransactionStore(DummyMatcher{}, 10))

m1 := Transaction{Result: &Result{Found: true}}
spy.Save(m1)
Expand Down
28 changes: 19 additions & 9 deletions pkg/match/transaction_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@ type TransactionStorer interface {
Reset()
ResetMatch(mock.Request)
GetAll() []Transaction
Get(limit uint, offset uint) []Transaction
Get(limit int, offset int) []Transaction
}


//InMemoryTransactionStore stores all received request and their matches in memory until the last reset
type InMemoryTransactionStore struct {
matches []Transaction
sync.Mutex
checker Matcher
limit int
}

//Save store a match information
func (mrs *InMemoryTransactionStore) Save(req Transaction) {
mrs.Lock()
if mrs.limit > 0 && mrs.limit == len(mrs.matches) {
mrs.matches[0] = Transaction{}
mrs.matches = mrs.matches[1:]
}

mrs.matches = append(mrs.matches, req)
mrs.Unlock()
}

//Reset clean the request stored in memory
func (mrs *InMemoryTransactionStore) Reset() {
mrs.Lock()
mrs.matches = make([]Transaction, 0, 100)
mrs.matches = make([]Transaction, 0, mrs.limit)
mrs.Unlock()
}

Expand Down Expand Up @@ -61,13 +66,13 @@ func (mrs *InMemoryTransactionStore) GetAll() []Transaction {
}

//Get return an subset of current matches (positive and negative) in memory
func (mrs *InMemoryTransactionStore) Get(limit uint, offset uint) []Transaction {
func (mrs *InMemoryTransactionStore) Get(limit int, offset int) []Transaction {
mrs.Lock()
defer mrs.Unlock()

max := offset + limit
if max > uint(len(mrs.matches)) {
max = uint(len(mrs.matches))
if max > len(mrs.matches) {
max = len(mrs.matches)
}

if offset >= max {
Expand All @@ -81,8 +86,13 @@ func (mrs *InMemoryTransactionStore) Get(limit uint, offset uint) []Transaction
}

//NewInMemoryScenarioStore is the InMemoryTransactionStore constructor
func NewInMemoryTransactionStore(checker Matcher) *InMemoryTransactionStore {
reqs := make([]Transaction, 0, 100)
return &InMemoryTransactionStore{matches: reqs, checker: checker}
func NewInMemoryTransactionStore(checker Matcher, limit int) *InMemoryTransactionStore {
l := 100
if limit > 0 {
l = limit
}

reqs := make([]Transaction, 0, l)
return &InMemoryTransactionStore{matches: reqs, checker: checker, limit: limit}

}
Loading

0 comments on commit 19ad0c2

Please sign in to comment.