Skip to content

Commit 310c153

Browse files
authored
Merge pull request #294 from smocker-dev/fix/persistence
Fix/persistence
2 parents dfeb89c + c34033e commit 310c153

File tree

7 files changed

+85
-7
lines changed

7 files changed

+85
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
build/
55
coverage/
66
node_modules/
7-
sessions/
7+
sessions
8+
!tests/sessions
89
smocker
910
smocker.test
1011
yarn-error.log

Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ CADDY=$(GOPATH)/bin/caddy
5252
$(CADDY):
5353
cd /tmp; go get github.com/caddyserver/caddy/v2/...
5454

55+
.PHONY: persistence
56+
persistence:
57+
rm -rf ./sessions || true
58+
cp -r tests/sessions sessions
59+
5560
.PHONY: start
56-
start: $(REFLEX)
61+
start: $(REFLEX) persistence
5762
$(REFLEX) --start-service \
5863
--decoration='none' \
5964
--regex='\.go$$' \
@@ -79,7 +84,7 @@ test:
7984

8085
PID_FILE=/tmp/$(APPNAME).test.pid
8186
.PHONY: test-integration
82-
test-integration: $(VENOM) check-default-ports
87+
test-integration: $(VENOM) check-default-ports persistence
8388
mkdir -p coverage
8489
go test -race -coverpkg="./..." -c . -o $(APPNAME).test
8590
SMOCKER_PERSISTENCE_DIRECTORY=./sessions ./$(APPNAME).test -test.coverprofile=coverage/test-integration-cover.out >/dev/null 2>&1 & echo $$! > $(PID_FILE)

server/services/persistence.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func (p *persistence) StoreSessions(sessions types.Sessions) {
109109
return
110110
}
111111
var sessionsGroup errgroup.Group
112-
for _, ses := range sessions {
113-
session := *ses
112+
for i := range sessions {
113+
session := sessions[i]
114114
sessionsGroup.Go(func() error {
115115
if err := p.createSessionDirectory(session.ID); err != nil {
116116
return err
@@ -161,8 +161,8 @@ func (p *persistence) LoadSessions() (types.Sessions, error) {
161161
}
162162
var sessionsGroup errgroup.Group
163163
var sessionsLock sync.Mutex
164-
for _, ses := range sessions {
165-
session := *ses
164+
for i := range sessions {
165+
session := sessions[i]
166166
sessionsGroup.Go(func() error {
167167
historyFile, err := os.Open(filepath.Join(p.persistenceDirectory, session.ID, historyFileName))
168168
if err != nil {

tests/features/0_persistence.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Persistance
2+
version: "2"
3+
testcases:
4+
- name: Check Mocks
5+
steps:
6+
- type: http
7+
method: GET
8+
url: http://localhost:8081/mocks
9+
assertions:
10+
- result.statuscode ShouldEqual 200
11+
- result.bodyjson.__len__ ShouldEqual 1
12+
- result.bodyjson.bodyjson0.state.id ShouldEqual YnJEM95SR
13+
- name: Check History
14+
steps:
15+
- type: http
16+
method: GET
17+
url: http://localhost:8081/history
18+
assertions:
19+
- result.statuscode ShouldEqual 200
20+
- result.bodyjson.__len__ ShouldEqual 1
21+
- result.bodyjson.bodyjson0.context.mock_id ShouldEqual YnJEM95SR
22+
- name: Check Sessions
23+
steps:
24+
- type: http
25+
method: GET
26+
url: http://localhost:8081/sessions
27+
assertions:
28+
- result.statuscode ShouldEqual 200
29+
- result.bodyjson.__len__ ShouldEqual 1
30+
- result.bodyjson.bodyjson0.id ShouldEqual 3giPMr5IR
31+
- result.bodyjson.bodyjson0.history.__len__ ShouldEqual 1
32+
- result.bodyjson.bodyjson0.mocks.__len__ ShouldEqual 1

tests/sessions/3giPMr5IR/history.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- context:
2+
mockid: YnJEM95SR
3+
mocktype: static
4+
delay: ""
5+
request:
6+
path: /test
7+
method: GET
8+
date: 2024-01-22T21:06:00.547178247+01:00
9+
response:
10+
status: 200
11+
body:
12+
message: test
13+
headers:
14+
Content-Type:
15+
- application/json
16+
date: 2024-01-22T21:06:00.547400418+01:00

tests/sessions/3giPMr5IR/mocks.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- request:
2+
path:
3+
matcher: ShouldEqual
4+
value: /test
5+
method:
6+
matcher: ShouldEqual
7+
value: GET
8+
response:
9+
body: |
10+
{"message": "test"}
11+
status: 200
12+
headers:
13+
Content-Type:
14+
- application/json
15+
context:
16+
times: 1
17+
state:
18+
id: YnJEM95SR
19+
times_count: 0
20+
locked: false
21+
creation_date: 2024-01-22T21:05:27.349554475+01:00

tests/sessions/sessions.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- id: 3giPMr5IR
2+
name: 'Session #1'
3+
date: 2024-01-22T21:05:11.838034004+01:00

0 commit comments

Comments
 (0)