Skip to content

Commit

Permalink
Merge pull request #294 from smocker-dev/fix/persistence
Browse files Browse the repository at this point in the history
Fix/persistence
  • Loading branch information
gwleclerc authored Mar 20, 2024
2 parents dfeb89c + c34033e commit 310c153
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
build/
coverage/
node_modules/
sessions/
sessions
!tests/sessions
smocker
smocker.test
yarn-error.log
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ CADDY=$(GOPATH)/bin/caddy
$(CADDY):
cd /tmp; go get github.com/caddyserver/caddy/v2/...

.PHONY: persistence
persistence:
rm -rf ./sessions || true
cp -r tests/sessions sessions

.PHONY: start
start: $(REFLEX)
start: $(REFLEX) persistence
$(REFLEX) --start-service \
--decoration='none' \
--regex='\.go$$' \
Expand All @@ -79,7 +84,7 @@ test:

PID_FILE=/tmp/$(APPNAME).test.pid
.PHONY: test-integration
test-integration: $(VENOM) check-default-ports
test-integration: $(VENOM) check-default-ports persistence
mkdir -p coverage
go test -race -coverpkg="./..." -c . -o $(APPNAME).test
SMOCKER_PERSISTENCE_DIRECTORY=./sessions ./$(APPNAME).test -test.coverprofile=coverage/test-integration-cover.out >/dev/null 2>&1 & echo $$! > $(PID_FILE)
Expand Down
8 changes: 4 additions & 4 deletions server/services/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (p *persistence) StoreSessions(sessions types.Sessions) {
return
}
var sessionsGroup errgroup.Group
for _, ses := range sessions {
session := *ses
for i := range sessions {
session := sessions[i]
sessionsGroup.Go(func() error {
if err := p.createSessionDirectory(session.ID); err != nil {
return err
Expand Down Expand Up @@ -161,8 +161,8 @@ func (p *persistence) LoadSessions() (types.Sessions, error) {
}
var sessionsGroup errgroup.Group
var sessionsLock sync.Mutex
for _, ses := range sessions {
session := *ses
for i := range sessions {
session := sessions[i]
sessionsGroup.Go(func() error {
historyFile, err := os.Open(filepath.Join(p.persistenceDirectory, session.ID, historyFileName))
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions tests/features/0_persistence.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Persistance
version: "2"
testcases:
- name: Check Mocks
steps:
- type: http
method: GET
url: http://localhost:8081/mocks
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 1
- result.bodyjson.bodyjson0.state.id ShouldEqual YnJEM95SR
- name: Check History
steps:
- type: http
method: GET
url: http://localhost:8081/history
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 1
- result.bodyjson.bodyjson0.context.mock_id ShouldEqual YnJEM95SR
- name: Check Sessions
steps:
- type: http
method: GET
url: http://localhost:8081/sessions
assertions:
- result.statuscode ShouldEqual 200
- result.bodyjson.__len__ ShouldEqual 1
- result.bodyjson.bodyjson0.id ShouldEqual 3giPMr5IR
- result.bodyjson.bodyjson0.history.__len__ ShouldEqual 1
- result.bodyjson.bodyjson0.mocks.__len__ ShouldEqual 1
16 changes: 16 additions & 0 deletions tests/sessions/3giPMr5IR/history.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- context:
mockid: YnJEM95SR
mocktype: static
delay: ""
request:
path: /test
method: GET
date: 2024-01-22T21:06:00.547178247+01:00
response:
status: 200
body:
message: test
headers:
Content-Type:
- application/json
date: 2024-01-22T21:06:00.547400418+01:00
21 changes: 21 additions & 0 deletions tests/sessions/3giPMr5IR/mocks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- request:
path:
matcher: ShouldEqual
value: /test
method:
matcher: ShouldEqual
value: GET
response:
body: |
{"message": "test"}
status: 200
headers:
Content-Type:
- application/json
context:
times: 1
state:
id: YnJEM95SR
times_count: 0
locked: false
creation_date: 2024-01-22T21:05:27.349554475+01:00
3 changes: 3 additions & 0 deletions tests/sessions/sessions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- id: 3giPMr5IR
name: 'Session #1'
date: 2024-01-22T21:05:11.838034004+01:00

0 comments on commit 310c153

Please sign in to comment.