diff --git a/README.md b/README.md index 0d048b0..ac0ad55 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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.
diff --git a/cmd/mmock/main.go b/cmd/mmock/main.go index b10e45a..c778d93 100644 --- a/cmd/mmock/main.go +++ b/cmd/mmock/main.go @@ -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, @@ -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() @@ -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) diff --git a/go.mod b/go.mod index 0029680..93f81d9 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 59f9265..2021a39 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/internal/console/dispatcher.go b/internal/console/dispatcher.go index d8dcf8c..a2027ea 100644 --- a/internal/console/dispatcher.go +++ b/internal/console/dispatcher.go @@ -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 @@ -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)) } diff --git a/pkg/match/spy.go b/pkg/match/spy.go index 92bd31b..a3f8750 100644 --- a/pkg/match/spy.go +++ b/pkg/match/spy.go @@ -1,7 +1,6 @@ package match import ( - "github.com/jmartin82/mmock/pkg/mock" ) @@ -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) } diff --git a/pkg/match/spy_test.go b/pkg/match/spy_test.go index 0eeb7b6..d3dcf24 100644 --- a/pkg/match/spy_test.go +++ b/pkg/match/spy_test.go @@ -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 { @@ -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) @@ -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) diff --git a/pkg/match/transaction_store.go b/pkg/match/transaction_store.go index c859024..4da2e81 100644 --- a/pkg/match/transaction_store.go +++ b/pkg/match/transaction_store.go @@ -11,20 +11,25 @@ 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() } @@ -32,7 +37,7 @@ func (mrs *InMemoryTransactionStore) Save(req Transaction) { //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() } @@ -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 { @@ -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} } diff --git a/pkg/match/transaction_store_test.go b/pkg/match/transaction_store_test.go index a240abe..f0f0c3b 100644 --- a/pkg/match/transaction_store_test.go +++ b/pkg/match/transaction_store_test.go @@ -2,13 +2,13 @@ package match import ( "errors" + "github.com/jmartin82/mmock/pkg/match/payload" "reflect" "testing" "github.com/jmartin82/mmock/pkg/mock" - ) type DummyMatcher struct { @@ -24,7 +24,7 @@ func (dm DummyMatcher) Match(req *mock.Request, mock *mock.Definition, scenarioA func TestStoreRequest(t *testing.T) { - msr := NewInMemoryTransactionStore(DummyMatcher{}) + msr := NewInMemoryTransactionStore(DummyMatcher{}, 10) m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} msr.Save(m1) m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} @@ -34,7 +34,7 @@ func TestStoreRequest(t *testing.T) { t.Fatalf("Invalid store len: %v", len(msr.matches)) } - if cap(msr.matches) != 100 { + if cap(msr.matches) != 10 { t.Fatalf("Invalid store cap: %v", cap(msr.matches)) } @@ -46,7 +46,7 @@ func TestStoreRequest(t *testing.T) { func TestGetAll(t *testing.T) { - msr := NewInMemoryTransactionStore(DummyMatcher{}) + msr := NewInMemoryTransactionStore(DummyMatcher{}, 10) m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} msr.Save(m1) m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} @@ -67,7 +67,7 @@ func TestGetAll(t *testing.T) { func TestGet(t *testing.T) { - msr := NewInMemoryTransactionStore(DummyMatcher{}) + msr := NewInMemoryTransactionStore(DummyMatcher{}, 10) matches := []Transaction{ {Time: 1}, @@ -82,8 +82,8 @@ func TestGet(t *testing.T) { tests := []struct { msg string - limit uint - offset uint + limit int + offset int expected []Transaction }{ {"Zero limit and offset", 0, 0, []Transaction{}}, @@ -110,12 +110,12 @@ func TestGet(t *testing.T) { func TestGetOnEmptyStore(t *testing.T) { - msr := NewInMemoryTransactionStore(DummyMatcher{}) + msr := NewInMemoryTransactionStore(DummyMatcher{}, 10) tests := []struct { msg string - limit uint - offset uint + limit int + offset int expected []Transaction }{ {"Zero limit and offset", 0, 0, []Transaction{}}, @@ -137,7 +137,7 @@ func TestGetOnEmptyStore(t *testing.T) { func TestReset(t *testing.T) { - msr := NewInMemoryTransactionStore(DummyMatcher{}) + msr := NewInMemoryTransactionStore(DummyMatcher{}, 10) m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} msr.Save(m1) m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} @@ -147,7 +147,7 @@ func TestReset(t *testing.T) { t.Fatalf("Invalid store len: %v", len(msr.matches)) } - if cap(msr.matches) != 100 { + if cap(msr.matches) != 10 { t.Fatalf("Invalid store cap: %v", cap(msr.matches)) } @@ -157,7 +157,7 @@ func TestReset(t *testing.T) { t.Fatalf("Invalid store len: %v", len(msr.matches)) } - if cap(msr.matches) != 100 { + if cap(msr.matches) != 10 { t.Fatalf("Invalid store cap: %v", cap(msr.matches)) } @@ -169,7 +169,7 @@ func TestResetMatch(t *testing.T) { comparator := payload.NewDefaultComparator() tester := NewTester(comparator, scenario) - msr := NewInMemoryTransactionStore(tester) + msr := NewInMemoryTransactionStore(tester, 10) m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} msr.Save(m1) m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} @@ -179,7 +179,7 @@ func TestResetMatch(t *testing.T) { t.Fatalf("Invalid store len: %v", len(msr.matches)) } - if cap(msr.matches) != 100 { + if cap(msr.matches) != 10 { t.Fatalf("Invalid store cap: %v", cap(msr.matches)) } @@ -196,3 +196,75 @@ func TestResetMatch(t *testing.T) { } } + +func TestCapacityLimit(t *testing.T) { + + msr := NewInMemoryTransactionStore(DummyMatcher{}, 2) + m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} + msr.Save(m1) + m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} + msr.Save(m2) + + reqs := msr.GetAll() + + if len(reqs) != 2 { + t.Fatalf("Invalid store len: %v", len(reqs)) + } + + if reqs[0].Request.Host != "TEST1" { + t.Fatalf("Store FIFO error") + } + + if reqs[1].Request.Host != "TEST2" { + t.Fatalf("Store FIFO error") + } + + m3 := Transaction{Request: &mock.Request{Host: "TEST3"}} + msr.Save(m3) + reqs = msr.GetAll() + + if len(reqs) != 2 { + t.Fatalf("Invalid store len: %v", len(reqs)) + } + + if reqs[0].Request.Host != "TEST2" { + t.Fatalf("Store FIFO error") + } + + if reqs[1].Request.Host != "TEST3" { + t.Fatalf("Store FIFO error") + } + +} + +func TestInfiniteCapacity(t *testing.T) { + + msr := NewInMemoryTransactionStore(DummyMatcher{}, 0) + m1 := Transaction{Request: &mock.Request{Host: "TEST1"}} + msr.Save(m1) + m2 := Transaction{Request: &mock.Request{Host: "TEST2"}} + msr.Save(m2) + + reqs := msr.GetAll() + + if len(reqs) != 2 { + t.Fatalf("Invalid store len: %v", len(reqs)) + } + + if reqs[0].Request.Host != "TEST1" { + t.Fatalf("Store FIFO error") + } + + if reqs[1].Request.Host != "TEST2" { + t.Fatalf("Store FIFO error") + } + + + if len(msr.matches) != 2 { + t.Fatalf("Invalid store len: %v", len(msr.matches)) + } + + if cap(msr.matches) != 100 { + t.Fatalf("Invalid store cap: %v", cap(msr.matches)) + } +}